#include <iostream>
#include <stdlib.h> /*使用malloc( ) 需要*/
struct data /*定義結構體*/
{ int day,month,year;
};
typedef struct /*定義結構體*/
{ char name[20];
long num;
struct data birthday; /*嵌套的結構體型別成員*/
}stu;
int main(int argc, char** argv) {
stu *student1;
stu student2;
student1=(stu *) malloc(10*sizeof(char));
// studnet2=malloc(sizeof(struct stu));
printf("Input name,num,year,month,day:/n");
scanf("%s",student1->name);
scanf("%ld",&student1->num);
scanf("%d%d%d",&student1->birthday.year,&student1->birthday.month,&student1->birthday.day);
printf("/nOutputname,number,year,month,day/n");
printf("%20s%10ld%10d//%d//%d/n",
student2.name,
student2.num,
student2.birthday.year,
student2.birthday.month,
student2.birthday.day);
return 0;
}
uj5u.com熱心網友回復:
加上頭檔案#inlcude <stdio.h>
uj5u.com熱心網友回復:
#include <iostream>
#include <stdlib.h> /*使用malloc( ) 需要*/
#include <stdio.h> //或<cstdio>
using namespace std;
struct data /*定義結構體*/
{
int day,month,year;
};
typedef struct /*定義結構體*/
{ char name[20];
long num;
struct data birthday; /*嵌套的結構體型別成員*/
}stu;
int main(int argc, char** argv) {
stu *student1;
stu student2;
//student1=(stu *) malloc(10*sizeof(char));
student1=(stu *) malloc(10*sizeof(char)); //這里申請的空間不足,用下面這句比較合適
// studnet1=malloc(sizeof(struct stu));
// studnet2=malloc(sizeof(struct stu));
printf("Input name,num,year,month,day:/n");
scanf("%s",student1->name); //這里已經越界了
scanf("%ld",&student1->num);
scanf("%d%d%d",&student1->birthday.year,&student1->birthday.month,&student1->birthday.day);
printf("/nOutputname,number,year,month,day/n");
printf("%20s%10ld%10d//%d//%d/n",
student2.name,
student2.num,
student2.birthday.year,
student2.birthday.month,
student2.birthday.day);
return 0;
}
供參考~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/76394.html
標籤:C語言
