请有人告诉我为什么程序在进入循环的第一个元素后卡住了?
please can someone tell me why the program get stuck after entering the first element of the loop?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxlong 20
typedef struct {
char name[maxlong];
char surname[maxlong];
} personne;
//writing the code to enter persons name and surname
//.ffffffffffffffffffffffffffffffff
personne enterPersonne() {
personne r;
printf("write the surname: ");
scanf("%s",&r.name);
printf("write the name : ");
scanf("%s",&r.surname);
return r;
}
int main (void)
{
int dimension,i;
personne *pers;
printf("enter the persons count : ");
scanf("%d",&dimension);
for(i=0; i<dimension; i++)
*(pers+i) = enterPersonne();
return 0;
}
就在 main
中的 for
循环之前,您需要分配一个内存缓冲区来保存 pers
数组的内容。
执行此操作的常规方法是:
pers = (personne *)malloc(dimension * sizeof(personne));
if ( pers == NULL ) {
// add code here to handle an out-of-memory condition
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxlong 20
typedef struct {
char name[maxlong];
char surname[maxlong];
} personne;
//writing the code to enter persons name and surname
//.ffffffffffffffffffffffffffffffff
personne enterPersonne() {
personne r;
printf("write the surname: ");
scanf("%s",&r.name);
printf("write the name : ");
scanf("%s",&r.surname);
return r;
}
int main (void)
{
int dimension,i;
personne *pers;
printf("enter the persons count : ");
scanf("%d",&dimension);
for(i=0; i<dimension; i++)
*(pers+i) = enterPersonne();
return 0;
}
就在 main
中的 for
循环之前,您需要分配一个内存缓冲区来保存 pers
数组的内容。
执行此操作的常规方法是:
pers = (personne *)malloc(dimension * sizeof(personne));
if ( pers == NULL ) {
// add code here to handle an out-of-memory condition
}