Reputation: 3325
I have some code that prompts a user to read in a file that has the format: [name] [someInt], but not all lines have [name]. I thus parse each line into a string array, and if it's length of 2, then it has a name and does a strcmp to find a match and then prints out the int associated. However, I'm running into some issues where I get
error: invalid operands to binary * (have ‘char *’ and ‘char *’)
when compiling at the printf("%s\n" *ans);
line
char * ans = NULL;
//open and read file line by line, below code is in line by 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;
}
}
Upvotes: 0
Views: 57
Reputation:
printf("%s\n" *ans);
^
You're missing a comma between these arguments. The compiler is interpreting the *
as multiplication, and not understanding how you expect it to multiply two strings.
Even with this change, you'll (probably?) still get a warning about types. Remove the *
entirely; I'm pretty sure you want to pass the string pointer to printf
, not the first character of the string.
Upvotes: 1
Reputation: 33
you are telling that res is NULL. Then, before malloc you are using realloc? Your mistake might be this.
Upvotes: 0