When working with pointers and arrays in C or C ++, it really helps to recognize them as very different constructs (I think one of the best books explaining this difference is a book called "Deep C Secrets", if I remember correctly ) What is stopping the waters is that there is a one-way silent conversion allowed from pointers to array names (inconsistency in the processing of the language of variable names), but it is very important not to interpret the existence of this decay phenomenon as implying equivalence.
To help us talk about this, let's introduce the idea of ββa βmemory cell." We model the memory cell as having two attributes:
a) value b) address
Then we can model a simple C ++ variable as having two attributes (we do not need types at this low level of abstraction):
c) name d) memory cell
Like most models, it has some drawbacks (it does not deal with an array with more than one element, but it is enough for our purposes).
So for example:
// non-array variable: name 'i', and memory cell: value=3, address=0x0A int i = 3; // non-array variable: name 'p', and memory cell: value=0x0A, address=0x0B int *p = &i; // array variable: name 'a', and memory cell: vale=4, address=0x0C int a[1] = { 4 }; // non-array variable: name 'b', and memory cell: value=0x0C, address = 0x0D int (*b)[1] = &a; // non-array variable: name 's', and memory cell: value=0x0C, address = 0x0E int *s = &a[0]; // non-array variable: name 't', and memory cell: value=0x0C, address = 0x0F int *t = a; // Here is the key difference! read on...
Now here is the main difference between an array variable and a non-array variable (pointer) C ++:
When a variable name is computed in C ++, it always calculates the value of its memory cell with one exception: if the variable names an array variable.
If the variable is the name of an array, it calculates the address of the memory cell.
The above two lines deserve attention.
Here are a few examples to help clarify the consequences (see the above variables):
int k = i; // the 'i' name evaluates to the value of its cell, so 'k' is set to 3 int *q = p; // 'p' evaluates to the value of its cell, so 'q' is set to 0x0A int *r = a; // 'a' evaluates to the *address* of its cell, so 'r' is set to 0x0C int (*c)[1] = b; // 'c' is set to 0x0D
This should in no way mean that the array variable is the same as the pointer variable.
They have different types in different ways, and any attempt to treat them as the same thing (that is, to define the variable name as an array in one translation unit and as a pointer to another) will lead to bad things.
So, for example, for. do not do that:
// myproj_file1.cpp
int array [100] = {0}; // here 'array' evaluates to the * address * of the first memory cell
// myproj_file2.cpp
extern int * array; // here 'array' evaluates to the * value * of the first memory cell
// Assuming the linker links the two
// what it does if you read the assembly, is something like this:
// extern int * array = (int *) array [0];
// but it doesn't have to, it can do anything, since the behavior is undefined
Hope this helps. If you still feel that further clarification may help, please ask the following question and feel free to get a copy (library?) Of this book "Deep C Secrets" :)
- The postscript page function types and their names and their decay are not relevant to most of this post
postscript I also did not intentionally notice that conversion of an array to a pointer does not occur when arrays are bound to reference types