Solved

Array name like a constant pointer?

Posted on 2007-12-03
13
806 Views
Last Modified: 2008-02-01
Arrays and pointers closely related
why we say that Array name like a constant pointer???
0
Comment
Question by:suoju1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +4
13 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 20396311
>>why we say that Array name like a constant pointer???

Who's we???

An array name is not like a constant pointer.
0
 

Author Comment

by:suoju1
ID: 20396389
i got this from ppt of a C programming book, i am confusing about it.
0
 
LVL 30

Expert Comment

by:Axter
ID: 20396420
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:suoju1
ID: 20396608
deitel & deitel's C how to program,
but i don't have a english copy of the book with me now.
0
 
LVL 30

Accepted Solution

by:
Axter earned 20 total points
ID: 20396787
I would never say this in a book, because it can lead to confusion.  Most C/C++ authors go out of their way to explain why a pointer is not the same as an array.

With that said, this particular author may have been referring a constant pointer address, and not a constant pointer pointie.

In the example below, p1 and p2 pointers are similar to array, in that you can not change what the pointer is pointing to.
That's a very limited similarity, and it's rare to find this type of constant pointer.
Usually you find a constant pointer where the constant is applied to the pointee, and not to the pointer address.


//Example:
char array[99] = "Hello World";
char const * p1 = array;
char * const p2 = array;

Open in new window

0
 
LVL 10

Expert Comment

by:peetm
ID: 20396926
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.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20397502
>>>> 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

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20397820
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
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20397971
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.

#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;
}

Open in new window

0
 
LVL 11

Expert Comment

by:jgordos
ID: 20398759
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
0
 

Author Comment

by:suoju1
ID: 20410542
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 ]???
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20410549
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
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20410562
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).
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C Programming - If Statement 8 84
what type of pointer is this pointer? 3 31
why doesn't assert terminate here? 2 18
C++ statement T∗ begin(Vector<T>& x) 5 42
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question