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;
}
}