Two-dimensional arrays and double pointers (RPM)

Source: Internet
Author: User

Original address: 46565577

I. INTRODUCTION 1, two-dimensional array

int array[10][10];
function declaration: void fun (int a[][10])
Function call: Fun (array);
Access: General use of a[i][j] to access elements in an array

2. Array of pointers

int *array[10];
function declaration: void fun (int *a[10]);
Function call: Fun (array);
Access: Use * (A[I]+J) to access elements in an array

3. Pointer to pointer

int **array;
function declaration: void Fun (int **a);
Function call: Fun (array);
Access: * (* (a+i) + j) or A[i][j]? Access elements (access methods for two-dimensional arrays represented by double pointers)

Second, analysis of examples?

The following is a related topic:

  1. #Define ROW 2
  2. #define COL 3
  3. void myputs(char **pos);
  4. int main()
  5. {
  6. Char **p;
  7. Char a[row][col]={"abc", "def"};
  8. p = A;
  9. Myputs (P);
  10. return 0;
  11. }
  12. void myputs(char **p)
  13. {
  14. int I, J;
  15. For (i = 0; i < ROW; i++)
  16. {
  17. For (j = 0; j < COL; J + +)
  18. printf ("%c", P[i][j]);
  19. printf ("\ n");
  20. }
  21. }

At first glance, this topic will print out characters. But in fact, this program will run with a segment error.

Cause: When a two-dimensional array is passed as a double pointer, the array's head pointer is passed to the function, and its internal structure is not clear whether it will be properly accessed.

Correct:

Method one: Change void myputs (char **p) to void Myputs (char p[row][col])

Method Two: Change char A[row][col] to char *a[col]

The first method is well understood and certainly feasible. Let's take a look at method two.

In "c Expert programming", the issue of the parameter transfer process is described, the following is related to our topic content

How are array and pointer parameters modified by the compiler?

The "array name is rewritten as a pointer parameter" rule is not recursively defined. Arrays of arrays are rewritten as "pointers to arrays" instead of "pointer pointers":

Parameters that are matched by an argument

Array of array char c[8][10];           CHAR (*) [10]; Array pointers

Pointer array char *c[10];               Char **c; Pointer to pointers

Array pointer (row pointer) char (*C) [10];          char (*C) [10]; does not change

Pointer to the pointer char **c;               Char **c; does not change

From the above, the parameters for char **c can be matched by Char **c and Char *c[10].

Why Char **c can match char *c[10], can't match char c[10][10]? This is related to the memory distribution of the two.

1, two-dimensional array of memory distribution:

Once a two-dimensional array is defined, a logically contiguous block of memory is allocated in memory. Char c[10][10], the system allocates a piece of contiguous memory of 100 bytes. In other words, such a two-dimensional array with a one-dimensional array char c[100] has a similar memory distribution. Below we use the program to verify:

  1. #Define ROW 2
  2. #define COL 3
  3. Union u
  4. {
  5. Char A[row][col];
  6. Char B[row*col];
  7. }u;
  8. int main()
  9. {
  10. int I, J;
  11. char ch = ' a ';
  12. For (i = 0; i < ROW; i++)
  13. For (j = 0; j < COL; J + +)
  14. {
  15. U.A[I][J] = ch;
  16. ch++;
  17. }
  18. For (i = 0; i < ROW; i++)
  19. {
  20. For (j = 0; j < COL; J + +)
  21. printf ("%c-%c\t", u.a[i][j], U.b[i*col + j]);
  22. printf ("\ n");
  23. }
  24. return 0;
  25. }

It can be seen that we can access a two-dimensional array in a way that accesses an array.

The specific memory distribution is as follows:

This is why int a[5][] is not allowed and int a[][4] is allowed.

2, two-dimensional pointer memory distribution:

Use two-dimensional pointers to represent the general dynamic allocation of two-dimensional arrays:

  1. Char **a;
  2. int I, J;
  3. char ch = ' a ';
  4. A = (char * *)malloc (sizeof ( char *) * ROW)
  5. ASSERT (a);
  6. For (i = 0; i < ROW; i++)
  7. {
  8. A[i] = (char *)malloc (sizeof (char) *col);
  9. ASSERT (A[i]);
  10. }
  11. For (i = 0; i < ROW; i++)
  12. For (j = 0; j < COL; J + +)
  13. {
  14. A[I][J] = ch;
  15. ch++;
  16. }

As you can see, the memory distributions of the two-dimensional arrays and the double pointers are completely different, so they cannot be passed to each other, and if passed, a segment error occurs. But Char *a[3] can be because its memory distribution is the same as char**a.

Third, summary char **p and Char p[2][3] cannot pass arguments to each other because their specific memory distribution is different, so there is a segment error at run time. One more thing to note: A[i][j in a two-dimensional array] and a[i][j in a double pointer) mean something different. In a two-dimensional array of int a[10][10], a[i][j] refers to the number of elements in the first row of column J. In the double pointer int **a, A[I][J] refers to the first J element in the address to which the int * pointer is placed. i.e. * (* (a+i) +j).

Two-dimensional arrays and double pointers (RPM)

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.