数据结构和c语言

2018-11-13  本文已影响5人  陈走路Aston

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;
}
上一篇下一篇

猜你喜欢

热点阅读