Re: loop indices in C array
Originally Posted by
Zacinfinite
[snip]
printf("array[%d] ", 2);
printf("%d ", array[2,0]);
printf("%d ", array[2,1]);
printf("\n");
return (0);
}
************************************************** *************
OUTPUT of Program:-
array[0] 134520864 134520872
array[1] 134520864 134520872
array[2] 134520864 134520872
************************************************** *************
ofcourse it prints out memory of row and column but how? Are numbers being used as pointers?
The sample code is (intentionally?) broken. You can not access multi-dimension arrays with commas separating the indices. An argument such as 'array[0,1]' actually evaluates to address of the second row in the array ('array[1]'), not the element in second column of the first row. (In C, expressions separated by commas are evaluated in sequence.)
If you wanted to retrieve the second element in the first row, proper C syntax would 'array[0][1]'.
My guess is that the book is either intentionally presenting you with bad code (in hopes that you will learn from discovering it), or that the book is actually not for C but for C++ (C++ permits comma-separated multi-dimension array indexing).
Last edited by saulgoode; August 21st, 2010 at 06:34 AM.
"We visited sixty-six islands and landed eighty-one times, wading, swimming (to shore). Most of the people were friendly and delightful; only two arrows shot at us, and only one went near -- So much for savages!" - J.C. Patterson
Bookmarks