数据结构和c语言

1. pointer

int y = 1;   //variable y will  live in the globals section, address is 1,000,000. And values is 1.
int main() {
    int x = 4;  //variable x will  live in the stack section, address is 4,100,000. And values is 4.
    return 0;
}
printf("x is stored at %p\n", &x);  // 「0x3E8FA0」print the address of variable x. This is 4,100,000 in hex (base 16) format. 
int *address_of_x = &x;
printf("x is stored at %p\n", address_of_x); // 0x7ffeeddd0ab8
printf("x is  at %d\n", *address_of_x); // 4
*address_of_x = 99;  // reset value

2. struct:

// 法1. 
struct fish{
  const char *name;
  int age;
};
struct fish snappy = {"Snappy", 78};

// 法2. 「增加了一个typedef」
typedef struct fish{
  const char *name;
  int age;
};
struct fish snappy = {"Snappy", 78};

// 法3. 「定义:给struct增加一个别称FI,声明:FI == struct fish」
typedef struct fish{
  const char *name;
  int age;
}FI;
FI snappy = {"Snappy", 78};

// 使用:
void catalog(struct fish sh){
    printf("%s . His age is %i\n",sh.name ,  sh.age );
}
int main() {
    printf("%d \n", snappy.age);
    catalog(snappy);
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容