06 pointer entry

1, simply put, pointer indicates an address is the address

. 1 #include <stdio.h>
 2  
. 3  void main () {
 . 4      int NUM = . 1 ;
 . 5      the printf ( " NUM value =% d num address P% = \ n- " , NUM, & NUM);
 . 6      // 1, if the output of a variable address, using the format% P
 . 7      // 2, & num represents taken num variable corresponding to the address 
. 8  
. 9  
10      int * PTR = & num; // define a pointer, the pointer 
. 11      the printf ( " ptr address is the value of% p ptr is stored in an address of address% p ptr storage pointed to by value (ptr point value)% D \ n- " , & ptr, ptr, * ptr);
 12 is      // . 1, int * indicates the type is a pointer type
 13     // 2, the name ptr, ptr int * is a type
 14      // . 3, PTR points to a variable of type int (address)
 15      // 4, the pointer variable, the address itself PTR &
 16      // . 5, get a pointer to the variable storing address PTR
 . 17      // . 6, obtaining the value of pointer: * PTR 
18 is  
. 19 }

 

 Memory layout diagram:

 

 2, Exercise 1

  Writing a program, obtain a variable num int address, and displayed on the terminal address to the pointer PTR num, and to modify the value of the num by PTR, and memory layout shown in FIG.

. 1 #include <stdio.h>
 2  void main () {
 . 3      int num = 100 ;
 . 4      the printf ( " The value of num is the address of% d num P% \ n- " , num, & num);
 . 5  
. 6      int * = & PTR num;
 . 7      * PTR = 200 is ;
 . 8      the printf ( " the value of num is the address of% d num P% \ n- " , num, & num);
 . 9 }

 

   Memory layout diagram

    

3, Exercise 2

  Determine whether the program correctly

  

 

 4, Exercise 3

  What output

 1 #include<stdio.h>
 2 
 3 void main() {
 4     int a = 300;
 5     int b = 400;
 6     int* ptr = &a; //ptr 指向a
 7     *ptr = 100; //a=100
 8     ptr = &b;  //ptr指向b
 9     *ptr = 200; //b=200
10     printf("a=%d,b=%d,*ptr=%d", a, b, *ptr);
11 }

 

 5,指针细节

  ①基本类型,都有对应的指针类型,形式为 数据类型*  ,比如 int的对应的指针就是 int* , float 对应的指针类型就是 float* ,以此类推

  ②此外还有指向数组的指针,指向结构体的指针,指向共用体的指针,二级指针,多级指针等。

  

  

  

 

Guess you like

Origin www.cnblogs.com/shanlu0000/p/12337603.html