#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
typedef int status;
typedef struct{
int a[3];
}triplet;
status inittriplet(triplet*e,int v1,int v2,int v3)
{
e=(triplet*)malloc(sizeof(triplet));
if(!e) return ERROR;
e->a[0]=v1;
e->a[1]=v2;
e->a[2]=v3;
return OK;
}
status delete(triplet*a){
if (!a)
{
return ERROR;
}
free (a);
return OK;
}
status get(triplet*e,int b,int c){
if(!e&&1>b&&b>3) return ERROR;
c=e->a[b-1];
return OK;
}
status put(triplet*e,int b,int c){
if (!e&&b<1&&b>3) return ERROR;
e->a[b-1]=c;
return OK;
}
status isascending(triplet*e){
return(e->a[0]<e->a[1]&&e->a[1]<e->a[2]);
}
status max(triplet*e,int b){
b=(e->a[0]>e->a[1])?((e->a[0]>e->a[2])?e->a[0]:e->a[2]):((e->a[1]>e->a[2])?e->a[1]:e->a[2]);
return OK;
}
int main(void)
{
triplet*e;
int b,c,i;
inittriplet(e,1,2,4);
get(e,2,c);
printf("%d",c);
put(e,2,5);
i=0;
while(i<3)
{
printf("%d\t",e->a[i]);
i++;
}
printf("\n");
max(e,c);
printf("%d\n",c);
if(isascending(e))
for(int k=0;k<3;k++)
printf("%d\t",e->a[i]);
system("pause");
return 0;
}
为什么函数inittriplet执行完后结构体中的数组a的值是乱的
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- qzjhjxj 2021-07-04 22:35关注
修改如下,问题见注释,供参考:
#include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 typedef int status; typedef struct{ int a[3]; }triplet; status inittriplet(triplet *&e,int v1,int v2,int v3)//status inittriplet(triplet*e,int v1,int v2,int v3) { e=(triplet*)malloc(sizeof(triplet)); if(!e) return ERROR; e->a[0]=v1; e->a[1]=v2; e->a[2]=v3; return OK; } status del(triplet*a){ //status delete(triplet*a) if (!a) { return ERROR; } free (a); return OK; } status get(triplet*e,int b,int &c){//status get(triplet*e,int b,int c) if(!e&&1>b&&b>3) return ERROR; c=e->a[b-1]; return OK; } status put(triplet*e,int b,int c){ if (!e&&b<1&&b>3) return ERROR; e->a[b-1]=c; return OK; } status isascending(triplet*e){ return(e->a[0]<e->a[1]&&e->a[1]<e->a[2]); } status max(triplet*e,int &b){ //status max(triplet*e,int b) b=(e->a[0]>e->a[1])?((e->a[0]>e->a[2])?e->a[0]:e->a[2]):((e->a[1]>e->a[2])?e->a[1]:e->a[2]); return OK; } int main(void) { triplet*e; int b,c,i; inittriplet(e,1,2,4); get(e,2,c); printf("%d\n",c); //printf("%d",c); put(e,2,5); i=0; while(i<3) { printf("%d\t",e->a[i]); i++; } printf("\n"); max(e,c); printf("%d\n",c); if(isascending(e)) for(int k=0;k<3;k++) printf("%d\t",e->a[k]);//printf("%d\t",e->a[i]); system("pause"); return 0; } //输出: //2 //1 5 4 //5 //请按任意键继续. . .
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
- ¥15 这种微信登录授权 谁可以做啊
- ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
- ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
- ¥15 网络设备配置与管理这个该怎么弄
- ¥20 机器学习能否像多层线性模型一样处理嵌套数据
- ¥20 西门子S7-Graph,S7-300,梯形图
- ¥50 用易语言http 访问不了网页
- ¥50 safari浏览器fetch提交数据后数据丢失问题
- ¥15 matlab不知道怎么改,求解答!!