R
Richard
One of the most repeatedly given pointers is to
http://c-faq.com/aryptr/aryptr2.html
which discusses pointers and arrays and the subtle differences between
char a[] = "hello";
char *p = "world";
Now, my question is this - how do you feel about the way the FAQ
describes the compiler working. We already now that pointers and arrays
may or may not compile differently and may or may not be more efficient
than each other depending on target HW.
But should the FAQ really go into detail about how the compiler
calculates element addresses?
e.g
,----
| It is useful to realize that a reference like x[3] generates different code depending on whether x is an array or a pointer. Given the declarations above, when the compiler
| sees the expression a[3], it emits code to start at the location ``a'', move three past it, and fetch the character there. When it sees the expression p[3], it emits code to
| start at the location ``p'', fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to. In other words, a[3] is three places past
| (the start of) the object named a, while p[3] is three places past the object pointed to by p. In the example above, both a[3] and p[3] happen to be the character 'l', but
| the compiler gets there differently. (The essential difference is that the values of an array like a and a pointer like p are computed differently whenever they appear in
| expressions, whether or not they are being subscripted, as explained further in question 6.3.) See also question 1.32.
`----
http://c-faq.com/aryptr/aryptr2.html
which discusses pointers and arrays and the subtle differences between
char a[] = "hello";
char *p = "world";
Now, my question is this - how do you feel about the way the FAQ
describes the compiler working. We already now that pointers and arrays
may or may not compile differently and may or may not be more efficient
than each other depending on target HW.
But should the FAQ really go into detail about how the compiler
calculates element addresses?
e.g
,----
| It is useful to realize that a reference like x[3] generates different code depending on whether x is an array or a pointer. Given the declarations above, when the compiler
| sees the expression a[3], it emits code to start at the location ``a'', move three past it, and fetch the character there. When it sees the expression p[3], it emits code to
| start at the location ``p'', fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to. In other words, a[3] is three places past
| (the start of) the object named a, while p[3] is three places past the object pointed to by p. In the example above, both a[3] and p[3] happen to be the character 'l', but
| the compiler gets there differently. (The essential difference is that the values of an array like a and a pointer like p are computed differently whenever they appear in
| expressions, whether or not they are being subscripted, as explained further in question 6.3.) See also question 1.32.
`----