What is wrong with the following lines?
//Thanks to Mark
#include <string.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
char datfile[127];
if(argc < 2) return -1;
strcpy_s(datfile, strlen(argv[1]), argv[1]);
strcat_s(datfile, strlen(argv[1]) + 4, ".dat");
printf("%s\n",datfile);//the warning appears here (why?)
return 0;
}
It shows Warning C4047 'function': 'const char *' differs in levels of indirection from 'char'
I've gone through the documentation provided by MSDN for C4047
. It names a term called levels of indirection
. I've gone through some discussions related with this topic i.e. levels of indirection
in the web and (as a newbie) I found those beyond the scope of my radar.
I'd be very glad if someone points out the problem with the code above and provides a simply understandable explanation of the term level of indirection
.
Verifiable example of original error:
Output of VS2015 (cl /nologo /W4 test.c):
"level of indirection" indicates mismatch in pointer level.
int, int*, int**
have different levels of indirection.With @Mat suggestion this the following line changed to double quotes:
Compiles with no warnings but crashes due to incorrect parameter use. The 2nd parameter to
strcpy_s
andstrcat_s
is the length of the destination buffer, not the source string length. Sincestrlen(arg[v])
doesn't include the nul terminator, thestrcpy_s
will fail because it will try to copy one more byte than indicated.Correct use of the 2nd parameter:
Output: