PHP速学视频免费教程(入门到精通)
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
编写一个C程序来定义结构体并显示成员变量的大小和偏移量
结构体 - 它是一个不同数据类型变量的集合,组合在一个名称下。
datatype member1; struct tagname{ datatype member2; datatype member n; };
在这里,struct - 关键字
tagname - 指定结构的名称
member1, member2 - 指定构成结构的数据项。
struct book{ int pages; char author [30]; float price; };
声明结构体变量有三种方式 -
struct book{ int pages; char author[30]; float price; }b;
struct{ int pages; char author[30]; float price; }b;
struct book{ int pages; char author[30]; float price; }; struct book b;
成员与结构变量之间的链接是通过成员运算符(或者点运算符)建立的。
可以通过以下方式进行初始化:
struct book{ int pages; char author[30]; float price; } b = {100, "balu", 325.75};
struct book{ int pages; char author[30]; float price; }; struct book b = {100, "balu", 325.75};
struct book{ int pages; char author[30]; float price; } ; struct book b; b. pages = 100; strcpy (b.author, "balu"); b.price = 325.75;
struct book{ int pages; char author[30]; float price; } ; struct book b; scanf ("%d", &b.pages); scanf ("%s", b.author); scanf ("%f", &b. price);
使用数据成员声明结构,并尝试打印它们的偏移值以及结构的大小。
实时演示
#include<stdio.h> #include<stddef.h> struct tutorial{ int a; int b; char c[4]; float d; double e; }; int main(){ struct tutorial t1; printf("the size 'a' is :%d<p>",sizeof(t1.a)); printf("the size 'b' is :%d</p> <p>",sizeof(t1.b)); printf("the size 'c' is :%d</p> <p>",sizeof(t1.c)); printf("the size 'd' is :%d</p> <p>",sizeof(t1.d)); printf("the size 'e' is :%d</p> <p>",sizeof(t1.e)); printf("the offset 'a' is :%d</p> <p>",offsetof(struct tutorial,a)); printf("the offset 'b' is :%d</p> <p>",offsetof(struct tutorial,b)); printf("the offset 'c' is :%d</p> <p>",offsetof(struct tutorial,c)); printf("the offset 'd' is :%d</p> <p>",offsetof(struct tutorial,d)); printf("the offset 'e' is :%d</p> <p></p> <p>",offsetof(struct tutorial,e)); printf("size of the structure tutorial is :%d",sizeof(t1)); return 0; }</p></stddef.h></stdio.h>
the size 'a' is :4 the size 'b' is :4 the size 'c' is :4 the size 'd' is :4 the size 'e' is :8 the offset 'a' is :0 the offset 'b' is :4 the offset 'c' is :8 the offset 'd' is :12 the offset 'e' is :16 size of the structure tutorial is :24
已抢7213个
抢已抢94858个
抢已抢14827个
抢已抢52071个
抢已抢194764个
抢已抢87280个
抢