func should take array and find a index of a target number :

``````   int find_index(int num,int Niz[],int index){

if(num == Niz[index])
return index;

else if(num != Niz[index]){

find_index(num,Niz,index++);

}

else {return -1;}

}
``````

I quess I know am accesing unacesable memory, just cant figure out where

The function doesn't make much sense. For example, line 15 is unreachable, If line 5 is false then the only other option is for line 8 to be true. This is a simplified version of your function

``````   int find_index(int num,int Niz[],int index){

if(num == Niz[index])
return …``````

## All 4 Replies

The function doesn't make much sense. For example, line 15 is unreachable, If line 5 is false then the only other option is for line 8 to be true. This is a simplified version of your function

``````   int find_index(int num,int Niz[],int index){

if(num == Niz[index])
return index;

find_index(num,Niz,index++);

return -1;

}
``````

Besides that, that happens when num is not in the array at all? The recursion continues infinitely because there is nothing to stop it and the value of index keeps incrementing forever.

A simple loop would be a lot more efficient for this purpose than recursion.

yeah, but I need to use recursion. Whould you care to help me how to stop func from calling if num == Niz[index]?

Anyway this is a working function :

``````int find_index(int num,int Niz[],int index){

if(index > 9)
return -1;

if(num == Niz[index])
return index;

if(num != Niz[index])
find_index(num,Niz,index+1);

}
``````

I just don't understand why does it work with index+1, but doesen't with index++, if you could just explain that. Thanks for your help

index+1 first increments index then does the recursive call. index++ doesn't increment index until after the recrsive function call.

It's similar to coding

`++index or index++`

Somehow that function needs to know the number of elements in the array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.