Two-dimensional arrays and pointers pointing to pointers

Source: Internet
Author: User

A problem raised by a question, first of all know [] the priority is higher than *, the title:

Char **p,a[6][8]; Q. Does p=a cause problems with the program at a later time? Why?

Directly with the program description:

#include <stdio.h>

void Main ()
Char **p,a[6][8];
p = A;
printf ("\ n");

Compile, and then you will find that the error is incorrect 1: "=": "Cannot convert from" char [6][8] "to" char * * * "C2440

So, I looked at "c expert programming" in section 10.5-using a pointer to a function to pass a multidimensional array.

Method One, the function is void fun (int arr[2][3]); This method can only handle arrays of type int of 2 rows and 3 columns.

Method Two, you can omit the length of the first dimension. The function is void fun (int arr[][3]), which, while limiting some slack, can only handle an array of 3 integer lengths per line.

or write this form void fun (int (*arr) [3]); This is an array pointer or a row pointer, arr and * First combine to make ARR a pointer to a pointer with 3

An array of data of type int.

Method Three, create a one-dimensional array, the elements in the array are pointers to other things, also known as Level two pointers. The function is an int fun (int **arr), which dynamically handles data that is not the same length for each column of the row.

Note: You can do this only if you change the two-dimensional array to a pointer array that points to a vector. For example, the following program can output ABC normally:


In the "C Expert Programming" 10.3 section of the small inspiration to speak very thorough: (The following text and contrast must be carefully analyzed!) )

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

Next look at a netizen of a section of the analysis quite to force the code:


Two-dimensional arrays and pointers pointing to pointers

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: 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.