/**   * struct和typedef struct 的使用方法 以及struct成员长度限制问题  **/ #include <stdio.h> #include <stdlib.h> #include <string.h>  #define MAX_PHONE 11 #define MAX_EMAIL 60  struct stud {     char * phone;     char email[MAX_EMAIL];     int age;     char * line; };  typedef struct string {     int length;     char data;     char pc[0];     int size; } Str;  //于是在声明变量的时候就可:Str s2;(如果没有typedef就必须用struct string s1来声明) //也是耶鲁cs课程的写法:http://www.cs.yale.edu/homes/aspnes/pinewiki/C(2f)Structs.html struct string2 {     int length;     char * data; };  //不加 struct 名字,这里的Str3相当于别名 只能用 Str3 xxx 声明 typedef struct{     int length; }Str3;  //可以直接Str4.data 来用 struct string4 {     int length;     char * data; }Str4;  int main(int argc, char const *argv[]) {      struct stud s0 = {         "批量赋值",         "[email protected]",         25,         "#######################",     };      printf("%s %d/n %s /n", s0.phone,s0.age,s0.line);      Str s1;     s1.length = 123;     printf("%d/n", s1.length);      //如果没有typedef就必须用struct string s1来声明     struct string2 s2;     s2.data = "如果没有typedef就必须用struct string s1来声明";     puts(s2.data);      Str3 s3;     s3.length = 321;     printf("%d/n", s3.length);      Str4.data = "可以直接Str4.data 来用";     puts(Str4.data);      int phone_length = strlen(s0.phone);      if (phone_length > MAX_PHONE)     {         printf("%s /n %d", s0.phone,phone_length);     }      printf("%d /n", s2.length);     printf("%s /n %d /n", s0.phone,phone_length);      return 0;  } 

发表评论

电子邮件地址不会被公开。 必填项已用*标注