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

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