0

I have a puzzle here:

int *a=*b;
a[1]=3;
a[2]=5

a is a pointer array and is assigned value as b. In my understanding, a[]should be a address, so why in practice we can assign value to the place the pointer (in this case a[]) to? Any explain?

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
Wei
  • 39
  • 7

2 Answers2

2

I'm assuming this is C, or the C subset of Objective C/C++, since C+, Java, PHP, and the other C-like languages don't use pointers.

Use code tags and a single statement per line.

The statement

int *a = *b;

Creates a as a pointer to int. Not a pointer to pointer to int, a pointer to an int.

It then sets the current address in a to be a dereference of b, whatever b is. You did not show the declaration of b. Unless b is of type int **, you should be getting a compiler warning.

a is not a pointer array. a is a pointer to an int. It could point to a single int, or it could be made to point to an array of ints. The compiler can't tell the difference..

If b is of type int **, or pointer to pointer to int, then your statement dereferences one of those pointers and makes a point to the first sub-array inside b.

The code

a[1] = 3;

assumes that a is a pointer to an array of integers, and since the compiler can't do any range checking, it tries to index into the array and save a value to the second int in the block of memory that a points to. If a does not point to a block of memory large enough to contain at least 2 integers, this statement may crash on a modern computer using protected memory, it might also just overwrite the memory that follows.

As @EdS. points out in a comment below, this is known in the business as

"undefined behavior"

If you're going to use a C pointer like this, the burden is on you to make sure that it really points to valid memory, and if you're going to use a pointer as if it's an array, you the burden is on you to make sure that you don't exceed the bounds of the memory pointed to by the pointer.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • This is all good, but really, there should be at least a single mention of the term *undefined behavior*. – Ed S. Jan 31 '14 at 21:25
1

But let's answer to your question:

Here am I going step by step through your code (though I modified it a bit for the purpose of the example), and show you what it is doing in a fake memory, so you get the idea:

int b[4];

here you allocate 4 cells of memory and make b a label for a memory cell containing the address of the first memory cell:

variable     b                      
address      0x1   0x2 0x3 0x4 0x5 
memory      [0x2] [   |   |   |   ]

then:

int* a = b;

here you allocate a new memory cell that can contain an address, as it is declared with a pointer type, and you assign to it the content of the b memory cell:

variable     b                       a
address      0x1   0x2 0x3 0x4 0x5   0x6
memory      [0x2] [   |   |   |   ] [0x2]

then:

a[1]=3;
a[2]=5;

you're setting a value to a[1] which translates to *(a+1) using pointers arithmetics, which is the content of address 0x2 + 1 i.e. content of 0x3. Same thing with a[2]. The memory is now:

variable     b                       a
address      0x1   0x2 0x3 0x4 0x5   0x6
memory      [0x2] [   |  3|  5|   ] [0x2]

I hope this ASCII will make it a bit more clear how arrays are working! And you should definitely read the Kernighan and Ritchie book, as well as this documentation which both explain very well how the whole memory is managed, and the pointers arithmetics, and arrays.

zmo
  • 24,463
  • 4
  • 54
  • 90
  • N.B.: this is what happens to answers when answering just after reading stuff like [this](http://www.theatlantic.com/technology/archive/2014/01/the-lost-ancestors-of-ascii-art/283445/) – zmo Jan 31 '14 at 21:34
  • and really, read the book and the online documentation, they should really help you! – zmo Jan 31 '14 at 21:35
  • Ok, I will. I am new to c codes. Again thanks for your answer. – Wei Jan 31 '14 at 21:36
  • well, if you like to thank me, I'd prefer `+1` instead of a `-1` :-> – zmo Jan 31 '14 at 21:37
  • what is that? I did nothing on this. – Wei Jan 31 '14 at 21:40
  • ok, right, wrongly assumed about the types, based on my changed example… but it's crazy to see that it's getting -3 because of that 1% of the explanation, where I tell the OP to refer to other's comments anyway. – zmo Jan 31 '14 at 22:11
  • N.B.: edited the post and removed that part ; and if you've got other concerns, I'd be glad to hear, so I can improve my answer. When I take time to do something, I don't like to throw it out. – zmo Jan 31 '14 at 22:11