目录
前言
🍓结构体的运用
🍓结构体与指针
struct Book
{
int isbn; //刊号
char author; //作者
float price; //单价
}
struct Book book1 = {12345678,"lqy",78.50f};
struct Book *pBook = &book1;
1️⃣指针解引用方式
(*pBook).isbn
printf("ISBN: %d\n",(*pBook).isbn);
printf("Author: %s\n",(*pBook).author);
printf("Price: %.2f\n",(*pBook).price);
ISBN: 12345678
Author: lqy
Price: 78.50
2️⃣非指针解引用方式
pBook->isbn
printf("ISBN: %d\n",pBook->isbn);
printf("Author: %s\n",pBook->author);
printf("Price: %.2f\n",pBook->price);
ISBN: 12345678
Author: lqy
Price: 78.50
int *pIsbn = &book1.isbn; //指向成员isbn指针
char *pAuthor = book1.author; //指向成员author指针
float *pPrice = &book1.price; //指向成员price指针
🍓结构体与数组
struct Book books[3] = {{12345678,"lqy",78.50f}
{23456789,"lqy1",75.50f}
{34567890,"lqy2",80.50f}};
for(int i = 0;i < 3;++i)
{
printf("ISBN: %d\n",books[i].isbn);
printf("Author: %s\n",book[i].author);
printf("Price: %.2f\n",book[i].price);
printf("----------------------------\n");
}
ISBN: 12345678
Author: lqy
Price: 78.50
----------------------------
ISBN: 23456789
Author: lqy1
Price: 75.50
----------------------------
ISBN: 34567890
Author: lqy2
Price: 80.50
----------------------------
for(int i = 0;i < 3;++i)
{
printf("ISBN: %d\n",(*(books + i)).isbn);
printf("Author: %s\n",(*(books + i)).author);
printf("Price: %.2f\n",(*(books + i)).price);
printf("----------------------------\n");
}
for(int i = 0;i < 3;++i)
{
printf("ISBN: %d\n",(books + i)->isbn);
printf("Author: %s\n",(books + i)->author);
printf("Price: %.2f\n",(books + i)->price);
printf("----------------------------\n");
}
🍓结构体与函数
struct Rect //矩形结构体
{
float length; //长
float width; //宽
float area; //面积
}
void calculate(struct Rect rect)
{
rect.area = rect.length * rect.width;
}
int main()
{
struct Rect rc; //定义结构体变量
printf("Please enter the length and width of the rectangle:\n");
scanf("%f%f",&rc.length,&rc.width); //获取用户输入的长和宽
calculate(rc); //调用calculate函数,计算矩形面积
printf("The area of the rectangle is: %.2f.\n",rc.area);
return 0;
}
Please enter the length and width of the rectangle:
4 5
The area of the rectangle is: 0.00.
struct Rect calculate(struct Rect rect)
{
rect.area = rect.length * rect.width;
return rect; //返回修改后的结构体变量rect
}
rc = calculate(rc);
Please enter the length and width of the rectangle:
4 5
The area of the rectangle is: 20.00.
void calculate(struct Rect *pRect)
{
pRect->area = pRect->length * pRect->width;
}
calculate(&rc);
Please enter the length and width of the rectangle:
4 5
The area of the rectangle is: 20.00.
struct Rect* calculate(struct Rect *pRect)
{
pRect->area = pRect->length * pRect->width;
return pRect;
}
rc = *calculate(&rc);
🍓结构体与字符串
struct Stu
{
int num; //学号
char *name; //姓名
};
struct Stu stu1 = {1,"Tom"};
stu1.name = "tom";
char str[20] = "Tom";
stu1.name = str;
*stu1.name = str;
printf("Name: %s\n",stu1.name);
Mane: tom
struct Stu stu2 = stu1;
strcpy(stu2.name,Jack);
printf("stu1 Name: %s\n",stu1.name);
printf("stu2 Name: %s\n",stu2.name);
stu1 Name: Jack
stu2 Name: Jack
#include <stdio.h>
#include <string.h>
struct Stu
{
int num; //学号
char *name; //姓名
};
int main()
{
struct Stu stu1 = {1,"Tom"};
char str1[20] = "Tom";
stu1.name = str1;
struct Stu stu2 = stu1;
char str2[20];
stu2.name = str2;
strcpy(stu2.name,"Jack");
printf("stu1 Name: %s\n",stu1.name);
printf("stu2 Name: %s\n",stu2.name);
return 0;
}
stu1 Name: Tom
stu2 Name: Jack