
第一个printf不用说打印的就是a的元素;说说b,b=a把a赋给b;gender和score不用说;name是个指针,所以复制给b的也是个指针,a的name和b的name指向同一块类存地址,然后调用f();函数把b的name指针传递进去,在f()函数中开辟一块空间并把这块空间的地址赋给b的name指针,f()函数结束后,在f()中开辟的空间并未消失了,f()函数中b。name指针是局部的,f();结束后b的name指针在栈中被释放了,所以b。name和a的name指正还是一样的,
第二段代码就选a;
在struct stu中name是个字符数组,在创建stu的一个对象后,就有空间储存字符,b=a;你懂的
然后调用f()函数,把qian写入b的name的那块空间,空间中的字符串就变为了qian,
#include "stdio.h"
void main(){
struct Student{
char id[10]
char name[10]
int score
}
stu = {"001", "Sam", 80}
printf("%s\t%s\t%d", stu.id, stu.name, stu.score)
}
扩展资料在C语言中,结构体(struct)指的是一种数据结构,是C语言中聚合数据类型(aggregate data type)的一类。结构体可以被声明为变量、指针或数组等,用以实现较复杂的数据结构。结构体同时也是一些元素的集合,这些元素称为结构体的成员(member),且这些成员可以为不同的类型,成员一般用名字访问。
C++提供了许多种基本的数据类型(如int、float、double、char等)供用户使用。但是由于程序需要处理的问题往往比较复杂,而且呈多样化,已有的数据类型显得不能满足使用要求。
因此C++允许用户根据需要自己声明一些类型,用户可以自己声明的类型还有结构体类型(structure)、共用体类型(union)、枚举类型(enumeration)、类类型(class )等,这些统称为用户自定义类型(user-defined type,UDT)。
#include<stdio.h>#include<string.h>
#include<stdlib.h>
struct telephone
{
char name[10]
char telno[20]
}
void search(struct telephone b[], char *x, int n)
int main()
{
int i,n
struct telephone b[100]
char nane[100]
for(i=0i++)
{
printf("Please input name:")
gets(b[i].name)
if(b[i].name[0]=='#')
break
printf("Please input telephone:")
gets(b[i].telno)
}
n=i
printf("Please input you want to find name:")
gets(nane)
search(b,&nane[0],n)
return 0
}
void search(struct telephone b[],char *x,int n)
{
int i
int find=0
for(i=0i<ni++)
{
if(strcmp(x,b[i].name)==0)
{
printf("the telephone is %s\n",b[i].telno)
find=1
}
}
if(find==0)
printf("Not found!")
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)