C/Cpp pointer

2018/08/22 C/CPP

编程规范

A good practice is to initialize pointer as NULL.

strcpy 对指针赋值NULL错误

执行strcpy(orgarray -> visibleFlag, NULL);时,会从NULL(也就是地址0x00000000)处读取数据赋值到orgarray -> visibleFlag,而这个地址是操作系统保护的,读取或写入都会造成段错误。

指针的一个等价写法 int x = 7; int *a = &x; int *b; b = &x;//b 是一个存放地址的指针变量

不同环境下的编译器错误处理方式可能不同, windows与linux, windows如何产生core dump…

指针和前置++后置++的优先级顺序rule

  • Precedence of prefix ++ and * is same. Associativity of both is right to left.
  • Precedence of postfix ++ is higher than both * and prefix ++. Associativity of postfix ++ is left to right.

深入分析

多级指针的分析和测试

这里再来理一下一级指针和二级指针。

type a ;

type *p1 = &a;定义了type 型指针变量p1, 该变量的值是a的地址, 通过p1可以对a的地址进行操作,通过*p1可以通过a的存放地址和类型获取到a的数值,

type **p2 = &p1定义了type 型指针变量p2, 该变量的值是p1自身的地址, 通过p2可以对p1自身的地址进行操作, 通过*p2可以p2指向的地址即p1的指向的地址(即a的地址)进行操作, 通过**p2可以对a的数值进行操作,。此处

测试程序

  printf("sizeof(int)=%u, sizeof(float *)=%u\n", sizeof(int), sizeof(int *));
    int a = 23;
    printf("address of a is %p, value of a is %d\n", &a, a);
    int *p1 = &a;
    printf("address of p1 is %p, value of p1 is %p, value of *p1 is %d\n", &p1, p1, *p1);
    int **p2 = &p1;
    printf("address of p2 is %p, value of p2 is %p, value of *p2 is %p, value of **p2 is %d\n", &p2, p2, *p2, **p2);

result

sizeof(int)=4, sizeof(float *)=4
											  										add of a is 0060FF04, value of a is 23
											   add of p1 is 0060FF08, value of p1 is 0060FF04, value of *p1 is 23
add of p2 is 0060FF0C 	value of p2 is 0060FF08, value of *p2 is 0060FF04, value of **p2 is 23

reference

Readlist:

Search

    Categories Cloud

    Life Linux C/CPP Database Web Benchmarks Software Data Python TCP/IP Financial Stock Bug Golang Rust General Infrastructure TODO Movie Multitenancy Java Ant Algorithm Fastjson Death Build Deploy Education India Aamir Khan Society Female Learning Method OJ Interviewee Interviewer AVL Tree MyBatis Code Reading Design Diary Dating Heap Data Structure Summary Reading Love Claire Mcfall Ferryman Zodiac Astrology Chinese Calculator flink Dubbo docker redis

    Table of Contents