Array pointers (also called row pointers)
define INT (*p) [n];
() high priority, the first explanation is that p is a pointer to an integer one-dimensional array, the length of the one-dimensional array is n, it can be said that the step of P. In other words, when executing p+1, p crosses the length of n integer data.
To assign a two-dimensional array to a pointer, you should assign this value:
int a[3][4];
int (*p) [4]; The statement defines an array pointer to a one-dimensional array with 4 elements.
P=a; Assign the first address of the two-dimensional array to p, i.e. a[0] or &a[0][0]
p++; After the statement executes, that is, P=p+1;p crosses line a[0][] points to the line a[1][]
So an array pointer is also called a pointer to a one-dimensional array, also known as a row pointer.
Array of pointers
define int *p[n];
[] High priority, the first combination with P as an array, and then by int* that this is an integer pointer array, it has n pointer type array elements. When P+1 is executed, p points to the next array element, so that the assignment is wrong: P=a, because P is an unknown representation, only p[0], p[1], p[2]...p[n-1], and they are pointer variables that can be used to hold variable addresses. But can be so *p=a; Here *p represents the value of the first element of the pointer array, the value of the first address of a.
To assign a two-dimensional array to a pointer array:
int *p[3];
int a[3][4];
p++; The statement indicates that the P array points to the next array element. Note: Each element of this array is a pointer
for (i=0;i<3;i++)
P[i]=a[i]
Here int *p[3] indicates that a one-dimensional array holds three pointer variables, respectively p[0], p[1], p[2]
So you have to assign values separately.
So the difference between the two is enlightened, the array pointer is just a pointer variable, it seems that the C language is specifically used to point to a two-dimensional array, it occupies memory of a pointer to the storage space. Pointer arrays are multiple pointer variables that exist in the form of an array of memory, occupying multiple pointers of storage space.
It is also important to note that both the reference and the array name reference are the same when used to point to a two-dimensional array.
For example, to represent an array of I row J columns of an element:
* (P[I]+J), * (* (p+i) +j), (* (P+i)) [j], P[i][j]
Priority: () >[]>*
Source: http://www.cnblogs.com/hongcha717/archive/2010/10/24/1859780.html
=========================================================================
Again on the difference between A and &a
In that case, the problem came. Before we talk about the difference between a and &a, now take a look at the following code:
int main ()
{
Char a[5]={' A ', ' B ', ' C ', ' D '};
char (*P3) [5] = &a;
char (*P4) [5] = A;
return 0;
}
Which is correct for the use of P3 and P4? What is the value of p3+1? What is the value of p4+1? There is no doubt that both P3 and P4 are array pointers, pointing to the entire array. &a is the first address of the entire array, and a is the first address of the first element of the array with the same values but different meanings. In C language, the data types on both sides of the assignment symbol "=" must be the same, if different need to show or implicitly type conversion. P3 the data types on both sides of this defined "=" are exactly the same, and the data types on both sides of the "=" sign P4 this definition are inconsistent. The type on the left is a pointer to the entire array, and the data type to the right is a pointer to a single character. The following warning is given on visual c++6.0:
Warning C4047: ' Initializing ': ' char (*) [5] ' differs in levels of indirection from ' char * '.
Fortunately, the warning is given here, but since &a is the same as the value of a, the compiler simply takes the value of the variable as the right value, so there is nothing wrong with running it. But I still warn you not to use it.
. Char (*P2) [5]=a; must use cast, such as: char (*P2) [5]= (char (*) [5]) A;
=========================================================================
Casting of addresses
Let's look at the following example:
struct Test
{
int Num;
Char *pcname;
Short sdate;
Char cha[2];
Short sba[4];
}*p;
Suppose the value of P is 0x100000. What are the values of the following table expressions?
p + 0x1 = 0x___?
(unsigned long) p + 0x1 = 0x___?
(unsigned int*) p + 0x1 = 0x___?
I believe there will be a lot of people in the beginning not to understand what this problem means. In fact, we take a closer look, this knowledge point familiar. A pointer variable is added to an integer minus, so how do you parse it?
Remember the difference between the expression "a+1" and "&a+1" in front of us? Actually, it's the same here. The pointer variable is added to an integer minus the integer that is not directly added to the address in the pointer variable. The unit of this integer is not a byte but the number of elements. So: the value of P + 0x1 is 0x100000+sizof (Test) *0x1. As for the size of this structure is 20byte, the previous chapters have been explained in detail. So the value of P +0x1 is: 0x100014.
(unsigned long) p + 0x1 value? This involves casting, which forces the value saved by the pointer variable p to be cast to the unsigned long integer. Once a value is cast, its type is changed. So the expression is actually an unsigned long integer plus another integer. So its value is: 0x100001.
(unsigned int*) p + 0x1 value? The p here is cast to a pointer to an unsigned integer. So its value is: 0x100000+sizof (unsigned int) *0x1, equals 0x100004.
The above question seems to have no technical content, the following is a technical content: under the x86 system, what is the value?
Intmain ()
{
int a[4]={1,2,3,4};
int *ptr1= (int *) (&a+1);//point to the memory cell behind the A array, &a+1 means to move backward by 16 storage units
int *ptr2= (int *) ((int) a+1);//The address of the storage unit of a adds one byte
printf ("%x,%x", PTR1[-1],*PTR2);//ptr1[-1] actually points to the last cell of the A array, and *PTR1 represents the value stored by the 4 contiguous storage units after the address of the A array moves one byte after the other
return 0;
}
This is my lecture class A student asked my question, he saw on the internet, said to have baffled n individuals. After I read the question, I told him that these people certainly do not understand the Assembly, a person who understands the assembly, the problem is a small case. Here's an analysis of the problem:
According to the above explanation, the difference between &a+1 and a+1 is clear.
PTR1: Cast the value of &a+1 to the int* type, the variable ptr,ptr1 assigned to the int* type will definitely refer to the next int type data of array A. PTR1[-1] is parsed into a * (ptr1-1), that is, ptr1 backwards 4 byte. So its value is 0x4.
PTR2: As explained above, the value of (int) a+1 is the address of the second byte of the element a[0]. The address is then coerced to the value of the int* type to ptr2, meaning that the value of *ptr2 should be 4 bytes in a row starting with the second byte of the element a[0].
C-language review of pointer arrays and array pointers