On pointers and Arrays in C (v)

Source: Internet
Author: User

Some knowledge of C pointers and arrays is written in the front, but there are some important knowledge not explained, here is a supplement.

First look at the way the normal variable (the pointer is also a variable) and the array name to view the address is different.

To view the address of an array variable, you do not need to use &. in the c,c++ language, the operation of an array variable is the equivalent of a direct action on the address of the variable.

#include <stdio.h>#include<stdlib.h>intMainvoid){    intarr[3]={1,2,3}; intA =4; int*p = &A; printf ("%p\n", &a); printf ("%p\n", &p); printf ("%p\n", arr); printf ("%p\n", p); System ("Pause"); return 0;}

Results of execution:

As you can see, for normal variables (including pointer variables), to see their addresses, add the & symbol, which represents the address of the variable symbol that is printed in its compiler symbol table. Otherwise, the data stored in the memory unit of the corresponding address in the symbol table is printed.

But the name of the array does not have to add & symbols, directly printed is the address in the symbol table.

Two-dimensional arrays

In fact, the C language has no multidimensional arrays, and some are just arrays of arrays.

Pointers and multidimensional arrays

(mainly two-dimensional arrays)

int a[3][4]={1,3,5,7,9,11,13,15,17,19,21,23};

Before saying, one thing, the address of an array is directly written in the compiler's symbol table at compile time, and does not need to be taken as a pointer variable. One of the table entries in the symbol table is a type, and the adjusted amount of the address is determined by this type.

Look at the world from a different angle:

As in the first line, the first line is treated as an element, a special element, and this "special" element is a one-dimensional array. Then the two-dimensional array is a "special" one-dimensional array consisting of three "special" elements.

A is the name of this "special" one-dimensional array, the first address, the address of the first element, that is, the first address, which refers to the first line of an entire row, not a specific element. So what we call " line pointers ". Similarly: a+0,a+1,a+2, are the line pointers.

Conclusion:

Representation

Meaning

Pointer type

a or a+0

point to Section 0 rows

Row pointers

A+1

point to Section 1 rows

Row pointers

A+2

point to Section 2 rows

Row pointers

Next, let's zoom in to see the first line, with the first line of elements: a[0][0],a[0][1],a[0][2],a[0][3]. As a separate one-dimensional array, a[0] is the name of the array, the first address of the array, the address of the first element, that is, a[0]+0. A[0] and a[0]+0 refer to specific elements, then we call them "column pointers".

Conclusion: (No. 0 rows as one-dimensional arrays)

Representation

Meaning

Pointer type

A[0]

is the name of a one-dimensional array, its first address, and the address of the 1th element ( a[0]+0)

Column pointers

A[0]+1

Section 0 lines, address of the first 2 elements

Column pointers

A[0]+2

Section 0 lines, address of the first 3 elements

Column pointers

Two important concepts: row pointers and column pointers.

Row pointer: Refers to an entire line, not to a specific element.

Column pointer: Refers to a specific element in a row.

You can interpret a column pointer as a concrete element of a row pointer, and the row pointer is interpreted as the address of a column pointer.

Then the specific conversion between the two concepts is:

* Row Pointers ---- column pointer (modifies the type in the symbol table, modifies the size of the memory space pointed to)

& Column Pointers ---- Row Pointers

From the above, a pointer variable (for the moment the array name as a pointer, in fact not) consists of two aspects: the address to which the pointer is typed.

Based on the above conversion formula :

Row pointers

Convert to: Column pointer

Column pointer equivalent representation

Content

Content equivalent representation

Meaning

a or a+0

*a

a[0]

*a[0]

* (*a)

a[0][0]

a+1

* (a+1)

a[1]

*a[1]

* (* (a+1))

a[1][0]

a+2

* (a+2)

a[2]

*a[2]

* (* (a+2))

a[2][0]

for Element A[1][2], its address is represented by a column pointer as a[1]+2 , the equivalent is expressed as * (a+1) +2 , then the content is * (* (a+1) +2) ;

lang= "en-us" xml:lang= "en-us" >&a[0]

lang= "en-us" xml:lang= "en-us" >&a[1]

TD valign= "Top" width= " TD valign= "Top" width= "

Column pointers

Row pointers

Equivalent representation

Meaning

a[0]

&a or & (a+0)

0 Line

a[1]

& (a+1)

1 Line

A[2]

&A[2]

& (A+2)

Section 2 Line

Example 1 : Outputs a two-dimensional array with column pointers.

#include <stdio.h>voidMain () {inta[3][4]={1,3,5,7,9, One, -, the, -, +, +, at}; int*p= a[0];//method of defining column pointers    for(; p < a[0] + A; p++) {printf ("%d",*p); }     return;}

Example 2 : Outputs the entire two-dimensional array with the row pointer.

#include <stdio.h>voidMain () {inta[3][4]={1,3,5,7,9, One, -, the, -, +, +, at}; int(*p) [4]= &a[0];//The row pointer definition method or INT (*p) [4]= A;   intI, J;  for(i =0; I <3; i++)      for(j =0; J <4; J + +) {printf ("%d", * (* (p + i) +j)); }     return;}

Http://www.cnblogs.com/kira2will/p/3590748.html

On pointers and Arrays in C (v)

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.