3 ms·
Slide 31 says: Actually, this is how arrays are handled in C. A C array is a set of consecutive memory addresses. The first value is pointed to by
by _kst_ 11y ago
Slide 31 says:
Actually, this is how arrays are handled in C.
A C array is a set of consecutive memory addresses.
The first value is pointed to by a pointer.
int a[] = {1, 2, 3}; // create an array
// a is just a pointer to the first element...
No, "a" is an array, not a pointer. Defining an array object does not create a pointer. The expression "a" is implicitly converted to a pointer to the array's first element in most but not all contexts.
If "a" were nothing more than a pointer to the first element of the array, then "sizeof a" would yield the size of a pointer rather than the size of the array object.
This is all explained very well in section 6 of the comp.lang.c FAQ, http://www.c-faq.com/ http://www.c-faq.com/.
- sjolsen 11y agoIt's also worth pointing out that there are no array parameters. If you write void foo (char bar [42]) { // ... } then within the scope of foo, bar will be a pointer to char, not an array of char. You can, on the other hand, write void foo (char (*bar) [42]) { // ... } in which case bar will be a pointer to array of 42 char (and notably, not a pointer to a pointer to char!). C does some very strange things. There usually is (or at least was) a good reason for it to do so, but especially if you're a beginner, you need to be on your toes if you really want to use it well.