2

I am trying to understand pointers and I can't get one thing. For instance,

//part of code

int *p;
int tab[3];

so now we have variable tab that contains address of first element of the array. And I want to create a pointer that points on that pointer. So, I would do this like this way:

p=&tab;

If it would work, p would be the address of tab (that means it would point to tab) Unfortunately it doesn't work. So how to get to address of the pointer itself? what is the correct type for address? (addresstype *p) I want to point of the memory block which contains address of first element of tab, not on tab itself.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3402584
  • 410
  • 4
  • 8
  • 18
  • Give this a read: [arrays](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – chris Mar 17 '14 at 13:00

4 Answers4

7

so now we variable tab that contains address of first element of the array.

No, we have a variable tab which is an array.

And I want to create a pointer that points on this pointer.

If you want a pointer to the first element of the array, then that's easy: an array is implicitly convertible to that pointer (which is what leads to the widespread, but incorrect belief that an array is a pointer).

int * p = tab;

If you want a pointer to the array, rather than the first element, then that's a different type:

int (*p)[3] = &tab;

But you wouldn't usually want anything like that.

So how to get to address of pointer itself?

There is no pointer, so there's no way to get that address. If you did have a pointer, then you could take the address of that, just like any other object:

int * p = arr;   // Pointer to first element of array
int ** pp = &p;  // Pointer to pointer

I want to point of the memory block which contains address of first element of tab, not on tab itself.

That makes no sense; tab is the memory block containing its elements.

Alex Bravo
  • 1,601
  • 2
  • 24
  • 40
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • +1 for `int (*p)[3] = tab;`, are there any situations where such a declaration would be useful? – Sebastian Hoffmann Mar 17 '14 at 12:58
  • @Paranaix: It can be useful as a function argument, so that only a correctly sized array can be passed (or the size deduced as a template parameter). You'd be more likely to use a reference rather than a pointer, though. – Mike Seymour Mar 17 '14 at 13:00
  • When i was learning about c++ it was told that tab is not an array but that's a pointer on some memory blocks(one after another). – user3402584 Mar 17 '14 at 13:00
  • 1
    @user3402584, Then you were unfortunately told wrong. This is a common misconception. Arrays will happily convert to pointers, but they aren't the same thing. – chris Mar 17 '14 at 13:01
  • @user3402584: Then you were told wrong. An array is a compound object containing its elements; a pointer just holds the address of an object. Arrays are often accessed via pointers, but are not the same thing. – Mike Seymour Mar 17 '14 at 13:02
  • Hmm so it doesnt make sense for me. It works like a pointer(we do for example: *(tab+1) to gain value of tab[1] but its not a pointer? – user3402584 Mar 17 '14 at 13:03
  • 2
    @user3402584: An array is convertible to a pointer, as described in the answer. So `tab+1` converts it to a pointer, then adds one to get the address of `tab[1]`. But, despite allowing this conversion, the array itself is not a pointer. – Mike Seymour Mar 17 '14 at 13:06
  • hmmm,so why this wrong definition is put in cplusplus.com? – user3402584 Mar 17 '14 at 13:09
  • anyway ty for making it clear for me how all this process works. – user3402584 Mar 17 '14 at 13:15
  • @user3402584 Because cplusplus.com is a frequently wrong resource for learning C++ from. Look at [cppreference](http://en.cppreference.com/w/) or a book off the [Book List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?rq=1) – The Forest And The Trees Mar 17 '14 at 13:23
  • @user3402584 Because this misconception is so widespread, there are many people passing it on as fact. I was taught it in the nineties and it was just as wrong back then as it is now. – molbdnilo Mar 17 '14 at 13:29
3

Remember that arrays decays to a pointer to the first element. So whenever you need a pointer to an array, just use the array as it is. Like

p = tab;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

try this

int *p;
int tab[3];
p = &tab[0];
Marichyasana
  • 2,966
  • 1
  • 19
  • 20
0

Test this code and you'll see how p points directly to the first position of the array, so you can access its values from p as well.

int *p;
int tab[3];

tab[0] = 56;
tab[1] = 57;
tab[2] = 58;

p = tab;

cout << p[0] <<endl;
cout << p[1] <<endl;
cout << p[2] <<endl;