2015.8.5 Personal understanding of pointers

Source: Internet
Author: User

1. To distinguish between the type of pointer and the type that the pointer points to

1.1 type of pointer: Remove the pointer name from the pointer declaration statement, and the remainder is the type of the pointer.

Eg:int *a; remove ' a ' to get "int *", so the pointer type is int *, that pointer is a pointer to an integer variable;

char *str; remove "str" to get "char *", so the pointer type is char *, that pointer is a pointer to a char type variable;

int **ptr; remove "ptr" and get "int * *", so the type of pointer change is "int * *, that pointer is a pointer to an integer pointer variable"

int (*PTR) [3]; remove "ptr", get "int (*) [3]", so the type of pointer change is int (*) [3], that is, the pointer is a pointer to an array of integers.

1.2 The type pointed to by the pointer: Remove the pointer's name and the pointer to the left of the pointer declaration in the statement, leaving the type that the pointer points to.

Eg:int *a, remove ' a ' and ' * ', get ' int ', so the type of the pointer is int type;

char *str; remove "str" and "*" to get "char", so the type of pointing is char type;

int **ptr; remove "ptr" and "*", get "int *", so the type of the pointer is int * TYPE;

int (*PTR) [3]; remove "ptr" and "*", get int [3], so the pointer points to an int [3] type.

2. To understand the different meanings of ' * ' on different occasions

1 void Main () 2 {3int *A; 4 int b=ten; 5 6 a=&b; 7 8 printf ("%d", *a); 9 }

Line 3rd int *a (when defined) ' * ' simply means that the variable defined at this time is a pointer variable

The *a in line 8th, where ' * ' means "point", "*a" means the variable pointed to by pointer variable A,which is B;

3, can not give the wild pointer blind assignment value

eg

1 void swap (int *a,int *b)2{3int *  temp; 4 *temp=*p1; 5 *p1=*p2; 6 *p2=*temp; 7 }

Line 3rd defines a pointer to an integer variable, temp, which is then assigned to *temp on line 4th. This is wrong .

Because *temp is the variable pointed to by the pointer temp, the TEMP does not have a definite value because temp is not assigned, so the memory unit that temp points to is unpredictable. The *temp assignment is to assign a value to an unknown storage unit, but this unknown storage unit may be a unit that holds important data, which can cause system errors.

2015.8.5 Personal understanding of pointers

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.