首页 / C语言 / c语言结构体学习-----简记(2)
c语言结构体学习-----简记(2)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c语言结构体学习-----简记(2),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3702字,纯文字阅读大概需要6分钟。
内容图文
结构体的指针(续)
注意这几行
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 Books Book1;
printBook( &Book1 );
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books *book );
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info by passing address of Book1 */
printBook( &Book1 );
/* print Book2 info by passing address of Book2 */
printBook( &Book2 );
return 0;
}
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 Books books[2];
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books *book );
int main( ) {
struct Books books[2];
/* book 1 specification */
strcpy( books[0].title, "C Programming");
strcpy( books[0].author, "Nuha Ali");
strcpy( books[0].subject, "C Programming Tutorial");
books[0].book_id = 6495407;
/* book 2 specification */
strcpy( books[1].title, "Telecom Billing");
strcpy( books[1].author, "Zara Ali");
strcpy( books[1].subject, "Telecom Billing Tutorial");
books[1].book_id = 6495700;
/* print Book1 info by passing address of Book1 */
printBook( &books[0] );
/* print Book2 info by passing address of Book2 */
printBook( &books[1] );
return 0;
}
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);
}
结构体占用内存计算 (函数实现)
sizeof(struct Books)
#include <stdio.h>
#include <string.h>
struct Books {
};
int main( ) {
printf("%d\n", (int) sizeof(struct Books)); /*0*/
return 0;
}
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
printf("%d\n", (int) sizeof(struct Books)); /*204*/
return 0;
}
位域 (没太搞懂)
有时候我们内存紧张的时候,我们可以使用位域定义结构体成员变量,比如当我们需要定义一个表示true
或false
的时候,如果想这样定义
int isOpen;
明显很浪费空间,因为一个真假值只需要一个字位表示,所以我们可以这样定义
unsigned int isOpen:1;
但是如果你直接写在函数中是会报错的,我们应该写在结构体中
int main() {
unsigned int isOpen:1; /*编译无法通过*/
return 0;
}
正确姿势
struct packed_struct {
unsigned int f1:1;
unsigned int f2:1;
unsigned int f3:1;
unsigned int f4:1;
unsigned int type:4;
unsigned int my_int:9;
} pack;
C尽可能紧凑地自动打包上述位字段,前提是字段的最大长度小于或等于计算机的整数字长。如果不是这种情况,那么一些编译器可能允许字段存储器重叠,而其他编译器会将下一个字段存储在下一个字中。
#include <stdio.h>
#include <string.h>
struct packed_struct {
unsigned int f1:1;
unsigned int f2:1;
unsigned int f3:1;
unsigned int f4:1;
unsigned int type:4;
unsigned int my_int:9;
} pack;
int main() {
printf("%d\n", (int) sizeof(struct packed_struct));
return 0;
}
输出结果 8
内容总结
以上是互联网集市为您收集整理的c语言结构体学习-----简记(2)全部内容,希望文章能够帮你解决c语言结构体学习-----简记(2)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。