extracting strings with strstr and using unique variable names causes "initialization makes integer from pointer" warning
I made a program in C that takes a string and converts the separator to null characters so my string can be left in place while I have a series of pointers (defined by ss) that point to the beginning of each string segment. I can act on those pointers directly with printf() but when I assign them to a variable, I get an "warning: initialization makes integer from pointer without a cast" and the arrow in the compiler points to the value after the equals sign.
I want to avoid strtok and strsep and friends because I heard strtok overwrites memory spaces and strsep I think is for windows only. Plus I want something that is within the standard C library.
Am I declaring them incorrectly or is using simple variable names not possible in my scenario?
Here is my entire code with relevant documentation included:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char bufo[1024]; //We need to make space for string, blank it and store it
memset(bufo,0,1023); //or execution will crash
strcpy(bufo," This is\r\n a test\r\n String \r\n Thank you \r\n your computer \r\n bye");
char *sp=bufo;//set movable pointer to start of string
char* ss[10]; //setup an array of string pointers
int spn=1; //string pointer starts at 1...
ss[0]=bufo; //because the 1st entry is full string unless \r\n is found
while((sp=strstr(sp,"\r\n"))){
//as we find \r\n, replace it with 2 nulls and skip over it
*sp++='\0';*sp++='\0';ss[spn++]=sp;
}
//Trying to use simple labels to reference string segments inside the huge
//string produces "warning: initialization makes integer from pointer without a cast"
//from C compiler
//So this doesn't work (I have to delete the following 6 lines if I want the program to work 100%
char* piece1=ss[0],piece2=ss[1],piece3=ss[2],piece4=ss[3],piece5=ss[4];
printf("piece 1: '%s'\n",piece1);
printf("piece 2: '%s'\n",piece2);
printf("piece 3: '%s'\n",piece3);
printf("piece 4: '%s'\n",piece4);
printf("piece 5: '%s'\n",piece5);
//But this does work, but I don't want to use ss[x] all the time...
printf("The results....\n");
printf("piece 1: '%s'\n",ss[0]);
printf("piece 2: '%s'\n",ss[1]);
printf("piece 3: '%s'\n",ss[2]);
printf("piece 4: '%s'\n",ss[3]);
printf("piece 5: '%s'\n",ss[4]);
}
Solution 1:
On this line:
char* piece1=ss[0],piece2=ss[1],piece3=ss[2],piece4=ss[3],piece5=ss[4];
You're declaring one variable of type char *
and four variables of type char
. The *
binds to the variable, not the type. What you currently have can also be written like this:
char *piece1=ss[0],piece2=ss[1],piece3=ss[2],piece4=ss[3],piece5=ss[4];
Which should make it more clear what the problem is. You instead want:
char *piece1=ss[0], *piece2=ss[1], *piece3=ss[2], *piece4=ss[3], *piece5=ss[4];