Sign in to follow this  

what exactly are arrays

This topic is 4839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I always thought, arrays are basicaly pointers. What exactly is the difference? A sample: int *pointer = new int[10]; pointer[1] = 123; pointer++; cout << *pointer; displayed value: 123 ---------- Now with "real" arrays: int array[10]; array[1] = 123; array++; cout << *array; The result: It doesnt compile. The failure is in the array++ statement.. ("not an l-value", i am using vc++ 6) -- Isnt the above array a pointer too? I always thought, the only difference between a "real" array declaration like int array[10] and int *pointer = new int[10], is, that the first one, reserves space on the stack, and the second statement, on the heap. And thats all. But it seems, there is more... Are arrays pointers, or not?

Share this post


Link to post
Share on other sites
An array is an ordered sequence of things. A pointer is a number which represents a memory location. They aren't the same thing. C and C++ let you implicitly convert an array to a pointer, and they have similar semantics, so I can see where the confusion arises.
In your case, you can't increment array because, simply, the increment operator can't be applied to arrays.
This picture might help you to visualise arrays and pointers.

Share this post


Link to post
Share on other sites
Quote:
Original post by captain fork
Now with "real" arrays:

int array[10];

array[1] = 123;

array++;

cout << *array;


The result: It doesnt compile. The failure is in the array++ statement.. ("not an l-value", i am using vc++ 6)

--

Isnt the above array a pointer too? I always thought, the only difference between a "real" array declaration like int array[10] and int *pointer = new int[10], is, that the first one, reserves space on the stack, and the second statement, on the heap. And thats all.

But it seems, there is more... Are arrays pointers, or not?


the name of a non constant statically allocated array is logically equivalent (may-not be physically equivalent) to a constant pointer to non constant data which automacially refers to the first element in the array that means it cannot refer to any other element or to another completely different array (you can offset it but cannot change it), you can alway have a non-contant pointer refer to any element in an array and move it to any other elements with-in it or a completely new array.

Share this post


Link to post
Share on other sites
Quote:
Original post by Beer Hunter

In your case, you can't increment array because, simply, the increment operator can't be applied to arrays.


But, isnt the name of an array a pointer to the first element? The failure was, trying to increment a constant POINTER.

But, the main question was: It is a pointer, right?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
As the other posters have said, an array can be implicitly converted to a pointer, but in reality it is something slightly different.

If you have this:
int numbers[20];
int * pnum = numbers;
cout
You'll find that sizeof(numbers) evaluates to 20 * sizeof(int) where int will probably be 4 on a lot of current systems.

It's kinda like how tomatoes are classified as fruits by nutritionists, but it's pretty much always used like a vegetable. They really aren't vegetables, but using them like them won't do a lot of harm. I hope that helps

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
As the other posters have said, an array can be implicitly converted to a pointer, but in reality it is something slightly different.

If you have this:
int numbers[20];
int * pnum = numbers;
cout
You'll find that sizeof(numbers) evaluates to 20 * sizeof(int) where int will probably be 4 on a lot of current systems.

It's kinda like how tomatoes are classified as fruits by nutritionists, but it's pretty much always used like a vegetable. They really aren't vegetables, but using them like them won't do a lot of harm. I hope that helps



Just to clear it up:

The index operator [] is the same, as, if i write something like:

cout << *(array + 1); - same as cout << array[1];

(assuming array is declared as int array[10])


It just looks like a pointer. Ok, i got the point, a array isn't a pointer, but, why is it sooo similiar? What was the reason to make it so?

Share this post


Link to post
Share on other sites
Quote:
Original post by captain fork
But, the main question was: It is a pointer, right?


It may or may not be its implementation dependant but you don't need to concern yourself with that because the c++ standard lets you treat an array name as a constant pointer so this:


type foo[size];

(foo + 2);


is legal because it just says at the address of foo + 2, it doesn't change the value of foo where as:


type foo[size];

++foo;
++foo;


is illegal because your trying to change the value of foo but its logically constant.

Share this post


Link to post
Share on other sites

This topic is 4839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this