Edit: I realized I can simplify the problem to just the following chunk of code:
unsigned char arr[20][50];
for(int i = 0; i<=20;i ){
strcpy(arr[i],"");
}
for(int i = 0; i<=20; i ){
for(int j = 0; j<5;j ){
strcat(arr[i],"0");
}
}
Anytime I am using strcat()
or strcpy()
, I get the following warning message in Clion:
Passing 'unsigned char [50]' to parameter of type 'char *' converts between pointers to integer types where one is of the unique plain 'char' type and the other is not
I am unsure how to address this. Thanks in advance for any help.
CodePudding user response:
It is perfectly fine to pass unsigned char *
to C standard library string function like strcpy()
, strcat()
etc. To get rid of warning message, you can simply cast the unsigned char *
argument to char *
while passing it to C standard library string functions, like this:
strcpy((char *)arr[i], "");
CodePudding user response:
Since arr
is a statically defined array (dimensions are constant), arr[i]
does NOT decay to a pointer, but is itself a 50-character array. So in this case you will have to specifically convert it to a pointer using &arr[i][0]
. That should fix it.
BTW, to initialize your array to all-empty-strings, it is much more efficient to use arr[i][0] = '\0' than
strcpy`.
Also, strcat
isn't very efficient either. Since you are just appending one character at a time, it makes more sense to keep an index to the next character in the string, and just store the character to it. Then you just need to be sure to terminate the string when you are done:
for(int i = 0; i <= 20; i ) {
int idx = 0;
for(int j = 0; j < 49; j ) {
arr[i][idx ] = '0';
}
arr[i][idx] = '\0';
}
And if you use this method, it is not necessary to initialize the array.