compiler errors - C - return value at index: control reaches end of non-void function -
first of all: started programming in c (my first programming language) , may stupid/simple question, trying implement function returns value @ index in array/list. error compiler: "error: compiler reaches end of non-void function" though returning values of type int. in advance:
int list_get(int list[], int length, int index) { if(index <= capacity){ for(int = 0; <= capacity; i++){ if(i == index) return list[i]; } } else{ printf("index out of range. \n"); return 0; } }
the compiler sees, both return
statements under sort of conditional block, tries warn you. nothing wrong, such.
consider flow,
index <= capacity
true,- for none of iterations,
i == index
true. - for loop exited.
in case, there's nothing returned....
to avoid, move return 0;
statement out of else
block. aware of possible side effect, if i == index
check fails, return 0.
Comments
Post a Comment