代码学习记录1

时间:2022-10-03 17:58:27
#include<stdio.h>
struct Book //结构体
{
char name[20];
short price;
};
int main()
{
struct Book b = { "c语言程序设计",100 };
printf("书名:《%s\n》", b.name);
printf("原价:%d\n", b.price);
struct Book* B = &b;
printf("%s\n", (*B).name);
printf("%d\n", (*B).price);
printf("%s\n",B->name);
printf("%d\n",B->price);
b.price =80;
printf("降价后:%d\n", b.price);
return 0;
}

(.):结构体变量.成员

(->):指针变量->成员

上面已经经过赋值来改变价格,然后我就在想怎么改变书名?然后我就偷摸摸的的跑去问了老师,嘿嘿还真有方法!​

strcpy--string copy  

#include<stdio.h>
#include<string.h>
struct Book
{
char name[20];
short price;
};
int main()
{
struct Book b={"《c语言程序设计》",100};
printf("书名:《%s\n》",b.name);
printf("价格:%d\n",b.price);
b.price=80;
printf("折后价:%d\n",b.price);
strcpy(b.name,"新书名"); //这样就可以了奥
printf("%s\n",b.name);
return 0;
}

最后分享日落啦!


代码学习记录1