Array pointers and pointers to pointers

Source: Internet
Author: User

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

Char **p,a[6][8]; Ask P=a if the program will cause problems in the future. Why.

Direct use of program Description:

#include <stdio.h>

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

Compile, and then you'll find that pass. Error 1 C2440: "=": Cannot convert from "char [6][8]" to "char * *"

So I looked at section 10.5, "c expert programming"-passing a multidimensional array to a function using pointers.

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

Method Two, you can omit the length of the first dimension. The function is void fun (int arr[][3]), which is a bit looser, but can only handle an array of 3 integer lengths per row.

Or written in 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 3

An array of data of type int.

Method Three, create a one-dimensional array in which the elements in the array are pointers to other things, or level two pointers. The function is an int fun (int **arr), which can dynamically process data of different lengths in each row.

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

#include <iostream> 
using namespace std; 
void Test (char **ptr) 
{ 
    cout << *ptr << endl; 
} 
 
int main () 
{ 
    char *p[3] = {"abc", "Def", "Ghi"}; 
    Test (p); 
    return 0; 
}

In the "C Expert Programming" 10.3 section of the small inspiration said very thoroughly: (The following text and contrast must be carefully analyzed.) )

How the array and pointer parameters are modified by the compiler.

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

The formal parameter that the argument matches

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

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

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 user of a section of the analysis of considerable power code:

 #include" stdafx.h "#include <iostream> using namespace std; 
    int _tmain (int argc, _tchar* argv[]) {int arr1[3]; 
    int arr2[3]; 
    int arr3[3]; 
    int * PTR; 
    PTR1 is a pointer to int [3], that is, the type of PTR and the type of &ARR1 is the same, note: arr1 points to the memory area fixed long int ptr1[3][3]={{1,2,3},{1,2,3},{1,2,3}}; 
    PTR2 is a pointer to int *, that is, the type of PTR2 is the same as &ptr, note: The memory area that PTR points to is indefinite int * PTR2[3]={ARR1,ARR2,ARR3}; 
    PTR3 is a pointer to int [3], that is, the type of PTR3 and the type of &ARR1 is the same, note: arr1 point to the memory area fixed long int (* PTR3) [3]=&arr1; PTR3=PTR1; Yes, they are the same type//ptr3=ptr2;//error cannot be converted from "int *[3]" to "Int (*) [3]//PTR4 is a pointer to an int *, that is, the PTR4 type and the &ptr are the same, note: PTR points to the inner 
    Storage area Indefinite Length int * * PTR4; ptr4=&arr1; Error cannot be converted from "Int (*) [3]" to "int * * PTR4=PTR2; Yes, they are of the same type//PTR4=PTR3; 
Error cannot be converted from "Int (*) [3]" to "int * * * 0; }

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.