Question about array

Hi guys, I have a question about Arrays;

Say I have a bunch of ints in an array, and I have another int (myInt) that changes. I want to perform a task if myInt matches any of those stored in the array. If myInt doesnt match any of the intigers stored in the array then the task isnt performed. Probably suuuper simple, just having a bit of a brain fart at the moment :confused:

Thanks!

Hello,

You have to use a for loop

Google, "match a value in an array C"

Something like this should work.

int myInt = 2; //The variable that changes.
int array[4] = {1, 2, 3, 4};

int size = sizeof(array) / sizeof(array[0]); //Always calculate the size in case you decide to modify the array.

while(size){
	size--;
	if(array[size] == myInt){
		performTheTask();
		break; //We found a match. Remove the break if you want the task to be performed multiple times in case there are multiple matching values.
	}
}


void performTheTask(){
	//Your implementation here.
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.