Pointer and array relationships

Source: Internet
Author: User

Pointer and array relationships

1. One-dimensional arrays

Int a[5]={1.2.3.4.5};

1)( p=&a[0] and p=a equivalent)

in the The array name in the C language is the first address of the array

2) printf ("%d", a[1]);

printf ("%d", * (a+1)); when we were using A[1] the time,

in fact, the internal computer automatically converts it to * (a+1)

3) int a=[5];

int i,*p=a;

For (i=0;i<5;i++,p++) {

scanf ("%d", p);

}

For (i=0;i<5;i++,p++) {

Printf ("%d", *p);

the contents of the comment cannot be deleted or the pointer variable p will refer to the array

4) above program p++ cannot be replaced with a++

(because p is a pointer variable.) The value of itself can vary,

A Although it is also an address. But he is a constant cannot subtract or self-add to a constant)

2. Two-dimensional array

The first subscript in a two-dimensional array represents the line address

the second subscript represents a column address

Eg: int a[2][3]={1,2,3,4,5,6};

A[0] on behalf of the first line of the first address

A[1] represents the first address of the second row

printf ("%d", * (a[0]+2));

Elements

Line Address

Column Address

A[1][2]

A[1]

A[1]+2

* (a[1]+2)

*(a+1)

*(*(a+1)+2)

* (* (a+1) +2)

3. String pointers

C , there are 2 ways to access the string 1) through a character array access 2) pointer access

4. array of pointers \

1) What is an array of pointers: all data types in an array are pointer types then this is the pointer array

2) The greatest benefit of a pointer array is to handle multi-string

because our string itself is stored in a character array, if more than one string is involved, you must

use a two-dimensional character array.

defines a pointer array

Char *p[]// Here we define an array of pointers;

Eg:char *p[3]={

"Movie"

"Movie1"

"Movie2"}

M

O

V

I

E

/

M

O

V

I

E

1

/

5.void pointer type and NULL type

defining the pointer must be initialized later, otherwise it is dangerous because it does not know where to point

If you don't know where to go, then let it be empty ( NULL)

Eg:int *i;

I=null;

void pointer type

you can point to any type of C data, that is, you can assign a value to a void type with any type of pointer variable , but when the void type pointer is to be converted to another type, The cast must be used

Eg: int i=10;

int *p,*m;

void *q;

p=&i;

Q=p;

m= (int*) q;

printf ("%d", *m);

6. Pointer with multi-level pointer (also called pointer)

int i=10;

int *p=&i;

int **q=&p;

int ***r=&q;

printf ("%d\n", ***r);// value of output i

printf ("%d\n", **r);// The value of the output p

printf ("%d\n", *r);// value of output q

Pointer and array relationships

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.