m0_62267751 2022-01-07 08:46 采纳率: 85.7%
浏览 28
已结题

C语言不知道问题出在哪里,代码和问题如下

#include <stdio.h>
#include <stdlib.h>
struct ex
{
 int i;
 float j;
 char *s;
};
void func (void)
{
struct ex *p = malloc(sizeof(struct ex));
p->s = malloc(20 * sizeof(char));
scanf("%s", &p->s);
printf("%s", p->s);
free(p);
}

img

  • 写回答

2条回答 默认 最新

  • fuill 2022-01-07 09:58
    关注

    改了下,scanf里的&去掉了,试试看,还有加了个主函数

    img

    #include <stdio.h>
    #include <stdlib.h>
    struct ex
    {
        int i;
        float j;
        char *s;
    };
    void func (void)
    {
        struct ex *p =malloc(sizeof(struct ex));
        p->s = (char*)malloc(20 * sizeof(char));
        scanf("%s", p->s);
        printf("%s", p->s);
        free(p);
    }
    int main()
    {
        func();
       return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月15日
  • 已采纳回答 1月7日
  • 创建了问题 1月7日