Noob question about pointers and arrays.

So I'm originally a python guy and had to switch over to C++ for a project, I used to know C++ somewhat before(long time ago) and am getting back into it. Anyway I've been studying pointers and have a question.

So, I gather that an array is a pointer, not just something that is treated like an array correct? As in if I create an array I just created a pointer yes? As in if I create a pointer that points to a single variable of type 'int' it technically is an array that has one entry of type 'int' in it correct?

And using this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main ()
{

	int a [10];

	
	for (int numb = 0; numb < 10; numb ++)
	{
		*(a + numb) = numb + 1;
		cout << *(a + numb);
	}
	
	cout << "\n";

	for (int numb = 0; numb < 10; numb ++)
	{
		cout << *(a + numb);
		
	}

	return 0;
}


means that the array 'a' is actually a pointer and I didn't make the compiler do something extra in order to use it like an array correct?


thanks in advance
So, I gather that an array is a pointer, not just something that is treated like an array correct?


No.

An array is a number of elements of the same type, arranged in a consecutive block of memory, created using the array creation notation.

A pointer is a primitive data type just like an int or a double. It often takes up the same amount of memory as an int, and holds an address, which is essentially just a number. Pointers have some special properties. Firstly, you can dereference a pointer, in which case the returned value is the object at memory location "address", where address is the address I referred to in my previous sentence. The object returned will be of type whatever-type-the-pointer-is. If you made a void* pointer, you will have to cast the returned object to the appropriate type yourself (this is frowned upon). Second special property; if you add or subtract from a pointer, the value of the pointer (which is a memory address) gets changed exactly the amount to point to the next object of the right type.

e.g. if your pointer points to an object that is 10 bytes in size, and you add 1 to your pointer, the pointer value will actually go up 10 bytes, to point to the next object. If you subtract 1, the value will go down enough to point to the previous object.

A pointer and an array are not the same thing. If you create a pointer, you have not created the thing you want it to point at. If you create a pointer but never give it a value, it will point somewhere in the memory at random.
1
2
3
4
5
6
7
8
9
int* p; // Made a pointer. It takes up probably the same memory as an int.
            //  Right now it has a garbage value. Could be pointing at any random memory.
p=NULL; // Now I have set the value to p to NULL, which will be useful to me when I 
              //   want to test in the future if this pointer actually points at something useful

int x = 7;// Made an int.
p= &x; // Now I have given p the value "address of x".
cout << p; // This will not be 7. It will be the address of x.
cout << *p; // This will be the value of whatever p points at. I have "dereferenced" p. It will be 7 


An array is not the same thing as a pointer.
Last edited on
ok, I see what you're saying here, but I'm a bit confused, you say that one of the differences is that a pointer can dereference information stored at an address, yet when using the line:

cout << *(a + numb) << endl;

from the above example, it dereferences at address '(a + numb)' at least I think that when you use the '*' in that way it is a dereference.


and when adding in the following code:

cout << (a + numb) << endl;

spits out an address, rather than the value of a variable or array entry

and if I add this bit of code onto the beginning of the previously mentioned sample:

1
2
3
4
5
6
7
8
int * b;
	int c;

	c = 10;

	b = &c;

	cout << b[0] << endl;


it spits out the value of 10, even though 'b' was never declared as an array.

am I missing something? Is an array just meant to act like a pointer, but in fact has something extra to differentiate it from a pointer?

I'm not trying to be argumentative here or anything, but from what I see, an array walks talks and looks like a pointer, at least it seems to get treated the same way in code. I guess my main question is, is there something that goes on under the hood of a C++ compiler that makes an array different from a pointer?
This:

b[0]

is interpreted identically to

*(b+0)

which you could also write as

0[b]


an array walks talks and looks like a pointer


Except that a pointer is the size of an int (usually), and arrays can vary in size from the size of a char all the way up to as many megabytes or gigabytes as your system will manage before it refuses to make a bigger array.

Here is more:

http://www.cplusplus.com/forum/articles/9/
Topic archived. No new replies allowed.