suoju1
asked on
Array name like a constant pointer?
Arrays and pointers closely related
why we say that Array name like a constant pointer???
why we say that Array name like a constant pointer???
ASKER
i got this from ppt of a C programming book, i am confusing about it.
You might be taking it out of context.
Can you post the exact quote, and under what section this information is in?
What's the name of the book?
Can you post the exact quote, and under what section this information is in?
What's the name of the book?
ASKER
deitel & deitel's C how to program,
but i don't have a english copy of the book with me now.
but i don't have a english copy of the book with me now.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
An array name is the address of the first element in the array - as such, as it's an address, it's a constant: the array is where it is. A pointer is a scalar variable [unless declared const] and so can be changed - it is a variable.
>>>> why we say that Array name like a constant pointer???
Maybe it is only translated wrongly?
An array and a constant pointer (to an array) have some features in common:
- both can be initialized only once.
- you neither can resize an array nor assign a new (pointer or ) array to a const pointer.
- if you pass them to a function they both can be turned to a non-const pointer.
void f(int* parr)
{
parr[0] = 0;
}
void g()
{
int arr[5];
int * carr const = new int[5];
f(arr); // ok, the array turns to a pointer when passed
f(carr); // ok, cause only the pointer was const and not the items
}
But there are differences as well:
- while the array was initialized with a bunch of elements, e. g.
int arr[5] = { 1, 2, 3, 4, 5, };
the pointer was initialized with some pointer value, e. g.
int * const carr = new int[5];
and there is no way to have the elements initialized like
it was possible with the array.
- you can't delete an array while you can do for pointers:
int * const carr = new int[5];
delete [] carr; // now carr was undefined
carr = NULL; // error: carr is const
- sizeof will return the array size in bytes but for a pointer it will return pointer size.
int arr[5] = { 1, 2, 3, 4, 5, };
int sarr = sizeof(arr); // 20 == 5 * 4
int * const carr = new int[5];
int scarr = sizeof(carr); // 4 == pointer size
Regards, Alex
Maybe it is only translated wrongly?
An array and a constant pointer (to an array) have some features in common:
- both can be initialized only once.
- you neither can resize an array nor assign a new (pointer or ) array to a const pointer.
- if you pass them to a function they both can be turned to a non-const pointer.
void f(int* parr)
{
parr[0] = 0;
}
void g()
{
int arr[5];
int * carr const = new int[5];
f(arr); // ok, the array turns to a pointer when passed
f(carr); // ok, cause only the pointer was const and not the items
}
But there are differences as well:
- while the array was initialized with a bunch of elements, e. g.
int arr[5] = { 1, 2, 3, 4, 5, };
the pointer was initialized with some pointer value, e. g.
int * const carr = new int[5];
and there is no way to have the elements initialized like
it was possible with the array.
- you can't delete an array while you can do for pointers:
int * const carr = new int[5];
delete [] carr; // now carr was undefined
carr = NULL; // error: carr is const
- sizeof will return the array size in bytes but for a pointer it will return pointer size.
int arr[5] = { 1, 2, 3, 4, 5, };
int sarr = sizeof(arr); // 20 == 5 * 4
int * const carr = new int[5];
int scarr = sizeof(carr); // 4 == pointer size
Regards, Alex
Arrays and pointers (const or otherwise) are NOT the same thing. An array has a completely different signature from a pointer.
int x[5] has a signature 'int [5]' and a size of 5 x 4 (4 bytes to an int) == 20 bytes
b. int const *x has a signature 'const int *' and a size of 1 x 4 (4 bytes to a pointer) == 4 bytes
A series of good articles on this subject by Siavosh Kasravi: -
http://www.cplusplus.com/articles/Arrptrexc.html
http://www.cplusplus.com/articles/Arrptr.html
http://www.cplusplus.com/articles/siavoshkc1.html
int x[5] has a signature 'int [5]' and a size of 5 x 4 (4 bytes to an int) == 20 bytes
b. int const *x has a signature 'const int *' and a size of 1 x 4 (4 bytes to a pointer) == 4 bytes
A series of good articles on this subject by Siavosh Kasravi: -
http://www.cplusplus.com/articles/Arrptrexc.html
http://www.cplusplus.com/articles/Arrptr.html
http://www.cplusplus.com/articles/siavoshkc1.html
The output from this program may help clarify the difference: -
int[5]
int const *
As you an see the compiler see two distinct types. Now if you then comment out Test(int [5]) and run it you'll see it then resorts to automatically degrading x to a int const * : -
int const *
int const *
If; however, you comment out Test(int const*) instead it will fail to build because it cannot automatically convert y, a int const *, to int [5].
C2664: 'Test' : cannot convert parameter 1 from 'const int *' to 'int []'
So, as you can see, to C++ they are 2 very different types; so much so that you can even overload functions using them.
int[5]
int const *
As you an see the compiler see two distinct types. Now if you then comment out Test(int [5]) and run it you'll see it then resorts to automatically degrading x to a int const * : -
int const *
int const *
If; however, you comment out Test(int const*) instead it will fail to build because it cannot automatically convert y, a int const *, to int [5].
C2664: 'Test' : cannot convert parameter 1 from 'const int *' to 'int []'
So, as you can see, to C++ they are 2 very different types; so much so that you can even overload functions using them.
#include <iostream>
char const * Test(int [5])
{
return "int[5]";
}
char const * Test(int const *)
{
return "int const *";
};
int main()
{
int x[5] = {0};
int const * y = x;
std::cout << Test(x) << std::endl;
std::cout << Test(y) << std::endl;
return 0;
}
They're only similar in that the "address" of the object referenced is the same...
the address of the const int *pMyPointer is some location in memory that won't move, even if you allocate some memory and assign it to the pointer; the array name is a locaition in memory, too, that won't change.
Both addresses will contain the address of the memory to start dereferencing at, but they really are not the 'same'.
-j
the address of the const int *pMyPointer is some location in memory that won't move, even if you allocate some memory and assign it to the pointer; the array name is a locaition in memory, too, that won't change.
Both addresses will contain the address of the memory to start dereferencing at, but they really are not the 'same'.
-j
ASKER
i now give out call the quote about "Array name like a constant pointer"
Arrays and pointers closely related
Array name like a constant pointer???
Pointers can do array subscripting operations
Define an array b[ 5 ] and a pointer bPtr
To set them equal to one another use:
bPtr = b; // bPtr= &b =&b[ 0 ]
The array name (b) is actually the address of first element of the array b[ 5 ]
bPtr = &b[ 0 ] //bPtr <> b[ 0 ] //you know why???
Explicitly assigns bPtr to address of first element of b
Why we use pointer convenient for array manipulation
Element b[ 3 ]
Can be accessed by *( bPtr + 3 )
Where n is the offset. Called pointer/offset notation
Can be accessed by bptr[ 3 ]???
Called pointer/subscript notation
bPtr[ 3 ] same as b[ 3 ]???
Can be accessed by performing pointer arithmetic on the array itself
*( b + 3 )
why is Element b[ 3 ] Can be accessed by bptr[ 3 ]???
Arrays and pointers closely related
Array name like a constant pointer???
Pointers can do array subscripting operations
Define an array b[ 5 ] and a pointer bPtr
To set them equal to one another use:
bPtr = b; // bPtr= &b =&b[ 0 ]
The array name (b) is actually the address of first element of the array b[ 5 ]
bPtr = &b[ 0 ] //bPtr <> b[ 0 ] //you know why???
Explicitly assigns bPtr to address of first element of b
Why we use pointer convenient for array manipulation
Element b[ 3 ]
Can be accessed by *( bPtr + 3 )
Where n is the offset. Called pointer/offset notation
Can be accessed by bptr[ 3 ]???
Called pointer/subscript notation
bPtr[ 3 ] same as b[ 3 ]???
Can be accessed by performing pointer arithmetic on the array itself
*( b + 3 )
why is Element b[ 3 ] Can be accessed by bptr[ 3 ]???
Did you look at the links I posted? It explains the differences quite clearly there!
A series of good articles on this subject by Siavosh Kasravi: -
http://www.cplusplus.com/articles/Arrptrexc.html
http://www.cplusplus.com/articles/Arrptr.html
http://www.cplusplus.com/articles/siavoshkc1.html
A series of good articles on this subject by Siavosh Kasravi: -
http://www.cplusplus.com/articles/Arrptrexc.html
http://www.cplusplus.com/articles/Arrptr.html
http://www.cplusplus.com/articles/siavoshkc1.html
array names and pointers are interchangeable in certain contexts, they are not however the same.
For example, you can assign an array name to a pointer :
int arr[];
int *ptr = arr;
and ptr will point to the first element of the arr array.
You can also use array subscripting for pointers :
ptr[2]
refers to the 3rd int from the start (of ptr).
For example, you can assign an array name to a pointer :
int arr[];
int *ptr = arr;
and ptr will point to the first element of the arr array.
You can also use array subscripting for pointers :
ptr[2]
refers to the 3rd int from the start (of ptr).
Who's we???
An array name is not like a constant pointer.