Iterating over an array that has been passed by reference in c -


i wish write print array function in c. have herd when pass array c function via reference passes pointer first element. assuming can increment pointer iterate on array program segfaults.

i know firstly why program seg faulting , idiomatic approach writing function in c. in adavnce.

void print_array(int *array, int length) {     int = 0;      (i = 0; < length; array++) {         printf("%d\n", *array);     } }  int main (int argc, int *argv[]) {     int test[10] = {0};     print_array(test, 10); } 

your loop infinite, because i in i < length never changes; condition evaluates true. result, you're jumping past end of loop. suggest changing loop to:

for (i = 0; < length; i++) {     printf("%d\n", array[i]); } 

... or, if wish use array++, change condition:

for (int *end = array + length; array < end; array++) {     printf("%d\n", *array); } 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -