1、结构体的声明和结构体变量的定义

#include <stdio.h>                          
#include <string.h>                         
 
int main() 
{                               
	struct Person 
	{     
		char  name[20];
		char  address[20];
	};                                
 
	struct Person A; 
	struct Person B; 
    
	strcpy(A.name, "Li Ming");  
	strcpy(B.name, "Li Hua");     
	
	printf("%s", A.name);
	printf("%s", B.name);
    
	return 0;
}

 

 2、结构体数组

#include <stdio.h>
#include <string.h>
#define MAXTITL 30
#define MAXAUTH 30
#define MAXBKS 100

struct book 
{                                       
	char title[MAXTITL];
	char author[MAXAUTH];
	float value;
}; 

struct book library[MAXBKS];           

int main() {
	strcpy(library[0].title, "A tale of two cities");  
	strcpy(library[0].author, "Dickens");             
	library[0].value = 30;
	printf("title = %s\nauthor = %s\nvalue = %f\n", 
           library[0].title, library[0].author, library[0].value);
}

 

3、结构体嵌套

#include <stdio.h>
#include <string.h>
#include <math.h>
struct point 
{
	float x;
	float y;
};

struct rect 
{
	struct point pt1;
	struct point pt2;
};

struct rect A;

int main() {
	printf("请输入长方形的左下顶点坐标\n");
	scanf("%f", &A.pt1.x);
	scanf("%f", &A.pt1.y);
	
	printf("请输入长方形的右上顶点坐标\n");
	scanf("%f", &A.pt2.x);
	scanf("%f", &A.pt2.y);
	
	printf("长方形的面积为:%f\n",(A.pt2.x- A.pt1.x)*(A.pt2.y- A.pt1.y));
}

 

4、结构体指针

#include <stdio.h>
#include <string.h>
#define LEN 20

struct name 
{
	char first[LEN];
	char last[LEN];
};

typedef struct guy 
{
	struct name handle;
	char favfood[LEN];
	char job[LEN];
	float income;
}GUY_INFO_S;

int main(void)
{
	GUY_INFO_S stFellow[2] = 
	{
		{{"Ewen","Villard"},"grilled salmon","personality coach",68112.00},
		{{"Rodney","Swillbelly"}, "tripe", "tabloid editor", 432400.00}
	};
	
	printf("\n信息如下: \n");
	printf("Fellow[0]的地址是:%p\n", &stFellow[0]);
	printf("Fellow[1]的地址是:%p\n", &stFellow[1]);
	
	GUY_INFO_S* pstHim;	
	pstHim = stFellow;
	
	printf("Fellow[0]的地址是:%p\n", pstHim);
	printf("Fellow[1]的地址是:%p\n", pstHim+1);	
	
	printf("pstHim->handle.first = %s\n", pstHim->handle.first);
	printf("pstHim->handle.last  = %s\n", pstHim->handle.last);	
	printf("pstHim->income       = $%.2f, \n", pstHim->income);
	printf("(*pstHim).income     = $%.2f\n\n", (*pstHim).income);
};

 

5、结构体作为函数参数

#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);
}

 

posted on 2022-02-28 15:52  轩~邈  阅读(80)  评论(0)    收藏  举报