Pointers and arrays in the C language

Source: Internet
Author: User
Tags modifier

The following excerpt extracts the minutes of the proceedings that I have authored. For the shortcomings of this article, you can put forward your own views.

Q1: What exactly is the pointer and array?

A: pointers and arrays are inherently different. The pointer is a memory address, under a 32-bit system, a pointer is always 4 bytes; The array is a contiguous memory space, and we can get the array size and the starting address of this contiguous memory space from a defined array. This starting address is the address of the first element of the array, and more specifically the first address of the first element in the array.

In the C language, there is only one-dimensional array. However, when an element of a one-dimensional array is still a one-dimensional array, a so-called one-dimensional array is formed. How can this be confirmed? The memory of two-dimensional arrays is still stored in the same linear way as one-dimensional arrays, except that the elements of this array are stored linearly in the other one-dimensional array. Multidimensional arrays follow this analogy.

Q2: Can I also get a more detailed description of arrays and pointers?

A: Of course you can.

We usually use a pointer like this:

int a[5];
int *p=a;

We first define a pointer to the int type P, which is initialized to a when p is defined, and we usually refer to this behavior as "p points to a shape array of size 5 a". In fact, we can think more concretely: does P really point to the entire array? Of course not. P just points to an int type variable. In this example, p points to the first element of the A array. In other words, p stores the &a[0]. In addition, p is a variable, we can self-decrement p (in the range of the A array is valid), let P point to the other elements of a array. In other words, p can store the address of any element in the entire array (more specifically the first address of the element). The array name A is also an address, but it is a constant, which always stores the first address of the array and cannot be changed.

For the P-pointers mentioned above, we can access the arrays by means of pointers (p+i) and subscripts P[i]. For array name A, we can also access the array by means of * (a+i) and subscript a[i]. For the P pointer, the process of accessing an array as a pointer is like this: we first get the first address of the array by the P pointer, then add the I offset, get the address of the element I in the array, and finally get the specific value by reference. And for P[i] such subscript access, will eventually be converted to the above pointer access method. For array name A, the same as the P method.

It is also important to note that the I offset above is not as simple as I byte, but the total number of bytes of the I element. Therefore, adding an integer to the pointer p and a binary representation of the pointer p with the same integer, the meanings of the two are very different.
Also, just speaking of a is a constant problem, I think of the const keyword. Last time in the modern software engineering course, I heard some students say that the Const modifier is constant. The const modifier is, of course, a variable, except that the variable is read-only, even if the variable has similar properties and constants.

Q3: For array a[3][4], how do I understand more information?

A: We can first know the following information: Array A is an array containing 3 elements, each array element is a one-dimensional array of size 4. Therefore, we usually say that this array has three rows and four columns (but you have to be clear: there is no 3*4 table in memory). Specifically, the two-dimensional array A has the following three elements: a[0],a[1],a[2]; Each element is a one-dimensional array. For a one-dimensional array a[0], it has 4 elements: a[0][0],a[0][1],a[0][2],a[0][3], and these 4 elements are of type int.

The above is the basic structure of this array, which is analyzed from the point of view of the pointer.

For the above array name A, we refer to the pointer pointer (A is a constant). For one-dimensional arrays we know that the array name is the address of the first element in the array. For the above mentioned array a,a is of course the address of the first element of this array. So, what is the first element of array a? By the way, it's a[0]. Then a this pointer stores the address of a[0], i.e. A is equivalent to &a[0]. Then A+i is obvious: A[i] 's address. What do you think of a[i]? It is the first element of array A. What is the first element in array a? It is a one-dimensional array of size 4. Corresponding to the above-mentioned "three rows and four columns", A+i is a pointer to line I. As you can see, I this offset is now in the unit of behavior.

As we have explained above, A+i points to a[i],a[i] is a one-dimensional array. A+i is a pointer to a pointer, so now take out the value of A+i, which is * (A+i). From what we call "pointers to Pointers", * (A+i) is also a pointer, but what does this pointer point to? It points to the address of the first element of the array a[i], which is the address of a[i][0], or &a[i][0]. What is the address of the J element in array A[i]? That is * (A+i) +j, that is &a[i][j]. Of course, you can also write this: A[i]+j.

Also note that the above A+i and * (a+i) memory address is actually the same, but the meaning is completely different. For specific reasons, you can get an answer from the above statement.

Q4: Does the pointer have so much content?

A: Of course not. For more advanced use of pointers, pointers to one-dimensional arrays, pointer arrays, function pointers. The way they are defined may confuse you, but in any case, they are still essentially pointers. It's a lot easier to understand the most basic pointers first and then understand these advanced pointers.

The above-mentioned q1~q3 from a certain point of view, the narrative is too wordy, even sometimes have to be a literal, and must be based on the practice of understanding the above content. My personal suggestion is that the pointer part must be based on the "theory-practice-theory" process of repetition, otherwise-apply now the most fashion words-"Pointer god horse everything is floating clouds ~".

Pointer to a one-dimensional array

Today when I see typedef int (*int_arry) [10]; This statement, because it is less used for such a definition, would like to write a test.c to try. However, when I finished writing a simple test program, I found that I knew little about the use of pointers to one-dimensional arrays.

At first, my program was this:

#include < stdio.h >
typedef int (*int_array) [10];
int main ()
{

int a[10]={1,2,3,4,5};
Int_array i=&a;

printf ("%d=%d\n", i[4],a[4]);
Return 0;11
}

After compiling, you are prompted with the following error:

test.c: In function ‘main’:

test.c:10: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int*’

In other words, i[4] is a int* type of pointer. Why is there such a mistake? Since I is a pointer to an array of 10 shaping elements. So what's wrong with pointing I to array A and then using i[4] to get the fourth element?

Then we analyze from another angle, the general i[4] such form can be regarded as a form of * (I+4). What is I+4? That's right! I is an array pointer, so i+4 is an array pointer. If the array I refers to is considered the 1th row of a two-dimensional table, then i+4 is a pointer to line 5th. That is, it has an offset of 4*sizeof (int) * 10 bytes relative to the position I point to. So * (I+4) is still a pointer, except it points to the first element of line 5th.

It seems that we find the problem, i[4] is not a cosmetic element, but a pointer to the shaping element. In the above program, I originally meant to print the fourth element of array A through I. So now we should revise:

printf ( "%d=%d\n" ,i[0][4],a[4]);

or any of the following sentences:

printf ( "%d=%d\n" ,(*(i+0))[4],a[4]); printf ( "%d=%d\n" ,*(i[0]+4),a[4]); printf ( "%d=%d\n" ,*(*(i+0)+4),a[4]);

You will find that if p is a pointer to a pointer, then it is always possible to get the data that the pointer ultimately points to by two times *, two times [] or once and once [], because in the final analysis [] it will always be the form of *. Understanding these, the above statement will not be a problem for you. Got it?

Pointers and arrays in the C language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.