C语言结构体

定义结构

为了定义结构,您必须使用 struct 语句。struct 语句定义了一个包含多个成员的新的数据类型,struct 语句的格式如下:

struct tag {
member-list
member-list
member-list
...
} variable-list ;

tag 是结构体标签。

member-list 是标准的变量定义,比如 int i; 或者 float f,或者其他有效的变量定义。

variable-list 结构变量,定义在结构的末尾,最后一个分号之前,您可以指定一个或多个结构变量。下面是声明 Book 结构的方式:

struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
在一般情况下,tag、member-list、variable-list 这 3 部分至少要出现 2 个。以下为实例:

//此声明声明了拥有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[20], *t3;

//也可以用typedef创建新类型
typedef struct
{
int a;
char b;
double c;
} Simple2;
//现在可以用Simple2作为类型声明新的结构体变量
Simple2 u1, u2[20], *u3;

 

 

结构体变量的初始化
和其它类型变量一样,对结构体变量可以在定义时指定初始值。

实例
#include <stdio.h>

struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book = {"C 语言", "RUNOOB", "编程语言", 123456};

int main()
{
printf("title : %s\nauthor: %s\nsubject: %s\nbook_id: %d\n", book.title, book.author, book.subject, book.book_id);
}
执行输出结果为:

title : C 语言
author: RUNOOBsubject: 编程语言book_id: 123456

 

 

 

访问结构成员

#include<stdio.h>#include<string.h>structBooks{chartitle[50]; charauthor[50]; charsubject[100]; intbook_id; }; intmain(){structBooksBook1; /* 声明 Book1,类型为 Books */structBooksBook2; /* 声明 Book2,类型为 Books *//* Book1 详述 */strcpy(Book1.title, "C Programming"); strcpy(Book1.author, "Nuha Ali"); strcpy(Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; /* Book2 详述 */strcpy(Book2.title, "Telecom Billing"); strcpy(Book2.author, "Zara Ali"); strcpy(Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; /* 输出 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); return0; }

 当上面的代码被编译和执行时,它会产生下列结果:

 Book1 title : C Programming

Book1 author :NuhaAliBook1 subject : C ProgrammingTutorialBook1 book_id :6495407Book2 title :TelecomBillingBook2 author :ZaraAliBook2 subject :TelecomBillingTutorialBook2 book_id :6495700

 

结构作为函数参数

您可以把结构作为函数参数,传参方式与其他类型的变量或指针类似。您可以使用上面实例中的方式来访问结构变量:

#include <stdio.h>
#include <string.h>

struct Books
{
char title[50];
char author[50];
char subject[100];
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 = 6495407;

/* Book2 详述 */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;

/* 输出 Book1 信息 */
printBook( Book1 );

/* 输出 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);
}

当上面的代码被编译和执行时,它会产生下列结果:

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_pointer;

现在,您可以在上述定义的指针变量中存储结构变量的地址。为了查找结构变量的地址,请把 & 运算符放在结构名称的前面,如下所示:

struct_pointer = &Book1;

为了使用指向该结构的指针访问结构的成员,您必须使用 -> 运算符,如下所示:

struct_pointer->title;

让我们使用结构指针来重写上面的实例,这将有助于您理解结构指针的概念:

#include<stdio.h>#include<string.h>structBooks{chartitle[50]; charauthor[50]; charsubject[100]; intbook_id; }; /* 函数声明 */voidprintBook(structBooks *book); intmain(){structBooksBook1; /* 声明 Book1,类型为 Books */structBooksBook2; /* 声明 Book2,类型为 Books *//* Book1 详述 */strcpy(Book1.title, "C Programming"); strcpy(Book1.author, "Nuha Ali"); strcpy(Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; /* Book2 详述 */strcpy(Book2.title, "Telecom Billing"); strcpy(Book2.author, "Zara Ali"); strcpy(Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; /* 通过传 Book1 的地址来输出 Book1 信息 */printBook( &Book1); /* 通过传 Book2 的地址来输出 Book2 信息 */printBook( &Book2); return0; }voidprintBook(structBooks *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); }

当上面的代码被编译和执行时,它会产生下列结果:

Book title : C ProgrammingBook author :NuhaAliBook subject : C ProgrammingTutorialBook book_id :6495407Book title :TelecomBillingBook author :ZaraAliBook subject :TelecomBillingTutorialBook book_id :6495700

 

 

 

 

 

 

 

struct Books
{
char title[50];
char author[50];
char subject[100];
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 = 321;
/* Book2 详述 */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 123;

/* 输出 Book1 信息 */
printBook( Book1 );

/* 输出 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 students{
char name[20];
char sex[10];
char age[20];
};

void all(struct students student1);//声明输出函数
struct students student(); //声明输入函数
int main(){
struct students student1;
struct students student2=student();

strcpy(student1.name,"张三");
strcpy(student1.sex,"男");
strcpy(student1.age,"18");

all(student1);
all(student2);
return 0;
}

//输出函数
void all(struct students student1){
printf("%s\n",student1.name);
printf("%s\n",student1.sex);
printf("%s\n",student1.age);
printf("\n");

}


添加函数
struct students student(){
struct students x={"Bruce","man","21"};
return x;
}

 

 

 

 

 

 

 

结构体菜鸟教程网址

https://www.runoob.com/cprogramming/c-structures.html

 

 

 

 

 

 

 

 

#include<stdio.h>#include<string.h>structBooks{chartitle[50]; charauthor[50]; charsubject[100]; intbook_id; }; intmain(){structBooksBook1; /* 声明 Book1,类型为 Books */structBooksBook2; /* 声明 Book2,类型为 Books *//* Book1 详述 */strcpy(Book1.title, "C Programming"); strcpy(Book1.author, "Nuha Ali"); strcpy(Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; /* Book2 详述 */strcpy(Book2.title, "Telecom Billing"); strcpy(Book2.author, "Zara Ali"); strcpy(Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; /* 输出 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); return0; }

#include<stdio.h>#include<string.h>structBooks{chartitle[50]; charauthor[50]; charsubject[100]; intbook_id; }; intmain(){structBooksBook1; /* 声明 Book1,类型为 Books */structBooksBook2; /* 声明 Book2,类型为 Books *//* Book1 详述 */strcpy(Book1.title, "C Programming"); strcpy(Book1.author, "Nuha Ali"); strcpy(Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; /* Book2 详述 */strcpy(Book2.title, "Telecom Billing"); strcpy(Book2.author, "Zara Ali"); strcpy(Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; /* 输出 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); return0; }

#include<stdio.h>#include<string.h>structBooks{chartitle[50]; charauthor[50]; charsubject[100]; intbook_id; }; /* 函数声明 */voidprintBook(structBooksbook); intmain(){structBooksBook1; /* 声明 Book1,类型为 Books */structBooksBook2; /* 声明 Book2,类型为 Books *//* Book1 详述 */strcpy(Book1.title, "C Programming"); strcpy(Book1.author, "Nuha Ali"); strcpy(Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; /* Book2 详述 */strcpy(Book2.title, "Telecom Billing"); strcpy(Book2.author, "Zara Ali"); strcpy(Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; /* 输出 Book1 信息 */printBook(Book1); /* 输出 Book2 信息 */printBook(Book2); return0; }voidprintBook(structBooksbook){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); }

当上面的代码被编译和执行时,它会产生下列结果:

posted @ 2021-01-29 12:55  Bruce_Sun  阅读(95)  评论(0)    收藏  举报