可以直接赋值。定义结构体类型,然后用这个类型定义出来的变量就是结构体变量。
C语言在相同类型的变量间赋值时是直接内存复制的,即将他们的内存进行复制,这里因为同样结构体变量,属于同一种变量,所以赋值时是按照他们的内存分布来直接拷贝的。
举例:
voidmain()
{
STUstu1={0,10};
STUtemp={12,88};
STU*p1=&stu1;
STU*p2=&temp;
printf("%d-%d\n",temp.name,temp.num);
temp=stu1;
printf("%d-%d\n",temp.name,temp.num);
temp={10,10};
printf("%d-%d\n",stu1->name,stu1->num);
printf("%ld-\n",&stu1);
printf("%ld-\n",stu1);
printf("%ld-\n",&temp);
printf("%ld-\n",temp);
change(stu1,temp);
temp=stu1;
p2=p1;
printf("%d-%d\n",p2->name,p2->num);
}
扩展资料:C语言中变量间互相赋值很常见,例如:
inta,b;
a=b;
结构体也是变量(自定义变量),两个结构体之间直接赋值按道理应该也是可以的吧,将一个结构体对象赋值给另一个结构体对象的。
例:
#include"stdio.h"
structtest
{
inta;
intb;
intc;
char*d;
};
intmain()
{
structtestt1={1,2,3,"tangquan"};
structtestt2={0,0,0,""};
printf("%d,%d,%d,%s\r\n",t2.a,t2.b,t2.c,t2.d);
t2=t1;
printf("%d,%d,%d,%s\r\n",t2.a,t2.b,t2.c,t2.d);
return0;
}
intmain(void){
structstudentsbao={}
printf("%d,%s\n",bao.id,bao.name)//输出是4224528,空(应该是null)
//structstudentsbao={3,"123"}可以。第一种赋值方法
//strcpy(bao.name,"bao")//可以,
//printf("%d,%s\n",bao.id,bao.name)
//bao.name="bao"错误“stray'\351'inprogram”其他是乱码,
//bao.name[0]='a'
//bao.name[0]='/0'
//printf("%d,%s\n",bao.id,bao.name)
/*这样可以,*/
//chararr[10]="baobao"
////bao.name=arr//error"assignmenttoexpressionwitharraytype"
//scanf("%s",bao.name)//可以,
//printf("%d,%s\n",bao.id,bao.name)
//所以scanf那一类函数都可以。
//还有就是memcpy函数也是可以的
return0
}
扩展资料
C语言结构体数组的直接赋值及数组的长度计算:
#include<stdio.h>
//自定义一个字符串的结构体,包含字符串和字符串长度两个变量
typedefstructStr{
charch[100]
intlength//char数组(字符串)的长度
}myStr
//刚开始声明变量时每个变量的字符串长度length都为0
//这里以长度为10的数组为例,数组长度是1000
//对第0个到第9个结构体数组的长度同时赋值为0
myStrmyStr1[10]={
[0...9]={
.length=0,
}
}
intmain(){
inti
for(i=0i<10i++){
printf("%d\n",myStr1[i].length)
}
return0
}
结构体数组指针作为函数参数,通过数组的首地址与偏移量对结构体数组进行scanf的赋值,在函数中通过指针间接访问到其指向的内存。
举例:编写函数,输入5个学号(int),5个姓名(字符串),5个成绩数组(每组三个成绩)(int[3]),依次调用函数
#include <stdio.h>
#include <stdlib.h>
struct student //建立结构体,学号,姓名,3门课的分数
{
int num
char name[10]
int score[3]
}Stu[5] //初始化,一共5个学生的数据
void getScore(struct student * p) //函数:向结构体读取分数,一共三门课
{
int i, j
for (i = 0i <5i++)
for (j = 0j <3j++)
scanf_s("%d", (&(p+i)->score[j]))
}
void getNum(struct student * p) //函数:向结构体读取学号
{
int i
for (i = 0i <5i++)
scanf_s("%d", &(p + i)->num)
}
void getName(struct student * p) //函数:向结构体读取姓名
{
int i
for (i = 0i <5i++)
scanf("%s", &(p + i)->name)
}
int main()
{
int i, j, average[3] = { 0 } //average数组储存每门课的平均分
getNum(Stu) //函数调用
getName(Stu)
getScore(Stu)
for (j = 0j <3j++)
{
for (i = 0i <5i++)
average[j] += Stu[i].score[j]
}
for (i = 0i <5i++)
{
printf("num = %d name = %s Score:", Stu[i].num, Stu[i].name) //依次打印学号 姓名
//printf("%d %d %d", Stu[0].score[0],Stu[0].score[1],Stu[0].score[2])
for (j = 0j <3j++) //打印三门课的分数
printf(" %d", Stu[i].score[j])
printf("\n")
}
printf("average:")
for (i = 0i <3i++)
printf("%f ", (float)average[i]/5) //打印三门课平均分
printf("\n")
system("pause")
return 0
}
如:
scanf("%c%c%c",&a,&b,&c)
输入为:
d e f
则把'd'赋予a, ' '(空格)赋予b,'e'赋予c。因为%c 只要求读入一个字符,后面不需要用空格作为两个字符的间隔,因此把' '作为下一个字符送给b。
只有当输入为:def(字符间无空格) 时,才能把'd'赋于a,'e'赋予b,'f'赋予c。
扩展资料:1、函数原型
int scanf(const char * restrict format,...)
函数 scanf() 是从标准输入流stdin [1] (标准输入设备,一般指向键盘)中读内容的通用子程序,可以说明的格式读入多个字符,并保存在对应地址的变量中。
函数的第一个参数是格式字符串,它指定了输入的格式,并按照格式说明符解析输入对应位置的信息并存储于可变参数列表中对应的指针所指位置。每一个指针要求非空,并且与字符串中的格式符一一顺次对应。
2、返回值
scanf函数返回成功读入的数据项数,读入数据时遇到了“文件结束”则返回EOF。如:
scanf("%d %d",&a,&b)函数返回值为int型。如果a和b都被成功读入,那么scanf的返回值就是2;
如果只有a被成功读入,返回值为1;如果a和b都未被成功读入,返回值为0;如果遇到错误或遇到end of file,返回值为EOF。end of file为Ctrl+z 或者Ctrl+d。
例:使用scanf函数输入数据。
#include <stdio.h>
int main(void)
{
int a,b,c
printf("Give me the value of a,b,c seperated with whitespaces:\n")
scanf("%d%d%d",&a,&b,&c)
printf("a=%d,b=%d,c=%d\n",a,b,c)
return 0
}
注意:&a,&b,&c中的&是寻址操作符,&a表示对象a在内存中的地址,是一个右值。变量a,b,c的地址是在编译阶段分配的(存储顺序由编译器决定)。
如果scanf中%d是连着写的如“%d%d%d”,在输入数据时,数据之间不可以用逗号分隔,只能用空白字符(空格或tab键或者回车键)分隔——“2 (空格)3(tab) 4” 或 “2(tab)3(回车)4”等。若是“%d,%d,%d”,则在输入数据时需要加“,”,如“2,3,4”。
参考资料:
百度百科——scanf(计算机语言函数)