Quote: If you wouldn't mind explaining yourself instead of making a one-or-two line comment, I'd appreciate it... :) (statements on their own are nearly useless)
Sure thing. Tell me if I need to clarify anything since this is quite a large subject (amazingly enough).
Quote: (arrays) they're pointers with a few additional properties but indexing into an array is basically the same as indexing into a pointer.
Let me being my argument by referring to the most basic fact - the ISO/ANSI C standard specifically distinguishes arrays and pointers as two seperate types. It constantly refers to them as seperate types.
Now, indexing into an array is definately *not* the same as indexing into a pointer. The compiler sees both types differently. Lets assume we have an array a[5] and a pointer p. Let us say we wanted to access index 3 of both objects. For "a", the compiler would emit code to begin at the location of a, move past it 3 objects, and get the value. For "p", the compiler must start at location of p, get the pointed object, start again at the pointer object's location, move three past, and then it finally ends up at the desired element. Clearly the indexing of both arrays and pointers are not the same.
(Reference:http://www.eskimo.com/~scs/C-faq/q6.2.html)
(I slightly stole my example from the FAQ ;).)
Quote: I'd be curious to hear what "very different things" arrays and pointers do, except for what I've already outlined about being allocated, and the additional (small) details about not being able to e.g. make an array 'point' somewhere else.
"small details"? Let me just list what I can think of when arrays cannot act like pointers:
-If you had a global variable "char array[10]", you could not reference it in another translation unit by saying "extern char *array;"
-Like you said, an array is not a modifiable lvalue.
-Arrays cannot be truly assigned to a pointer of a pointer (it will compile, but the types are incompatable). Ex:
void f(char **p) { }
int main( void )
{
char *p;
char ar[5];
f(&p);//OK
f(&ar);//Incompatable types
}
-You cannot have an array of a function.
-You cannot have an array of an incomplete type.
-sizeof of an array will yield a very different result
Basically, this is all that I can think of off the top of my head. Like I said before, an array is not a pointer but it can decay into a pointer of the same type.
Quote: By the way, it seems awfully interesting that the entire concept of strings in C depends on the 'mere convenience' of arrays acting as pointers when passed to functions. Again, I'd like to hear your explanation as to why these are so incredibly different.
I fail to understand for what you are asking. I don't understand what you mean by "the entire concept of strings in C". String literals are considered "special" (as per the standard) when it comes to assigning them to arrays, if that answers any questions that you were implying.
|