Two-level pointers and two-dimensional arrays

Source: Internet
Author: User

A function of the form:

void f (float **p) {

/* To access in the function body as a two-dimensional array */

P[1][1] = 0;//c++ compile OK with VC, run error (illegal access)

}

float **p; In fact, p here is not a pointer to a two-bit array, but a pointer to a pointer

Visiting like you must be a problem.

For example:

Float a[2][2]={0,1,2,3};

Float **p= (float**) a;//forces two-dimensional array pointers to pointers to pointers

Then at this point

p[0]=0;

P[1]=1;

p[2]=2;

p[3]=3;

and

p[0][0]=* (* (p+0) +0) =**p;

p[0][1]=* (* (p+0) +1);

For p[0][0]: because of *p=0; ====> **p=* (0); Referring to memory with zero address is bound to be wrong.

For p[0][1]=* (*p+1) ====>* (4), illegal memory is referenced

Similarly, for p[1][0]=* (1), p[1][1]=* (5), all references to illegal memory

Therefore, a two-bit array cannot be simply converted to a pointer pointer.

The correct pointer to a two-dimensional array should be:

float A[5][10];

Float (*p) [10];//only need to be defined as a pointer to the second dimension, ignoring the first dimension

P=a;

P[0][1]=A[0][1];

Two-level pointers and two-dimensional arrays are not equivalent.

A secondary pointer is a pointer to a pointer

A two-dimensional array is actually a pointer, Char a[3][4]; A is the first address that points to the entire two-dimensional array. It is equivalent to (char *) [n], not char * *;

Therefore cannot direct: t=a;

To do this: t = (char * *) A;

We know that char array[]= "abcdef"; Array is the first address of the arrays,

Well, in a two-dimensional array, the array is, of course, the first address.

Look at this definition char array[][3] ={"AB", "CD", "EF"};

How do you know that? Define such an array in the VC debug window

We see:

Array---------0x64324234

|------Array[0]---0x64324234 "AB"

|------ARRAY[1]---0x64324337 "CD"

|------ARRAY[2]---0x6432433a "EF"

It is clear that the actual compiler is the implementation of the two-dimensional array, in fact, the array is "one-dimensional pointer array" the first address, where each element pointer corresponds to a string, then we can see if this is possible to use the array of two-dimensional arrays.

char **parray = array; The compiler prompts an error, what do I do? Add a (char * *) to try, still error, set to see if the value of Parray is equal to the value of array, but can we use the same output string as array? obviously not. , the compiler does not process parray+i into parray+i*3 to find the address of the I pointer, but simply adds an i. This means that the compiler simply assigns the address value to the Parray, and it doesn't actually make any sense. We can't use it to access any data. Is it strange?

Let's take a look at this. Define char *p[] = {"AB", "CD", "EF"}; A pointer array is defined. Char **sp = p; This usage often sees why you can use the SP to access the string. It is true that the compiler identified the SP as a pointer to a one-dimensional array when it was compiled, so we can manipulate the entire array by doing it as an array name.

Two-level pointers and two-dimensional arrays

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.