Solved

Array name like a constant pointer?

Posted on 2007-12-03
13
805 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
  • 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
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using Diagnostic tools in VS2015: Unresoved allocations 19 125
What is atomic operation? 6 67
Windows Server 2012 R2 - connect to computer 13 62
Reactjs with .NET 3 72
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 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