c - Trying to set and print value of pointer -
i have code prompts user read in file has format: [name] [someint], not lines have [name]. parse each line string array, , if it's length of 2, has name , strcmp find match , prints out int associated. however, i'm running issues
error: invalid operands binary * (have ‘char *’ , ‘char *’)
when compiling @ printf("%s\n" *ans);
line
char * ans = null; //open , read file line line, below code in line line while loop char ** res = null; char * p = strtok (line, " "); int n_spaces = 0, i; while (p) { res = realloc (res, sizeof (char*) * ++n_spaces); if (res == null) { exit (-1); /* memory allocation failed */ } res[n_spaces-1] = p; p = strtok (null, " "); printf("%d\n", n_spaces); if(n_spaces == 2 && (strcmp(name,res[0]) == 0)) { namematch = true; printf("match found!\n"); ans = res[1]; printf("%s\n" *ans); break; } }
printf("%s\n" *ans); ^
you're missing comma between these arguments. compiler interpreting *
multiplication, , not understanding how expect multiply 2 strings.
even change, you'll (probably?) still warning types. remove *
entirely; i'm pretty sure want pass string pointer printf
, not first character of string.
c pointers
No comments:
Post a Comment