An example to understand the pointer point of a two-dimensional array of C language _c language

Source: Internet
Author: User
Tags data structures

The concept of an array has not been thoroughly understood, and it is only thought that it is a constant pointer, similar in usage to a basic pointer. So when I try to access a two-dimensional array with a level two pointer, it often goes wrong. Here is the first error program to write:

#include <stdio.h>

int main ()
{
    int iarray[2][3] =    {{1,2,3},{4,5,6}};
    int **parray = NULL;

    Parray = IArray;
    
    printf ("array[0][0] =%d\n", parray[0][0]);
    printf ("array[1][2] =%d\n", parray[1][2]);
        
    return 0;
}

At the beginning I was like this: the array and the pointer is similar, one-dimensional arrays and one-dimensional pointers, then two-dimensional array names should be similar to two-dimensional pointers, so the above program is not wrong, should print out is 1 and 6. But when I actually compile and run, there is a section of error, that is, I visited the address space should not be accessed. Where did that mistake go? How should the correct procedure be written?
     to solve the problem, I had to understand the meaning of the array again. After poring over some books, I found that the array was not as simple as I had imagined: a set of variables identified by a constant pointer. Arrays should also be considered as a complete variable type: a name, a size, and an address. Not much is the name and its address. It is also precisely because the array has size, so when using sizeof array names, the actual array size is calculated, not the size of the pointer.
     also because of this, pointers to arrays and pointers to pointers are also very different. The most obvious difference between the two is that they are shown in the pointer step. We know that when the pointer is over the + + operation, the actual address that spans depends on the type of data that the pointer points to: for a typical 32-bit machine, if you are pointing to an int, the actual address that spans is 4, pointing to the pointer type of data, and the actual address that spans is 4, when you point to an array type, The actual address that spans is the length of the array.
     now back to analyze the error program above, according to the subscript reference symbol [] operation rules, we know parray[0][0] is actually **parray, and IArray is actually just an array variable name, And its value is the beginning address of the entire array (in fact &iarray,iarray,iarray[0] and the value of the &iarray is the beginning address of the array, are the values given by the compiler during compilation). So in fact, *parray is already iarray[0][0] value, that is, 1, and **parray is to access the address of the address space in the data, naturally, a section of error.
     In fact, using a pointer to access a two-dimensional array can be directly with a single level of pointers. For example, the following program:

int main ()
{
    int iarray[2][3] =    {{1,2,3},{4,5,6}};
    int *parray = NULL;

    Parray = IArray;
    
    printf ("array[0][0] =%d\n", *parray);
    printf ("array[1][2] =%d\n", * (Parray + 1 * 3 + 2));
        
    return 0;
}

Because the array itself is contiguous in the address space, based on the number of rows and columns, we compute the address offset of the access cell by ourselves, and we can easily traverse all the data in the two-dimensional array with a first-level pointer.
We can also try to access the members of a two-dimensional array with pointers to the array. Here is the case program:

int main ()
{
    int iarray[2][3] =    {{1,2,3},{4,5,6}};
    int (*parray) [3] = NULL;

    Parray = IArray;
    
    printf ("array[0][0] =%d\n", parray[0][0]);
    printf ("array[1][2] =%d\n", parray[1][2]);
        
    return 0;
}

A simple analysis of this program: we know [] the binding direction of the operator is left to right, parray[1][2] is equivalent to (* (Parray + 1)) [2], and since Parray is an array pointer and the length of the array is 3, so * (Parray + 1) Represents iarray[1] This array, then parray[1][2] is completely equivalent to iarray[1][2].
If you have to use a level two pointer to access a two-dimensional array, we also have to borrow the pointer array (the data stored in the array is a pointer type), and the following is the case program:

int main ()
{
    int iarray[2][3] =    {{1,2,3},{4,5,6}};
    int *iparray[2] = {iarray[0], iarray[1]};
    int **parray = NULL;

    Parray = Iparray;
    
    printf ("array[0][0] =%d\n", parray[0][0]);
    printf ("array[1][2] =%d\n", parray[1][2]);
        
    return 0;
}

Because the two-level pointer jumps two times, extra space is required to store the first-level pointers in the middle. Therefore, it is generally not recommended to use two-level pointers to access two-dimensional arrays.

It is well known that the pointer is essentially an address! The address of a variable is called a "pointer" to this variable. If there is such a variable: its storage unit is stored in the address of other variables! We call it "pointer variable." (Notice the difference between the two: two completely different concepts!)
We all know that array names and function names are their entry addresses. Similarly, a variable name is actually the address of this variable! The C language has an operator of "&": the address operator. Because array names and function names themselves represent addresses, they are usually not and cannot be invoked or otherwise manipulated (in fact, the direct reference to the function name is equivalent to the address to which it is being fetched). This is why they are called "Constants"! But for a variable, things are different. To get its address, you must perform a "&" operation, even though it represents an address value! The direct reference to a variable is the data content of the memory unit in which it resides! "Pointer variable" as a variable is certainly not an exception! But the difference between it and other ordinary variables is that its contents are other variables (including "pointer variable") address, on the WIN32, its size constant 32 bits, 4BYTE. Ordinary variables are not limited in size! The data content of the address to which the pointer variable is directed is obtained by the operator "*". In understanding we see the "Pick operator *" As part of a type, and the data type is a variable address type (both for each "*")!
As long as you understand the above common sense, "pointers" will no longer be the "stumbling block" in the program design!
from the perspective of memory mappings, the rule array for C (excluding multidimensional arrays designed through data structures) does not exist multidimensional, that is, all arrays are essentially one-dimensional, and the first-level pointers are equivalent to one-dimensional arrays! The key difference is the semantic differences between multidimensional arrays and one-dimensional arrays! And we understand that multidimensional arrays usually describe them graphically as "Matrix". A more precise understanding is that each element of a multidimensional array is an array, so recursively until the end each element is a simple variable type, resulting in a special one-dimensional array!

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.