Analysis between the relationship between the secondary pointer and the two-dimensional array

xiaoxiao2021-03-06  24

Analysis between the relationship between the secondary pointer and the two-dimensional array

Today, I saw someone asked someone asking the pointer, I would like to explain a few common mistakes through such an example and deepen the second.

The relationship between level pointers and two-dimensional arrays.

We know Char array [] = "abcdef"; array is the first address of the array,

So Array is of course the first address of the array in the two-dimensional array.

Take a look at this definition char array [] [3] = {"ab", "cd", "ef"};

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

We saw:

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

| ------ array [0] --- 0x64324234 "ab"

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

| ------ Array [2] --- 0x6432433a "ef"

I have already understood that the actual compiler is such a two-dimensional array. In fact, Array is the first address of the "one-dimensional pointer array", each element pointer

Corresponding to a string, let's take a look at whether it can be used to use the Array two-dimensional array.

Char ** parray = array; Compiler prompts an error, what should I do? Add a (char **) try, still mistake, set up the value of Parray and Array

The value is equal, but can we do the same output string like using Array [i]? It is obvious that the compiler will not put

PARRAY i is processed into PARRAY I * 3 to find the address of the i-th pointer, but simply adds an i. This indicates that the compiler has only done the address value to

Parray, and it doesn't have any meaning. We can't use it to access any data. Is it very strange?

Let's take a look at this definition of char * p [] = {"ab", "cd", "EF"}; define a pointer array.Char ** sp = p; this kind of usage often saw, why

You can use SP [i] to access the string. It is indeed that the compiler identifies the SP is a pointer to the one-dimensional array time when compiling.

Pointer, then we can do it as a group name to manipulate the entire array, C magic place or essential place is here, hope

This article can help those friends who have doubts about pointers or secondary pointers, this is the first article in my blog, huh, huh.

转载请注明原文地址:https://www.9cbs.com/read-65483.html

New Post(0)