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.

Answer from Joshua Green on Stack Overflow
Top answer
1 of 2
6

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.

2 of 2
1

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!

🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.array.indexof
Array.IndexOf Method (System) | Microsoft Learn
This method is an O(n) operation, where n is count. ... Searches for the specified object in a range of elements of a one dimensional array, and returns the index of its first occurrence.
🌐
Quora
quora.com › How-can-I-find-the-index-of-an-element-in-an-array-in-C
How to find the index of an element in an array in C - Quora
Answer (1 of 9): [code]#include int main(){ int a[] = {1,2,3,4,5}; int Key = 3 //searching element int n= sizeof(a)/sizeof(a[0]); for(int i=0;i
🌐
Reddit
reddit.com › r/c_programming › why is array[index] == index[array] ?
r/C_Programming on Reddit: Why is array[index] == index[array] ?
July 28, 2022 - Aah, I think this was the missing piece. I had commutativity and the array[index] == *(array + index) == *(index + array) bit, but when I thought about index[array] for whatever reason I imagined something like *((&index) + array)
🌐
GitHub
gist.github.com › sachendra003 › 62b1189ef4f8dc8963ff69ba81609f0a
C program to Find Index of Array #1 · GitHub
C program to Find Index of Array #1. GitHub Gist: instantly share code, notes, and snippets.
🌐
GitHub
gist.github.com › naveen-kumar-vadla › ed2151bed2d9ea1dcc4045fb815ba0f7
Integer Array indexOf in c · GitHub
Integer Array indexOf in c. GitHub Gist: instantly share code, notes, and snippets.
🌐
Arduino Forum
forum.arduino.cc › projects › programming
How to get element index of an array? - Programming - Arduino Forum
August 11, 2023 - Good morning, I am working on a project where I need to get the element index of an array. The following may clarify this: char cti[]={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; If I need the placement number …
Find elsewhere
🌐
Arduino Forum
forum.arduino.cc › projects › programming
indexOf equivalent for c-string ? - Programming - Arduino Forum
April 11, 2020 - As i am working on a project that needs to check responses of the SIM800l to AT-commands, i easily want to be able to check for keywords within a c-string. I am using a Mega, which does have quite a lot of memory but it'…
🌐
Reddit
reddit.com › r/learnjavascript › how can i use .indexof() with arrays of objects?
r/learnjavascript on Reddit: How can I use .indexOf() with arrays of objects?
August 16, 2022 -

If this helps, here's what the inside of the array looks like

sessions[sessions.length] = {name: "", Scramble: "", totalSolves: 0, solves: []};
🌐
TutorialKart
tutorialkart.com › c-programming › c-find-index-of-specific-element-in-array
Find Index of Specific Element in Array in C
October 19, 2021 - #include <stdio.h> int main() { int arr[] = {2, 4, 6, 8, 10}; int x = 8; int arrLen = sizeof arr / sizeof arr[0]; int index = -1; for (int i = 0; i < arrLen; i++) { if (arr[i] == x) { index = i; break; } } if (index > -1) { printf("Index : %d\n", index); } else { printf("%d is not present in this array.\n", x); } return 0; } ... In this C Tutorial, we learned how to find the index of a specific element in given Array, with examples.
🌐
Reddit
reddit.com › r/explainlikeimfive › eli5: why the index of array data structure in c starts at 0, and not 1( as it feels natural and intuitive)?
r/explainlikeimfive on Reddit: ELI5: Why the index of array data structure in C starts at 0, and not 1( as it feels natural and intuitive)?
December 24, 2021 - Say someone said "paint a 3 meter long line starting from 4 meters away", you'd start at 4+0 meters and end at 4+3 meters. If you started at 4+1 meters you'd be painting incorrectly. The array data structure in C is a pointer to an address.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › indexOf
Array.prototype.indexOf() - JavaScript | MDN
The following example uses indexOf() to locate values in an array. ... const array = [2, 9, 9]; array.indexOf(2); // 0 array.indexOf(7); // -1 array.indexOf(9, 2); // 2 array.indexOf(2, -1); // -1 array.indexOf(2, -3); // 0
🌐
McNeel Forum
discourse.mcneel.com › grasshopper developer
C# Array.IndexOfFindAll - Grasshopper Developer - McNeel Forum
September 18, 2020 - (1) I am trying to replace items values, without a loop only with a python in function, of an array. (2) I am trying to get, without a loop only with a predicate, all items containing a certain value. What do I need to…
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › find-the-index-of-an-element-in-an-array-in-cpp
How to Find the Index of an Element in an Array in C++? - GeeksforGeeks
July 23, 2025 - To find the index of an element in an array in C++, we can use the std::find() function that searches the element in the given range and returns the pointer to the matching element.
🌐
ByteHide
bytehide.com › home › indexof usage in c#: tutorial
IndexOf Usage in C#: Tutorial (2026)
December 19, 2023 - In the given example, ‘z’ is not found in the array, so IndexOf reports back with a ‘-1’. Hereby, ‘-1’ is IndexOf’s messaging system indicating that the thing you’re looking for is missing. Let’s delve into the aspect of list handling using IndexOf. It will make your journey with C# lists smoother and speedier.
🌐
Quora
quora.com › Can-the-index-of-an-array-in-C-start-from-1
Can the index of an array in C start from 1? - Quora
Answer (1 of 5): You can happily ignore the 0th index in an array and start using from the second position (or later) if you want to. Sometimes this is useful if it’s more logical to map the index to an actual number or rank, e.g. x[5] contains the 5th value of ‘x’. Disadvantages are that: 1. Y...
🌐
Code Maze
code-maze.com › home › check if a string array contains a value and get index
Check If a String Array Contains a Value and Get Index
January 27, 2023 - Here, we pass two arguments (the array we want to search and the value to search for) to the IndexOf() method. This method returns the index of the first occurrence of value, or -1 if it is not found. Next, let’s implement a method to see how we can use the Array.FindIndex() method to perform this operation:
🌐
TutorialsPoint
tutorialspoint.com › how-to-use-the-indexof-method-of-array-class-in-chash
How to use the IndexOf(,) method of array class in C#?
using System; class Program { static void Main() { int[] arr = new int[10]; arr[0] = 100; arr[1] = 200; arr[2] = 300; arr[3] = 400; arr[4] = 500; arr[5] = 600; arr[6] = 700; arr[7] = 800; arr[8] = 900; arr[9] = 1000; int a = Array.IndexOf(arr, 800); Console.WriteLine(a); } }
🌐
Programiz
programiz.com › javascript › library › array › indexof
JavaScript Array indexOf()
currentIndex = array.indexOf(element, currentIndex + 1); } return indices; } var priceList = [10, 8, 2, 31, 10, 1, 65, 10]; var occurance1 = findAllIndex(priceList, 10); console.log(occurance1); // [ 0, 4, 7 ] var occurance2 = findAllIndex(priceList, 8); console.log(occurance2); // [ 1 ] var occurance3 = findAllIndex(priceList, 9); console.log(occurance3); // []