I do believe an array name IS the first value's address.
For example:
So in a way, an array is a pointer, but they have distinctions of course.Code:int array[5]; *(array) = 5; array[0] = 5; // same thing
I do believe an array name IS the first value's address.
For example:
So in a way, an array is a pointer, but they have distinctions of course.Code:int array[5]; *(array) = 5; array[0] = 5; // same thing
★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★
As I have repeated, no, that is not true. Semantically, a conversion happens from array to pointer to first element of array.Originally Posted by execute
Yes, you could look at it like that, especially since array notation and pointer notation are also mostly interchangeable.Originally Posted by execute
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Isn't it the same thing then?
If int mins[5] then mins is of the type pointer to int (a[0] specifically), but its size is the total no. of bytes that make up that object ie if int is 4 bytes then 5*4 = 20 bytes. I fail to see the difference in what you have quoted and what is given in K&R's ANSI C book.
I would suggest reading what you quote. An expression that has type "array of foo" has type "array of foo" more or less by definition. It can be converted to an expression of "pointer to foo", but it is not, in and of itself, of that type.
I don't have a copy of K&R, so I don't actually know what you're referring to. Do you mean
? If so, then I'm a little bit surprised (ok, a lot surprised) that that appears. I understood that arrays were a bolted-on after-market accessory to original C, but I thought that by the time ANSI came around arrays were a full part of the language.Code:int mins[5]; /*the type of mins is pointer to int*/
I can admit that this is being pedantic, but there is a pedantic difference. If you say that mins is of type pointer to int, then it follows that sizeof(mins) == sizeof(int*), and likewise sizeof(mins) == sizeof(&mins[0]). However, empirically you know that this is not the case. The explanation is that mins is not a pointer; it is an array.Originally Posted by itCbitC
Let's use cas' other example. Now, you say that mins is of type pointer to int. That implies that &mins is of type pointer to pointer to int. So, we can write this program:
Since p of type int**, *p is of type int*, and (*p)[1] is of type int. We expect that the output is 1. Compile and run the program. What output do you get?Code:#include <stdio.h> int main(void) { int mins[5] = {0, 1, 2, 3, 4, 5}; int **p = &mins; printf("%d\n", (*p)[1]); return 0; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I guess all I am trying to say is that the compiler has double-standards (no pun intended!) when it comes to arrays. When given as the operand to the sizeof operator, it's an object that has been allocated a chunk of bytes; otherwise it's converted to be of the type "pointer to ..." though it's not an lvalue as it's a const*.
excerpt from book below and perhaps you can clear the mists swirlin' in head!
AFAIK arrays were in pre ANSI C too though frankly I'm not sure if they were bolted on as a value added selling point.Code:primary-expression [ expression ] A primary expression followed by an expression in square brackets is a primary expression. The intuitive meaning is that of a subscript. Usually, the primary expression has type pointer to . . ., the subscript expression is int, and the type of the result is . . . . The expression E1 [E2] is identical (by definition) to * ( (E1) + ( E2 ) ) . All the clues needed to understand this notation are contained in this section together with the discussions in ...
I agree, but I suppose that the practical reason is that copying the array would have been an expensive default, leading to a proliferation of &a[0] to pass an array named a as an argument. Making the conversion to a pointer to the first element as a default simplifies that syntax, at the cost of introducing special cases somewhere else.Originally Posted by itCbitC
execute's comment in post #16 seems fine to me: "so in a way, an array is a pointer, but they have distinctions of course".Originally Posted by itCbitC
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
So here's where the word "conversion" comes in again. If E1 is an array, and the name is evaluated by itself, it is converted to a pointer to the first element (not permanently; think of it as a new temporary object of type pointer to ... if you want). So E1[E2] is syntactic sugar for *(E1 + E2); in this expression E1 is evaluated by itself (before the +E2 happens), so the conversion happens. (And then the pointer arithmetic happens, and the dereferencing.)
From reading the to and fro here today I think the concept has settled in my head somewhat. The array name is not really a pointer, it's just converted to/used as one in certain situations, like when passing an array as an argument into a function. But since it's not really a pointer, the operator sizeof gives the total size of the array as expected. Am I right? I'm pretty much new to C and the concept of pointers and as I was warned, it can be a little tricky to the beginner!
Yes, that's true.
Well, the thing is that sizeof (once again) does not decay the array into a pointer, thus sizeof sees the "real type" and can deduce the size from that.But since it's not really a pointer, the operator sizeof gives the total size of the array as expected. Am I right? I'm pretty much new to C and the concept of pointers and as I was warned, it can be a little tricky to the beginner!
But when you use the array somewhere else, such as pass it into a function, it becomes a real pointer, and thus sizeof sees the type of the pointer.
It worked on the function you declared the argument as an ar[5], it didn't work on the function where the parameter was declared as a[] because a[] = *a.
Last edited by since; 12-08-2009 at 09:13 PM.