struct tag {
member-list
member-list
member-list
...
} variable-list ;
struct Books
{
char title[];
char author[];
char subject[];
int book_id;
} book;
//此声明声明了拥有3个成员的结构体,分别为整型的a,字符型的b和双精度的c
//同时又声明了结构体变量s1
//这个结构体并没有标明其标签
struct
{
int a;
char b;
double c;
} s1; //此声明声明了拥有3个成员的结构体,分别为整型的a,字符型的b和双精度的c
//结构体的标签被命名为SIMPLE,没有声明变量
struct SIMPLE
{
int a;
char b;
double c;
};
//用SIMPLE标签的结构体,另外声明了变量t1、t2、t3
struct SIMPLE t1, t2[], *t3; //也可以用typedef创建新类型
typedef struct
{
int a;
char b;
double c;
} Simple2;
//现在可以用Simple2作为类型声明新的结构体变量
Simple2 u1, u2[], *u3;
//此结构体的声明包含了其他的结构体
struct COMPLEX
{
char string[];
struct SIMPLE a;
}; //此结构体的声明包含了指向自己类型的指针
struct NODE
{
char string[];
struct NODE *next_node;
};
struct B;    //对结构体B进行不完整声明

//结构体A中包含指向结构体B的指针
struct A
{
struct B *partner;
//other members;
}; //结构体B中包含指向结构体A的指针,在A声明完后,B也随之进行声明
struct B
{
struct A *partner;
//other members;
};
#include <stdio.h>

struct Books
{
char title[];
char author[];
char subject[];
int book_id;
} book = {"C 语言", "RUNOOB", "编程语言", }; int main()
{
printf("title : %s\nauthor: %s\nsubject: %s\nbook_id: %d\n", book.title, book.author, book.subject, book.book_id);
}
#include <stdio.h>
#include <string.h> struct Books
{
char title[];
char author[];
char subject[];
int book_id;
}; int main( )
{
struct Books Book1; /* 声明 Book1,类型为 Books */
struct Books Book2; /* 声明 Book2,类型为 Books */ /* Book1 详述 */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = ; /* Book2 详述 */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = ; /* 输出 Book1 信息 */
printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
printf( "Book 1 subject : %s\n", Book1.subject);
printf( "Book 1 book_id : %d\n", Book1.book_id); /* 输出 Book2 信息 */
printf( "Book 2 title : %s\n", Book2.title);
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
printf( "Book 2 book_id : %d\n", Book2.book_id); return ;
}
#include <stdio.h>
#include <string.h> struct Books
{
char title[];
char author[];
char subject[];
int book_id;
}; /* 函数声明 */
void printBook( struct Books book );
int main( )
{
struct Books Book1; /* 声明 Book1,类型为 Books */
struct Books Book2; /* 声明 Book2,类型为 Books */ /* Book1 详述 */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = ; /* Book2 详述 */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = ; /* 输出 Book1 信息 */
printBook( Book1 ); /* 输出 Book2 信息 */
printBook( Book2 ); return ;
}
void printBook( struct Books book )
{
printf( "Book title : %s\n", book.title);
printf( "Book author : %s\n", book.author);
printf( "Book subject : %s\n", book.subject);
printf( "Book book_id : %d\n", book.book_id);
}
#include <stdio.h>
#include <string.h> struct Books
{
char title[];
char author[];
char subject[];
int book_id;
}; /* 函数声明 */
void printBook( struct Books *book );
int main( )
{
struct Books Book1; /* 声明 Book1,类型为 Books */
struct Books Book2; /* 声明 Book2,类型为 Books */ /* Book1 详述 */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = ; /* Book2 详述 */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = ; /* 通过传 Book1 的地址来输出 Book1 信息 */
printBook( &Book1 ); /* 通过传 Book2 的地址来输出 Book2 信息 */
printBook( &Book2 ); return ;
}
void printBook( struct Books *book )
{
printf( "Book title : %s\n", book->title);
printf( "Book author : %s\n", book->author);
printf( "Book subject : %s\n", book->subject);
printf( "Book book_id : %d\n", book->book_id);
}
struct 位域结构名
{ 位域列表 };
struct bs{
int a:;
int b:;
int c:;
}data;
struct packed_struct {
unsigned int f1:;
unsigned int f2:;
unsigned int f3:;
unsigned int f4:;
unsigned int type:;
unsigned int my_int:;
} pack;
struct bs{
unsigned a:;
unsigned :; /* 空域 */
unsigned b:; /* 从下一单元开始存放 */
unsigned c:
}
ain(){
struct bs{
unsigned a:;
unsigned b:;
unsigned c:;
} bit,*pbit;
bit.a=; /* 给位域赋值(应注意赋值不能超过该位域的允许范围) */
bit.b=; /* 给位域赋值(应注意赋值不能超过该位域的允许范围) */
bit.c=; /* 给位域赋值(应注意赋值不能超过该位域的允许范围) */
printf("%d,%d,%d\n",bit.a,bit.b,bit.c); /* 以整型量格式输出三个域的内容 */
pbit=&bit; /* 把位域变量 bit 的地址送给指针变量 pbit */
pbit->a=; /* 用指针方式给位域 a 重新赋值,赋为 0 */
pbit->b&=; /* 使用了复合的位运算符 "&=",相当于:pbit->b=pbit->b&3,位域 b 中原有值为 7,与 3 作按位与运算的结果为 3(111&011=011,十进制值为 3) */
pbit->c|=; /* 使用了复合位运算符"|=",相当于:pbit->c=pbit->c|1,其结果为 15 */
printf("%d,%d,%d\n",pbit->a,pbit->b,pbit->c); /* 用指针方式输出了这三个域的值 */
}

最新文章

  1. 图片处理GraphicsMagick &amp; ImageMagick
  2. eclipse连hadoop2.x运行wordcount 转载
  3. .NET蓝牙开源库:32feet.NET
  4. android stutio 快捷键
  5. 干货-iOS、mac开源项目及库,以后我也会持续更新。
  6. 数组有没有length()这个方法? String有没有length()这个方法?
  7. EF4.1之贪婪加载和延迟加载
  8. 队列与DelphiXe新语法
  9. chattr实现文件不可删除
  10. Uniqueidentifier数据类型
  11. BZOJ_1002_[FJOI2007]_轮状病毒_(递推+高精)
  12. Vue 2.0基础
  13. nginx 站点80跳443配置
  14. textarea中的空格与换行
  15. 使用Django清理数据库中的数据
  16. 二.redis 数据类型
  17. Jmeter+ant集成接口测试报告
  18. thymeleaf中的判断总结
  19. 如何确定系统上的CPU插槽数量
  20. 《转载》Eclipse项目上传码云

热门文章

  1. SpringMVC:提交参数名与接收参数名问题
  2. js保留的关键字
  3. 1.softmax初探
  4. Arduino LiquidCrystal库函数中文对照
  5. JAVA初学者——Hello,World!
  6. html标签title属性效果优化
  7. CocoaPods为多个target添加依赖库/Podfile的配置
  8. python笔记(很乱)、打算抽个时间再好好整理
  9. pycharm实用技巧
  10. github 新建库,提交命令