struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
#include <stdio.h>
#include <string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
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 */
printf( "Book 1 title : %s
", Book1.title);
printf( "Book 1 author : %s
", Book1.author);
printf( "Book 1 subject : %s
", Book1.subject);
printf( "Book 1 book_id : %d
", Book1.book_id);
/* print Book2 info */
printf( "Book 2 title : %s
", Book2.title);
printf( "Book 2 author : %s
", Book2.author);
printf( "Book 2 subject : %s
", Book2.subject);
printf( "Book 2 book_id : %d
", Book2.book_id);
return 0;
}
Book 1 title : C Programming Book 1 author : Nuha Ali Book 1 subject : C Programming Tutorial Book 1 book_id : 6495407 Book 2 title : Telecom Billing Book 2 author : Zara Ali Book 2 subject : Telecom Billing Tutorial Book 2 book_id : 6495700
#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 */
printBook( Book1 );
/* Print Book2 info */
printBook( Book2 );
return 0;
}
void printBook( struct Books book )
{
printf( "Book title : %s
", book.title);
printf( "Book author : %s
", book.author);
printf( "Book subject : %s
", book.subject);
printf( "Book book_id : %d
", book.book_id);
}
Book title : C Programming Book author : Nuha Ali Book subject : C Programming Tutorial Book book_id : 6495407 Book title : Telecom Billing Book author : Zara Ali Book subject : Telecom Billing Tutorial Book book_id : 6495700
struct Books *struct_yiibaier;
struct_yiibaier = &Book1;
struct_yiibaier->title;
#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
", book->title);
printf( "Book author : %s
", book->author);
printf( "Book subject : %s
", book->subject);
printf( "Book book_id : %d
", book->book_id);
}
Book title : C Programming Book author : Nuha Ali Book subject : C Programming Tutorial Book book_id : 6495407 Book title : Telecom Billing Book author : Zara Ali Book subject : Telecom Billing Tutorial Book book_id : 6495700
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;
struct stuff *ref = &Huqinwei;
ref->age = 100;
printf("age is:%d\n",Huqinwei.age);
struct stuff *ptr;
ptr->age = 200;
printf("age is:%d\n",Huqinwei.age);
struct test{
int a[3];
int b;
};
//对于数组和变量同时存在的情况,有如下定义方法:
struct test student[3] = {{{66,77,55},0},
{{44,65,33},0},
{{46,99,77},0}};
//特别的,可以简化成:
struct test student[3] = {{66,77,55,0},
{44,65,33,0},[b] {46,99,77,0}};
[/b]
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct changeable{
int iCnt;
char pc[0];
}schangeable;
main(){
printf("size of struct changeable : %d\n",sizeof(schangeable));
schangeable *pchangeable = (schangeable *)malloc(sizeof(schangeable) + 10*sizeof(char));
printf("size of pchangeable : %d\n",sizeof(pchangeable));
schangeable *pchangeable2 = (schangeable *)malloc(sizeof(schangeable) + 20*sizeof(char));
pchangeable2->iCnt = 20;
printf("pchangeable2->iCnt : %d\n",pchangeable2->iCnt);
strncpy(pchangeable2->pc,"hello world",11);
printf("%s\n",pchangeable2->pc);
printf("size of pchangeable2 : %d\n",sizeof(pchangeable2));
}
size of struct changeable : 4 size of pchangeable : 4 pchangeable2->iCnt : 20 hello world size of pchangeable2 : 4
//对于“一锤子买卖”,只对最终的结构体变量感兴趣,其中A、B也可删,不过最好带着
struct A{
struct B{
int c;
}
b;
}
a;
//使用如下方式访问:
a.b.c = 10;
struct A{
struct B{
int c;
}b;
struct B sb;
}a;
a.b.c = 11;
printf("%d\n",a.b.c);
a.sb.c = 22;
printf("%d\n",a.sb.c);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有