How to use pointers to reference elements of a two dimensional array

Updated on educate 2024-03-27
8 answers
  1. Anonymous users2024-02-07

    The two-dimensional array name a is a pointer to a pointer. It is also a pointer array name, which contains 3 elements, a[0]a[1], a[2]. Although the address stored by a is the address of the first element of the array, it does not point to a variable, but to a pointer.

    It should be a pointer to a pointer, which points to the same pointer as the address you store, i.e. a[0], and a[0] is the pointer to the first row of a one-dimensional array, which can point to a specific variable.

    Now you see, *(a+0) is not a variable, she is a pointer, a points to a[0], *a+0) is a[0].

  2. Anonymous users2024-02-06

    One- and two-dimensional arrays and second-level pointers.

    Two-dimensional arrays. A two-dimensional array is essentially an array with an array as an array element, i.e., an "array of arrays". A two-dimensional array is equivalent to a first-order array consisting of several one-dimensional arrays.

    int a[3][4];

    A is a two-dimensional array, and a[0], a[1], and a[2] all represent a one-dimensional array, and the three elements of them together form a one-dimensional array a (the element is a one-dimensional array). In other words, a two-dimensional array is a one-dimensional array composed of several one-dimensional array elements (a[0], a[1], a[2]).

    int a[3][4]=,,}

    Whether it's a one-dimensional array or a two-dimensional array, the array name represents the first address of the array.

    2.Secondary pointer.

    A pointer to a pointer is called a secondary pointer, i.e., a secondary pointer does not point to a normal variable, but to a pointer. For example, int **p, *p does not represent the value of one variable, but another pointer.

    Second- and second-level pointers point to two-dimensional arrays.

    int a[3][4];

    In this case, a is a second-level pointer, *a points to the first address of the two-dimensional array (which is also the first address of a[0]), *a+1) points to the first address of a[1], and *(a+2) points to the first address of a[2]. *(a+0) +1) for element a[0][1], *a+i)+j) for element a[i][j].

    3. Application examples.

    #include

    void main()

    for(int i=0; i<3; i++)

    printf("");}

    Fourth, run the test.

  3. Anonymous users2024-02-05

    A method to access any element of a two-dimensional array using a pointer variable:

    1.Use Column Pointer: Define a column pointer p so that it points to the 0th element of the 2D array.

    int a[3][4];

    int *p;

    p=&a[0][0];

    Since a[0] is the name of the array on line 0, p=&a[0][0] is equivalent to p=a[0], because a[i][j] is preceded by i*4+j elements.

    Any row of the two-dimensional array, column j elements, can be represented as *(p+i*4+j).

    2.Use a line pointer: define a line pointer p so that it points to line 0 int a[3][4] of a two-dimensional array;

    int (*p)[4];

    p=a;It can also be p=&a[0];

    where * (p+i)+j) represents any element of row j and column j.

  4. Anonymous users2024-02-04

    In C, a pointer to a two-dimensional array of characters can be defined in the following way: char c[4][5]; Define a version of a 2D array of character weights with 4 rows and 5 columns.

    char (*pc)[5];Define an array pointer with 5 elements: pc=c; Point the array pointer pc to the two-dimensional character array c Note: char (*pc)[5] cannot be written as char *pc[5]char (*pc)[5] indicates the array pointer, and each element in the array is char;

    char *pc[5] represents an array of pointers, and each element in the array is char*, i.e. each element is a pointer.

  5. Anonymous users2024-02-03

    1. Define the two-dimensional pointer BAI

    An array is pretty much the same as a du defines an array of one-dimensional pointers, zhi

    It's just that the dimension of the matrix DAO adds one dimension back.

    Already. 2. The following is an example of an answer body to illustrate how to define a two-dimensional array:

    int *p[2][3];Defining a two-dimensional array is just a definition, not an allocation of address space.

    int i,j;The number of rows and columns of the array.

    The following two for loops are used to initialize the two-dimensional pointer array, i.e., to assign an address. If you do not initialize, the pointer becomes a wild pointer (that is, pointing to unknown).

    for(i=0; i<2; i++)

    for(j=0; j<3; j++)

    p[i][j] = (int *)malloc(sizeof(int));

    p[0][1] = 2;Assign a value to the memory unit that the pointer points to in the pointer array.

    printf("%d", *p[0][1]);Output the result.

  6. Anonymous users2024-02-02

    Define a two-dimensional array of characters: char str[3][10];

    Define a pointer array : char *p[5] = ;

  7. Anonymous users2024-02-01

    As defined: int a[3][4];

    int *p; p=a[0];In this case, p points to 0 rows and 0 column elements.

  8. Anonymous users2024-01-31

    There are two basic ways to pass a two-dimensional array to a pointer as a function argument in the C language

    1. Pass the "array pointer".

    include void output( int (*pa)[3], int n ) This function can only output a two-dimensional array of n rows and 3 columns}void main(),int i; int *pa[2]; for( i=0;i<2;i++ pa[i]=a[i]; output(pa, 2, 3);}

Related questions
6 answers2024-03-27

If you want to learn systematically, you can consider signing up for a live online class, and recommend CGWANG's online class. The teacher speaks carefully, you can watch it back after the class, and there are also the same type of recorded classes that you can learn for free (give away lifelong VIP). >>>More

5 answers2024-03-27

What is on a plane is two-dimensional. Two-dimensional, that is, left and right, up and down, there is no front and back. >>>More

5 answers2024-03-27

2-dimensional bar code *** (2-dimensional bar code) is a black and white graphic distributed in a plane (two-dimensional direction) with a specific geometric figure according to a certain law to record data symbol information; In the first compilation, the concept of "0" and "1" bit streams that form the basis of the internal logic of the computer is cleverly used, and a number of geometric shapes corresponding to the binary are used to represent the numerical information of the text, and the information is automatically read by the image input device or the photoelectric scanning device to realize the automatic processing of information: it has some commonalities of barcode technology: each symbol system has its specific character set; Each character occupies a certain width; It has a certain verification function, etc. >>>More

5 answers2024-03-27

The three-dimensional is good, and beoss3 has a three-dimensional feeling. >>>More

6 answers2024-03-27

Four-dimensional. Fourth dimension. Four-dimensional space. >>>More