Can you explain the context in which that was said ?
Main Topics
Browse All Topicswhy we say that "Array name like a constant pointer"?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
An array of ints refers to one or more ints in consecutive locations in memory.
A pointer to an int points to one or more ints in consecutive locations in memory.
So, it's easy to see that the two are conceptually very similar.
This is also true for the way they are used. For example, the way you index into an array is the same as the way you index using a pointer. See the examples you posted yourself : arr[5], ptr[5], *(arr + 5) and *(ptr + 5) have the same meaning, namely to get the value at index 5 of array 'arr', resp. pointer 'ptr'.
You can also assign an array to a pointer, like :
>>>> The name of an array is the address of the first element in the array. I..e. it resolves to be an address.
>>Only in a context where a pointer is expected.
How do you mean; like if an int is expected, it's just an int value? Then you'd expect a diagnostic. And, if you do mean that, I would argue that it's still an address, but that it's then coerced into an int value.
Or, do you mean ...
void foo(char * p)
{
}
int main(void)
{
char p[] = "abc";
foo(p);
return 0;
}
Where because of pass by value, foo's p is a variable?
Or some other such thing??
>> How do you mean;
I mean that an array only "transparently reverts" to a pointer in the context where a pointer is needed (ie. when assigning to a pointer or when using the * dereference operator, or when adding an offset).
In all other context, the array name cannot be considered (equivalent to) a pointer, and is in fact quite different from a pointer.
For example, with :
int arr[10];
int *ptr;
&arr and &ptr have quite different results.
sizeof(arr) and sizeof(ptr) have quite different results.
An array cannot be assigned to.
Etc.
Below is a series of very good articles that discuss the differences between arrays and pointers. If you are trying to understand how and why they differ they are certianly a very good read.
Exceptions where arrays are not treated as a pointer
http://www.cplusplus.com/f
The difference between pointers and arrays
http://www.cplusplus.com/f
Array is not pointer
http://www.cplusplus.com/f
>>In all other context, the array name cannot be considered (equivalent to) a pointer, and is in fact quite different from a pointer.
You mean 'address' in all/most of the above I think - as I think we've discussed this before? I.e., that the common understanding of 'pointer' is an entity declared using pointer notation [*].
>>&arr ... sizeof(arr) ...
Yes, citing both & and sizeof is I believe certainly 'the norm' when discussing this subject.
>>An array cannot be assigned to.
Yes! As it's a constant. And even maybe when it's declared using * of course.
>> You mean 'address' in all/most of the above I think
I do mean pointer. But I can live with 'address' ;) Pick whichever you prefer heh. It doesn't change the point I was making. Arrays are not the same as pointers, and should not be seen as being the same. In some cases, an array will have the same behavior as a pointer (and will "transparently revert" to one if needed), but other than that they're two very different concepts.
>> Of course not. A int pointer only points to one int.
Depends how you look at it ... But then again, that doesn't really matter, as it doesn't change anything.
>> Arrays are not the same as pointers
Agreed. Just because an array decomposes to a pointer and they share similar semantics doesn't make them the same. An array has a type of T[x] whereas a pointer has a type of T * and the compiler specifically treats them as different types.
Consider....
char a[] = "hello"; // a is an array of 6 chars in size
char * p = "hello"; // p is a pointer of char * in size
It's like saying a char will automatically convert to an long therefore a char is a long. It is not. They are different types with differing storage sizes.
>>Arrays are not the same as pointers ...
And I never said they were - so I hope that isn't addressed to me.
>>>>Arrays are not the same as pointers ...
And I think one of the most interesting associated demonstrations of this involves multiple translation units. Not quite the same as we're chatting about here though, but, as an E.g.,
one.c
====
void blah(void);
char p[] = "boo";
int main(void)
{
blah();
return 0;
}
two.c
====
extern char * p;
void blah(void)
{
puts(p);
}
>>>> Depends how you look at it ...
It may seem like nit-picking but a pointer never points to multiple addresses ...
You may say it points to the first element of an array of integers - or even is pointing to a sequence of consecutive integers but surely not to 'one or more' integers. We shouldn't forgo the big advantage of using proper and clear definitions solely for the sake of a more 'popular' speaking ...
>> but a pointer never points to multiple addresses ...
When did I say that ?
>> or even is pointing to a sequence of consecutive integers but surely not to 'one or more' integers.
I'm not sure what the difference is, but I guess I'm missing something. Please enlighten me.
suoju1, does our discussion make sense to you, or are we complicating things here ? Please let us know if we're not helping you ...
Oh, and btw, Alex, if you want to quote me, then please quote the entire phrase - I said :
>> A pointer to an int points to one or more ints in consecutive locations in memory.
and then compare it to what you said :
>> or even is pointing to a sequence of consecutive integers
Anyway ... All of that is besides the point, and doesn't matter, as it doesn't change anything (I think I already said that).
>>>> please quote the entire phrase
You don't want to understand me?
IMO, it is important to state that a pointer contains one address (where it is pointing to) and *not one single information more than that*. So, even if you *know* that the address the pointer is pointing to contains an array of integers, it is not the pointer which tells you but you have to take that knowledge from somewhere else.
>>>> All of that is besides the point
I may remember you that the initial question is 'Array name like a constant pointer?'. The main difference of the array and the pointer is that the first has the size information while the second doesn't. So any statement which is likely to curtain that main difference isn't helpful.
@itsmeandnobodyelse
>> ... nit-picking ...
I wouldn't worry about it! Some people like to get their oar in with some of the most unhelpful and needlessly pedantic comments ... makes me always want to preceed my comments with something like, 'In nearly all cases ...', or 'usually ...'. Really pisses me off!
I'm not quite sure that the problem is here. A pointer and an array are different types, as I've already demonstrated the compiler knows this. An array will coerce to a pointer but that doesn't make an array a pointer. As far as I can tell, this is the only point Inifinity08 is making and the point is valid. To imply otherwise is misleading the OP, especially since this Q is, ostensibly, looking to seek clarification of the differences (and similarities) between arrays and pointers.
I'd suggest that if anyone doubts this assertion they try reading the following extracts from the current C++ standards document:
"8.3.4 Arrays [dcl.array]
1 In a declarationT D where D has the form
D1 [constant-expressionopt]
and the type of the identifier in the declarationT D1 is derived-declarator-type-l
identifier of D is an array type. T is called the array element type; this type shall not be a reference type, the
(possibly cv-qualified) type void, a function type or an abstract class type. If the constant-expression
(5.19) is present, it shall be an integral constant expression and its value shall be greater than zero. The
constant expression specifies the bound of (number of elements in) the array. If the value of the constant
expression is N, the array has N elements numbered 0 to N-1, and the type of the identifier of D is
derived-declarator-type-l
set of N sub-objects of type T. If the constant expression is omitted, the type of the identifier of D is
derived-declarator-type-l
derived-declarator-type-l
array of unknown bound of T, see 3.9. Any type of the form cv-qualifier-seq array ofN T is adjusted to
array of N cv-qualifier-seq T, and similarly for array of unknown bound of T. [Example:
typedef int A[5], AA[2][3];
typedef const A CA; // type is array of 5 const int
typedef const AA CAA; // type is array of 2 array of 3 const int
end example] [Note: an array of N cv-qualifier-seq T has cv-qualified type; such an array has internal
linkage unless explicitly declared extern (7.1.5.1) and must be initialized as specified in 8.5. ]
2 An array can be constructed from one of the fundamental types (except void), from a pointer, from a
pointer to member, from a class, from an enumeration type, or from another array.
3 When several array of specifications are adjacent, a multidimensional array is created; the constant
expressions that specify the bounds of the arrays can be omitted only for the first member of the sequence.
[Note: this elision is useful for function parameters of array types, and when the array is external and the
definition, which allocates storage, is given elsewhere. ] The first constant-expression can also be omitted
when the declarator is followed by an initializer (8.5). In this case the bound is calculated from the number
of initial elements (say, N) supplied (8.5.1), and the type of the identifier of D is array ofN T.
4 [Example:
float fa[17], *afp[17];
declares an array of float numbers and an array of pointers to float numbers. For another example,
static int x3d[3][5][7];
declares a static three-dimensional array of integers, with rank 3×5×7. In complete detail, x3d is an array
of three items; each item is an array of five arrays; each of the latter arrays is an array of seven integers.
Any of the expressions x3d, x3d[i], x3d[i][j], x3d[i][j][k] can reasonably appear in an
expression. ]
5 [Note: conversions affecting lvalues of array type are described in 4.2. Objects of array types cannot be
modified, see 3.10. ]
6 Except where it has been declared for a class (13.5.5), the subscript operator [] is interpreted in such a way
that E1[E2] is identical to *((E1)+(E2)). Because of the conversion rules that apply to +, if E1 is an
array and E2 an integer, then E1[E2] refers to the E2-th member of E1. Therefore, despite its asymmetric
appearance, subscripting is a commutative operation.
7 A consistent rule is followed for multidimensional arrays. If E is an n-dimensional array of rank
i× j× . . . ×k, then E appearing in an expression is converted to a pointer to an (n - 1 )-dimensional array
with rank j× . . . ×k. If the * operator, either explicitly or implicitly as a result of subscripting, is applied to
this pointer, the result is the pointed-to (n - 1 )-dimensional array, which itself is immediately converted
into a pointer.
8 [Example: consider
int x[3][5];
Here x is a 3×5 array of integers. When x appears in an expression, it is converted to a pointer to (the first
of three) five-membered arrays of integers. In the expression x[i], which is equivalent to *(x+i), x is
first converted to a pointer as described; then x+i is converted to the type of x, which involves multiplying
i by the length of the object to which the pointer points, namely five integer objects. The results are added
and indirection applied to yield an array (of five integers), which in turn is converted to a pointer to the first
of the integers. If there is another subscript the same argument applies again; this time the result is an integer.
]
9 [Note: it follows from all this that arrays in C + + are stored row-wise (last subscript varies fastest) and that
the first subscript in the declaration helps determine the amount of storage consumed by an array but plays
no other part in subscript calculations. ]"
"4.2 Array-to-pointer conversion [conv.array]
1 An lvalue or rvalue of type array ofN T or array of unknown bound of T can be converted to an rvalue
of type pointer to T. The result is a pointer to the first element of the array.
2 A string literal (2.13.4) that is not a wide string literal can be converted to an rvalue of type pointer to
char; a wide string literal can be converted to an rvalue of type pointer to wchar_t. In either case,
the result is a pointer to the first element of the array. This conversion is considered only when there is an
explicit appropriate pointer target type, and not when there is a general need to convert from an lvalue to an
rvalue. [Note: this conversion is deprecated. See Annex D. ] For the purpose of ranking in overload resolution
(13.3.3.1.1), this conversion is considered an array-to-pointer conversion followed by a qualification
conversion (4.4). [Example: "abc" is converted to pointer to const char as an array-to-pointer conversion,
and then to pointer to char as a qualification conversion. ]"
Perhaps an example would help:
int a [] = {0,1,2,3,4,5,6,7,8,9};
int * p = &a[0];
// Now we have both a and p referring to the same memory location.
// Legal.and equivalent and some compilers may even produce the same code for each.
a[0] = -1;
p[0] = -1;
// ILLegal.
a += 1;
// Legal.
p += 1;
So, in one sense, a exibits more 'const'ness than p.
Paul
Business Accounts
Answer for Membership
by: suoju1Posted on 2008-12-10 at 07:40:12ID: 23139775
Array b printed with:
Array subscript notation
b[ 0 ] = 10
b[ 1 ] = 20
b[ 2 ] = 30
b[ 3 ] = 40
Pointer/offset notation where
the pointer is the array name
*( b + 0 ) = 10
*( b + 1 ) = 20
*( b + 2 ) = 30
*( b + 3 ) = 40
Pointer subscript notation
bPtr[ 0 ] = 10
bPtr[ 1 ] = 20
bPtr[ 2 ] = 30
bPtr[ 3 ] = 40
Pointer/offset notation
*( bPtr + 0 ) = 10
*( bPtr + 1 ) = 20
*( bPtr + 2 ) = 30
*( bPtr + 3 ) = 40
why we say the above are true?