#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);
}
改了下,scanf里的&去掉了,试试看,还有加了个主函数
#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);
}