The code your wrote is wrong on a few levels. Your line
char *name[] = {"monkey", "pig", "goat", "dog", "cat"};
creates an array of char pointers, each pointing to a NULL-terminated string. So far, so good. However, your line
int *found = strstr (*name,"dog");
sets found to a pointer to the first occurrence of "dog" in *name = name[0] = "monkey". Besides not looking through the array name as you intend, you're also assigning the char * returned by strstr to an int *. Not good. Your next line
printf("Found at %i \n", found);
tries to print found, but the specifier requires an int and you're passing it found, an int *. These are all things to avoid, and I think a lot of this is undefined behavior.
What you want is a loop that uses strcmp, for example:
char *name[] = {"monkey", "pig", "goat", "dog", "cat"};
unsigned int numElements = sizeof(name)/sizeof(name[0]);
unsigned int i;
for(i = 0; i < numElements; ++i) {
if (strcmp(name[i], "dog") == 0) {
printf("Found at %u\n", i);
break;
}
}
if (i >= numElements) {
printf("Not found\n");
}
Computing numElements in this way won't work if you pass the array into a function, so you'll have to explicitly pass the number of elements in that case.
The code your wrote is wrong on a few levels. Your line
char *name[] = {"monkey", "pig", "goat", "dog", "cat"};
creates an array of char pointers, each pointing to a NULL-terminated string. So far, so good. However, your line
int *found = strstr (*name,"dog");
sets found to a pointer to the first occurrence of "dog" in *name = name[0] = "monkey". Besides not looking through the array name as you intend, you're also assigning the char * returned by strstr to an int *. Not good. Your next line
printf("Found at %i \n", found);
tries to print found, but the specifier requires an int and you're passing it found, an int *. These are all things to avoid, and I think a lot of this is undefined behavior.
What you want is a loop that uses strcmp, for example:
char *name[] = {"monkey", "pig", "goat", "dog", "cat"};
unsigned int numElements = sizeof(name)/sizeof(name[0]);
unsigned int i;
for(i = 0; i < numElements; ++i) {
if (strcmp(name[i], "dog") == 0) {
printf("Found at %u\n", i);
break;
}
}
if (i >= numElements) {
printf("Not found\n");
}
Computing numElements in this way won't work if you pass the array into a function, so you'll have to explicitly pass the number of elements in that case.
For character arrays, there is the strchr function, which searches an array for a character. If it finds it, it returns a pointer to that character. If not, it will return a NULL pointer. You can then use pointer subtraction to determine the index.
For general arrays, there is the library bsearch function if the array is sorted. Most compilers provide an lsearch nonstandard function that just does a linear search over the array to find a given value.
If you are using C++, then you have access to the find, lower_bound, and upper_bound STL algorithms, which perform similar tasks.
Hope this helps!
Videos
If this helps, here's what the inside of the array looks like
sessions[sessions.length] = {name: "", Scramble: "", totalSolves: 0, solves: []};