In Python we could use if x in list:
so I was wondering if there's a similar command in C, so that we don't have to go through the whole thing using a for
.
Asked
Active
Viewed 225 times
-2

user6122011
- 5
- 1
-
You should write a function to loop though the a passed array and return true if found. return false if all elements don't match. Use that function in for. – Robert Jacobs Mar 28 '16 at 19:42
1 Answers
6
How can you know whether a value is contained in an array without cycling through it? This is exactly what Python does under the hood. No, there's no magical way to instantly know this.

ForceBru
- 43,482
- 10
- 63
- 98
-
4
-
@AChampion, doesn't this answer the question? This is the answer. Sad, but true... – ForceBru Mar 28 '16 at 19:42
-
-
I knew it wasn't magically just wanted to know if there was a way of not using `for` implicitly – user6122011 Mar 28 '16 at 19:44
-
5@Olaf, it does: the question is 'is there a similar command in C so that we don't have to go through the whole thing using a `for`' the answer is _NO_ – ForceBru Mar 28 '16 at 19:45
-
@user6122011, you can always use a `while` loop instead :) Still, in order to search for something, you must cycle through some part of an array. – ForceBru Mar 28 '16 at 19:47
-
1Sounds like a perfect answer to me. "Is there a way..." ---> "No." – Lee Daniel Crocker Mar 28 '16 at 21:23