简体   繁体   中英

why a pointer can be assigned value?

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?

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.

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 ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM