连续定义连个字符指针:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student
{
char * id;
char * name;
int mark;
}student;
main()
{
student * st = (student*)malloc(sizeof(student));
st->id =(char*)malloc (sizeof (char)*20);//分配空间
st->name=(char*)malloc (sizeof (char)*20);//分配空间
/*printf("ID address: %d", st->id);
printf("Name address: %d", st->name);
printf("Name address: %d", &st->mark);*/
printf("学号:");
scanf("%s", st->id);
printf("%s\n",st->id);
printf("姓名:");
scanf("%s",st->name);
printf("学号:%s 姓名:%s\n",st->id, st->name);
}
如果我想知道这个结构体占用多长?(下面程序错误,为什么st-td == 0????)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student
{
char * id;
char * name;
// int mark;
}student;
char *fname = "liujie";
char *no = "201530310059";
main()
{
student * st =(student*)malloc(sizeof(student));
st->id =(char*)malloc (strlen(no));//分配空间
st->name=(char*)malloc (strlen(fname));//分配空间
printf("strlen(fname)= %d\n", strlen(fname)) ;
printf("strlen(no)= %d\n", strlen(no)) ;
/*printf("ID address: %d", st->id);
printf("Name address: %d", st->name);
printf("Name address: %d", &st->mark);*/
// student *ss;
printf("strlen(st->id)= %d\n", strlen(st->id)) ;
printf("结构体总长度 = %d\n", strlen(st->id)+strlen(st->name)+sizeof(int));
printf("学号:");
scanf("%s", st->id);
printf("%s\n",st->id);
printf("姓名:");
scanf("%s",st->name);
printf("学号:%s 姓名:%s\n",st->id, st->name);
}