关于传递结构体指针中指针 值传递的问题。

这里包括形参和实参
struct dangdangtest
char name[<span style="color: #];
void change(int num)//值传递 新建一个变量接受传递的值
num = <span style="color: #00;
//使用结构体作为参数 浪费内存 需要建立结构体
void change2(struct dangdangtest ddt)
ddt.num = <span style="color: #00;
printf("\nmain=%p,%p",ddt,&ddt.num);//函数内部结构体地址
//函数内部改变需要地址 所以需要指针保存
void changeall(struct dangdangtest *p)
(*p).num = <span style="color: #00;//*根据地址取出内容
void main5()
ddt.num = <span style="color: #;
sprintf(ddt.name,"adf");//初始化
printf("\nmain=%p,%p",ddt,&ddt.num);//main函数内部结构体地址
/*change(ddt.num);
printf("%d",ddt.num);99*/
/*change2(ddt);
printf("%d",ddt.num);*/
changeall(&ddt);
printf("\n%d",ddt.num);
getchar();
//参数对于数组来说传递的是地址 4个字节
void datatest(struct dangdangtest ddd[<span style="color: #])
printf("datatest=%d",sizeof(ddd));
void dtc(struct dangdangtest ddd[<span style="color: #])
ddd[<span style="color: #].num = <span style="color: #;
sprintf(ddd[<span style="color: #].name,"<span style="color: #4656");//前面有提过
printf("datatest=%d",sizeof(ddd));
void main()
struct dangdangtest ddd[<span style="color: #];//<span style="color: #个元素 36*10
struct dangdangtest *p =
printf("\n%d",sizeof(ddd));//
ddd[<span style="color: #].num = <span style="color: #;
sprintf(ddd[<span style="color: #].name,"dfasdf");
/*datatest(ddd);
printf("main=%d",sizeof(ddd));*/
getchar();
阅读(...) 评论()结构体 赋值 初始化-结构体类型的指针初始化和赋值问题
作者:用户
浏览:422 次
结构体类型的指针初始化和赋值问题编程的时候我定义了一个结构体,结构体有几个变量的值我必须要赋初值,因为循环得用。结构体这样定义:typedefstruct{BYTEstream_Tunsign
结构体类型的指针初始化和赋值问题
编程的时候我定义了一个结构体,结构体有几个变量的值我必须要赋初值,因为循环得用。结构体这样定义:typedef struct{
BYTE stream_T
unsigned short PID;
unsigned short InfoD}M_PICE;用的时候:M_PICE* p_Mpice = NULL;p_Mpice-&InfoDescrip = 0;运行到上一句就报错Unhandled exception in DVB.exe:0xC0000005:Access Violation
typedef struct pice{
int stream_T
unsigned short PID;
unsigned short InfoD}M_PICE;M_PICE* pMint main(){
pMypice = (M_PICE*)malloc(sizeof(M_PICE));//此处分配内存空间,否则怎么存数据、
pMypice-&InfoDescrip = 0;
printf(""%dn""pMypice-&InfoDescrip);}赶紧采纳。
【云栖快讯】红轴机械键盘、无线鼠标等753个大奖,先到先得,云栖社区首届博主招募大赛9月21日-11月20日限时开启,为你再添一个高端技术交流场所&&
稳定可靠、可弹性伸缩的在线数据库服务,全球最受欢迎的开源数据库之一
6款热门基础云产品6个月免费体验;2款产品1年体验;1款产品2年体验
弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率
开发者常用软件,超百款实用软件一站式提供C语言两个结构类型的指针的值传递问题【c语言吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:548,047贴子:
C语言两个结构类型的指针的值传递问题收藏
学校的作业,在老师给的代码中有如下的形式:typedef string message&79&;struct args{};这是不让改的,并且由于是在服务器间走通信变量必须以这种形式存在现在的问题是,我有另一个结构struct args2{message msg2;};有两个指针 struct *args,struct *args2 (同上这里必须采用指针的形式以完成某些服务)args是提交上来的值,args2会先经过malloc来分配空间,能够保证两个指针都不为空。我需要让args2-&msg2具有与args-&msg相同的值,但是由于某些原因两个指针不可以指向同一个地址(即不能直接使用args2-&msg2=args-&msg)该怎么办?已经尝试过并失败(段错误)的方法包括stpcpy(args2-&msg2,args-&msg);*args2-&msg2=*args-&貌似只要涉及到*args-&msg的地方就有段错误。求教怎么能拿出这个值来,让两个指针的值相同但是不能指向同一个地址万分感谢哦
c语言哪里好?达内,0基础入学,无专业要求,先就业后付款,毕业&薪&起点.c语言哪里好?达内C语言课程,独创TTS8.0授课模式,一线名师授课.
顺便求问linux下跟踪segmentation fault的方法
登录百度帐号推荐应用博客访问: 901414
博文数量: 164
博客积分: 10053
博客等级: 上将
技术积分: 1340
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: C/C++
结构体的定义:
typedef struct
&&&& char ip[30];&&&&
&&&& char usb[30];
}xiaoshou_
结构体的初始化:
xiaoshou_cmd *conf,&&&
strcpy(tem.ip,"xiaoshou");&&&&
printf("ip= %s\n",tem.ip);&&&& strcpy(tem.usb,"xiaoshou");&&&&
printf("ip= %s\n",tem.usb);
结构体的指针初始化:
xiaoshou_cmd *conf,&&&
conf=&&&&&&&&& //结构体指针必须实例化,不然会出现莫名的错误!
strcpy(conf-&ip,"xiaoshou");&&&&
printf("ip= %s\n",conf-&ip);
strcpy(conf-&usb,"xiaoshou");&
printf("ip= %s\n",conf-&usb);
测试程序:
/××××××××将命令行输入的参数传递到主函数中×××××××××××××××××/#include &stdio.h&
#include &unistd.h&
#include &string.h&
typedef struct
{&&& char ip[30];
&&&&& char usb[30];
&&&&& int help_
}xiaoshou_
xiaoshou_cmd * get_cmd_conf1(int argc,char& *argv[]);
int main(int argc,char& *argv[])
&&& printf("argc= %d \n",argc);
&&& xiaoshou_cmd *conf,
&&& conf=&
&&& opterr = 0;
&&& conf=get_cmd_conf1(argc,argv);
&&& if (conf-&help_flag==1)
&&& &&& printf("-i:&& server ip!\n");
&&& &&& printf("-u:&& device usb!\n");
&&& &&& printf("-h:&&& help!\n");
&&& &&& printf("ip= %s\n",conf-&ip);
&&& &&& printf("usb= %s\n",conf-&usb);
&&& return 0;
xiaoshou_cmd * get_cmd_conf1(int argc,char& *argv[])
&&& xiaoshou_cmd *
&&& opterr = 0;
&&& while((result=getopt(argc,argv,"i:u:h"))!=-1)
&&& &&& switch(result)
&&& &&& &&& case 'u':
&&& &&& &&& &&& strcpy(res-&usb,optarg);
&&& &&& &&& &&&
&&& &&& &&& case 'i':
&&& &&& &&& &&& strcpy(res-&ip,optarg);
&&& &&& &&& &&&
&&& &&& &&& case 'h':
&&& &&& &&& &&& res-&help_flag=1;
&&& &&& &&& &&&
阅读(2083) | 评论(0) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。博客访问: 250025
博文数量: 108
博客积分: 3265
博客等级: 中校
技术积分: 1207
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: C/C++
//对指向结构体类型变量的正确使用。输入一个结构体类型变量的成员,并输出。
#include &stdlib.h& /*使用malloc()需要* /
struct data / *定义结构体* /
&& int day,month,
struct stu /*定义结构体* /
&&& char name[20];
/嵌*套的结构体类型成员*/
main() /*定义m a i n ( ) 函数* /
&&&& struct stu * 定/*义结构体类型指针*/
& & &student=malloc(sizeof(struct stu)); 为/指* 针变量分配安全的地址*/
&&&& printf("Input name,number,year,month,day:\n");
&&&& scanf("%s",student-&name); 输/*入学生姓名、学号、出生年月日*/
&&&& scanf("%ld",&student-&num);
&&&& scanf("%d%d%d",&student-&birthday.year,&student-&birthday.month,
&&&& &student-&birthday.day);
&&&& printf("\nOutputname,number,year,month,day\n");
&&&& /*打印输出各成员项的值*/
&&&& printf("%20s%10ld%10d//%d//%d\n",student-&name,student-&num,
&&&& student-&birthday.year,student-&birthday.month,
&&&& student-&birthday.day);
程序中使用结构体类型指针引用结构体变量的成员,需要通过C提供的函数malloc()来为指针分配安全的地址。函数sizeof()返回值是计算给定数据类型所占内存的字节数。指针所指各成员形式为:
student-&name
student-&num
student-&birthday.year
student-&birthday.month
student-&birthday.day
===============================
struct data
&&& intday,month,
struct stu/*定义结构体*/
&&& char name[20];
&&& /嵌*套的结构体类型成员*/
struct stu student[4],*p;定/*义结构体数组及指向结构体类型的指针*/
作p=student,此时指针p就指向了结构体数组student。
p是指向一维结构体数组的指针,对数组元素的引用可采用三种方法。
student+i和p+i均表示数组第i个元素的地址,数组元素各成员的引用形式为:
(student+i)-&name、(student+i)-&num和(p+i)-&name、(p+i)-&num等。student+i和p+i
与&student[i]意义相同。
若p指向数组的某一个元素,则p++就指向其后续元素。
3)指针的数组表示法
若p=student,我们说指针p指向数组student,p[i]表示数组的第i个元素,其效果与
student[i]等同。对数组成员的引用描述为:p[i].name、p[i].num等。
//程序示例:
structdata/*定义结构体类型*/
&&&& intday,month,
structstu/*定义结构体类型*/
&&&& char name[20];
&&&& structstu*p,student[4]=
&&&&&&&& {"liying",1,},
&&&&&&&& {"wangping",2,},
&&&&&&&& {"libo",3,},
&&&&&&&& {"xuyan",4,}
/*定义结构体数组并初始化*/
&&&& p=/*将数组的首地址赋值给指针p,p指向了一维数组student*/
&&&& printf("\n1----Outputname,number,year,month,day\n");
&&&& for(i=0;i&4;i++)/*采用指针法输出数组元素的各成员*/
&&&& printf("%20s%10ld%10d//%d//%d\n",(p+i)-&name,(p+i)-&num,
&&&& (p+i)-&birthday.year,(p+i)-&birthday.month,
&&&& (p+i)-&birthday.day);
//结构指针变量作函数参数
#include&stdio.h&
struct stu
&&&& char *
&&&& char ***;
&&&&&{101,"Li ping",'M',45},
&&&& {102,"Zhang ping",'M',62.5},
&&&& {103,"He fang",'F',92.5},
&&&& {104,"Cheng ling",'F',87},
&&&& {105,"Wang ming",'M',58},
&&&& struct stu *
&&&& void ave(struct stu *ps);
&&&& ave(ps);
void ave(struct stu *ps)
&&&& int c=0,i;
&&&& float ave,s=0;
&&&& for(i=0;i&5;i++,ps++)
&&&&&&&&&& s+=ps-&
&&&&&&&&&& if(ps-&score&60) c+=1;
&&&& printf("s=%f\n",s);
&&&& ave=s/5;
&&&& printf("average=%f\ncount=%d\n",ave,c);
测试环境:VC++6.0
输出结果:
s=345.000000
average=69.000000
Press any key to continue
阅读(16855) | 评论(1) | 转发(4) |
相关热门文章
给主人留下些什么吧!~~
为什么不给malloc强制转化为stu?struct&stu&*&定/*义结构体类型指针*/&&&&&&student=malloc(sizeof(struct&stu));&为/指*&针变量分配安全的地址*/
请登录后评论。}

我要回帖

更多关于 结构体指针参数传递 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信