On Mon, 2005-10-31 at 16:36, Matthew Saltzman wrote:
I meant to comment that these two different notations are functionally identical; an array name is nothing more than a pointer. For example, if we have the following code:
#include <stdio.h> void main (void){
char foo[] = "This is a string\n"; char *bar = foo; /* these expressions all print "T\n" */ printf("%c\n", foo[0]); printf("%c\n", *bar); printf("%c\n", bar[0]); printf("%c\n", *foo);/* ...and my favorite... */
printf("%c\n", 0[foo]);/* these expressions all print "i\n" */ printf("%c\n", foo[2]); printf("%c\n", *(bar + 2)); printf("%c\n", bar[2]); printf("%c\n", *(foo + 2));printf("%c\n", 2[foo]);/* That's right, folks, [] is commutative! */
/* Or, if you have a strong stomach, simplify to: printf("%c\n", "This is a string\n"[2]) ; printf("%c\n", 2["This is a string\n"] ) ; /* It's all simple addition...*/
}