『暮雨孤城』26 2021-12-02 11:03 采纳率: 33.3%
浏览 21

不知道哪错了,help me

//输入三个整数,按由小到大的顺序输出
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void sortInt(int p[]);
int main()
{
int
date[3]; //定义一个指针型数组
int i;
printf("piease scanf three numbers:");
for(i=0;i<3;i++)
{
date[i]=(int*)malloc(3);
scanf("%d\n",*date[i]);
}

sortInt(date);//定义的函数,需要输入其地址。
printf("%d%d%d\n",date[0],date[1],date[2]);
for(i=0;i<3;i++)
{
    free(date[i]);
    date[i]=0;
}
return 0;

}
void sortInt(int *p[])
{

int *temp;
if(p[0]>p[1])
{
    temp=p[0];
    p[0]=p[1];
    p[1]=temp;
}
if(p[0]>p[2])
{
    temp=p[0];
    p[0]=p[2];
    p[2]=temp;
}
if(p[1]>p[2])
{
    temp=p[1];
    p[1]=p[2];
    p[2]=temp;
}

}

  • 写回答

1条回答 默认 最新

  • 陆宇杭 2021-12-02 11:42
    关注

    1、确定了数组的大小了,不需要malloc和free了
    2.scanf里面应该是&date[i]
    3、几个地方不需要加*,建议再去了解一下指针的含义

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    void sortInt(int *p);
    int main()
    {
        int date[3]; //定义一个指针型数组
        int i;
        printf("piease scanf three numbers:");
        for (i = 0;i < 3;i++)
        {
            scanf("%d\n", &date[i]);
        }
        sortInt(date);//定义的函数,需要输入其地址。
        printf("%d %d %d\n", date[0], date[1], date[2]);
        return 0;
    }
    void sortInt(int * p)
    {
        int temp;
        if(p[0]>p[1])
        {
            temp=p[0];
            p[0]=p[1];
            p[1]=temp;
        }
        if(p[0]>p[2])
        {
            temp=p[0];
            p[0]=p[2];
            p[2]=temp;
        }
        if(p[1]>p[2])
        {
            temp=p[1];
            p[1]=p[2];
            p[2]=temp;
        }
    }
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 12月2日

悬赏问题

  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题
  • ¥20 基于matlab的航迹融合 航迹关联 航迹插补
  • ¥15 用Matlab实现图中的光线追迹
  • ¥15 联想笔记本开机出现系统更新界面