Learning point C language (20): Data Type-pointer

Source: Internet
Author: User
1. Get the variable address through & variable:
 
# Include <stdio. h> int main (void) {int num = 10; printf ("variable value: % d \ n", num); printf ("variable address: % d \ n ", & num); getchar (); Return 0 ;}

  

2. The variable indicating the variable address is a pointer:

 
# Include <stdio. h> int main (void) {int num = 10; int * P = & num; printf ("% d, % P \ n", num, P ); getchar (); Return 0 ;}

  

3. * The pointer is the same as the variable itself:

 
# Include <stdio. h> int main (void) {int num = 10; int * P = & num; printf ("% d, % P, % d \ n", num, P, * P); * P = 11; printf ("% d, % P, % d \ n", num, P, * P); (* P) ++; printf ("% d, % P, % d \ n", num, P, * P); num = 99; printf ("% d, % P, % d \ n ", num, P, * P); getchar (); Return 0 ;}

  

4. Pay attention to initialization when declaring the pointer. It is dangerous to have no initialization pointer:

 
# Include <stdio. h> int main (void) {int n1 = 11; int n2 = 22; int * P = NULL;/* Initialization is blank */P = & N1; printf ("% d, % P, % d \ n", N1, P, * P); P = & N2; printf ("% d, % P, % d \ n ", N2, P, * P); getchar (); Return 0 ;}

  

5. Why is it dangerous to have no initialization pointer:

 
# Include <stdio. h> int main (void) {int * P; // The above pointer P is not initialized, but it also has a spam address printf ("% P \ n ", p); // who knows what will be covered if a value is assigned to it at this time? // * P = 100;/* do not execute this */getchar (); Return 0 ;}

  

6. pointer to a constant: the pointer cannot be used to modify the value it points to, but the value can be modified through its variable.

# Include <stdio. h> int main (void) {int n1 = 111; int n2 = 222; const int * P = & N1;/* Note the use of const */printf ("% d, % P, % d \ n ", N1, P, * P); n1 = 333; // * P = 333;/* cannot, because the current pointer is a constant */printf ("% d, % P, % d \ n", N1, P, * P); P = & N2; /* You can change the pointer to */printf ("% d, % P, % d \ n", N2, P, * P); getchar (); Return 0 ;}

  

7. Constant pointer: Lock pointer pointing

 
# Include <stdio. h> int main (void) {int n1 = 111; int n2 = 222; int * const P = & N1;/* Note the use of const */printf ("% d, % P, % d \ n ", N1, P, * P); n1 = 333; // * P = 333;/* cannot, because the current pointer is a constant */printf ("% d, % P, % d \ n", N1, P, * P); // P = & N2; /* you cannot change the pointer to * // printf ("% d, % P, % d \ n", N2, P, * P ); getchar (); Return 0 ;}

  

8. Pointers are of the following type:

# Include <stdio. h> int main (void) {long n = 100l; float F = 1.5f; double D = 3.14159265; long * P1 = & N; float * P2 = & F; double * P3 = & D; printf ("% LD \ n", * P1); printf ("% G \ n", * P2); printf ("%. 8f \ n ", * P3); getchar (); Return 0 ;}

  

9. Confusing pointer definitions:

What should I do:
Int * P;
Int * P;
Int * P;

This is correct because the C language ignores the blank space, but the following example shows which one is better:

 
# Include <stdio. h> int main (void) {int N1, N2, * P;/* defines two integers (N1, N2) and an integer pointer (P) */n1 = 111; N2 = 222; P = & N1; printf ("% d, % P \ n", * p, p); P = & N2; printf ("% d, % P \ n", * p, p); getchar (); Return 0 ;}

  

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.