2019-07-16

2019-07-17  本文已影响0人  多血

sizeof

#include <stdio.h>
#define Size(a) sizeof(a)
struct sdshdr1 {
    int len;
    int alloc;
    char buf[];
};
struct sdshdr2 {
    int len;
    int alloc;
    char *buf;
};

int main()
{
  printf("sdshdr1: %d, sdshdr2: %d!\n", Size(struct sdshdr1), Size(struct sdshdr2));
  return 0;
}

输出:sdshdr1: 8, sdshdr2: 16!

sds结构体的定义

https://www.cnblogs.com/hoohack/p/7824972.html

c指针 负值索引

p[i]是*(p + i)的语法糖,除此之外没有任何意义。
i既可以是正值,也可以是负值

int main()
{
    int num[10] = {0,1,2,3,4,5,6,7,8,9};
    int *p = num+2;
    printf("p[-1] = %d!\n", p[-1]);
    return 0;
}

输出:p[-1] = 1!

sds的指针默认指向的是sdshdr结构体的buf,如果想获取结构体中的len

((struct sdshdr *)((s)-(sizeof(struct sdshdr)))) -> len

上一篇 下一篇

猜你喜欢

热点阅读