Two-dimensional arrays and two-level pointers (it really doesn't matter)

Source: Internet
Author: User

When I first started to learn C, I always thought that a first-class pointer could be used to access a one-dimensional array, then the two-dimensional array would be accessed with a level two pointer ....

In fact, two-level pointers and two-dimensional arrays really does not matter, and, remember never use a two-level pointer access to two-dimensional arrays ....

Here are a few of the little programs that deepen the impression .....

Lab Environment: Host CPU Core i5,vs2012

Program 1:

int _tmain (int argc, _tchar* argv[]) {    int **p= NULL;     int a[2[3] = {1,2,3,4};     = A;     return 0 ;}

Result: Compile error, Error: cannot convert from "int [2][3]" to "int * *"

Visible, level two pointers and two-dimensional array names are not a class of things at all.

Program 2:

int_tmain (intARGC, _tchar*argv[]) {    int**p=NULL; inta[2][3] = {1,2,3,4}; P= (int**) A; printf ("the length of the pointer is:%d\n",sizeof(p)); printf ("value is:%d\n",(int)(*p)); P++; printf ("value is:%d\n",(int)(*p));    GetChar (); return 0;}

Output Result:

Why is it possible to output the result: purely coincidental !!!!!

What a coincidence: the length of the pointer and the int type are exactly 4 bytes on my machine.

Put the program in the virtual machine to run, the virtual machine system is centos7,64 bit.

#include <stdio.h>#include<stdlib.h>#include<string.h>intMain () {inta[2][2]={1,2,3,4}; int**p = (int**) A; printf ("value is:%d\n",(int)(*p)); P++; printf ("value is:%d\n",(int)(*p)); Char*pointer; printf ("the length of the pointer is:%zd\n",sizeof(pointer)); return 0;}

Result: Compile error, prompt:

The reason is that the pointer in my virtual machine takes up 8 bytes, while int is four bytes.

But Long is 8 bytes, so changing to a long can also achieve the effect on the host.

#include <stdio.h>#include<stdlib.h>#include<string.h>intMain () {Longa[2][2]={1,2,3,4}; Long**p = (Long**) A; printf ("value is:%ld\n",(Long)(*p)); P++; printf ("value is:%ld\n",(Long)(*p)); Char*pointer; printf ("the length of the pointer is:%zd\n",sizeof(pointer)); return 0;}

The result is:

The reason, like the first program, is purely coincidental: both the pointer and the long in my virtual machine account for 8 bytes.

In writing this program there is also a small episode (small error), also designed two knowledge points, by the way share, write the above program.

#include <stdio.h>#include<stdlib.h>#include<string.h>intMain () {inta[2][2]={1,2,3,4}; int**p = (int**) A; printf ("value is:%d\n",(Long)(*p)); P++; printf ("value is:%d\n",(Long)(*p)); Char*pointer; printf ("the length of the pointer is:%zd\n",sizeof(pointer)); return 0;}

The output is:

I accidentally used%d instead of%LD when outputting long data.

Take a look at the effect of%ld:

#include <stdio.h>#include<stdlib.h>#include<string.h>intMain () {inta[2][2]={1,2,3,4}; int**p = (int**) A; printf ("value is:%ld\n",(Long)(*p)); P++; printf ("value is:%ld\n",(Long)(*p)); Char*pointer; printf ("the length of the pointer is:%zd\n",sizeof(pointer)); return 0;}

The output is:

Why is that? Or from a memory point of view:

In my virtual machine, the pointer is 8 bytes, and P is a level two pointer, *p is an int type of pointer, accounting for 8 bytes, so in fact *p occupy A[0] and a[1].

As for why the result is 8589934593. This also involves a byte-order problem (not explained here)

My CPU is Intel, the general x86 series of CPUs are in low-byte order.

So a[0] and A[1] Two units in memory similar to:

High address------------------------------------------------------------------------------------Low address

00000000 00000000 00000000 00000010 00000000 00000000 00000000 00000001

To interpret this memory as a long type of data is 8589934593.

--------------------------------------------------------------------------------------------------------------- ---------------

Also note that I above, those two coincidences can run programs, are used *p access, if using **p will error, because **p is *p point to the value of memory space, and *p point to the memory, that is, 8589934593 address space, is unknown.

Anyway, remember, the level two pointer is not related to the two-dimensional data.

The problem is that since you can't access two-dimensional data with a level two pointer, how do you access a two-dimensional array with pointers?

In my previous essay in the crisis caused by TypeDef and function pointers, it was mentioned that to declare a type of pointer variable, simply declare a variable of that type first, then add * (Note priority)

So how do you access a two-dimensional array with pointers?

Let's recall how to access a one-dimensional array with pointers. When accessing an array, we actually declare a pointer variable of the same type as the array element , point to the address of the first element of the array , and then use that pointer to access the array.

For example, Access int b[3]= {.

The elements in B are of type int, so we declare a pointer variable of type int, such as int *p, and then point P to the address of the first element of B, that is P = &b[0], and the first address of the array is the same as the address of the first element, so you can also use p=b;

It is emphasized here that the type of the pointer and array elements are consistent, not the same as the arrays type, such as the type of B is an array of three elements of type int, while the elements in B are of type int, B is an array type, and the element is of type int, it is not the same. The reason above can be used p=b is not that p is a pointer to an array type, just the first address of the array, and the address of the first element in a group . It is important to remember that P is a pointer to an int type rather than a pointer to an array type ....

Back to the two-dimensional array, take int a[2][3]= {1,2,3,4,5,6}, the element of array A is a data containing three int type data, that is, the element of A is a one-dimensional array, the array contains 3 elements ....

So how do you declare pointer variables that point to elements in a array? In the crisis caused by TypeDef and function pointers, the method declares a variable of that type in time, and then adds *,

The element in a is an array of 3 int data, defined as an ordinary variable: int p[3], and then precede the variable name with a *, but note the precedence of the operator because the priority of the * is lower than [], so parentheses, that is, int (*p) [3].

This then points p to the address of the first element of a: P = &a[0], then you can use p to access the array a !

Give the program:

#include <stdio.h>#include<stdlib.h>#include<string.h>intMain () {inta[2][3] = {1,2,3,4,5,6}; int(*p) [3] = &a[0]; printf ("value is:%d\n", (*P) [0]); printf ("value is:%d\n", (*P) [1]); printf ("value is:%d\n", (*P) [2]); P++; printf ("value is:%d\n", (*P) [0]); printf ("value is:%d\n", (*P) [1]); printf ("value is:%d\n", (*P) [2]);    GetChar (); return 0;}

The result is:

One more program to deepen your understanding

#include <stdio.h>#include<stdlib.h>#include<string.h>intMain () {inta[2][3][4] = {1,2,3,4,5,6,7,8,9,Ten, One, A, -, -, the, -, -, -, +, -, +, A, at, -}; int(*p) [3][4] = &a[0]; inti =0;  for(; I <3; i++)    {        intj =0;  for(; J <4; j + +) printf ("value is:%d\n",(*p) I    [j]); } P++; I=0;  for(; I <3; i++)    {        intj =0;  for(; J <4; j + +) printf ("value is:%d\n",(*2) I    [j]); }    return 0;}

Operation Result:

Finally, put on a picture from CSDN.

Hope to help you, there are flaws in the place also please correct me, thank you ....

Two-dimensional arrays and two-level pointers (it really doesn't matter)

Related Article

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.