存取权限指针这是我的代码:如何在结构
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct pers
{
int age;
char *name;
float height;
}Person;
void read(Person p[], int *nr) // an array of persons, and nr is the number of persons
{
scanf("%d", nr);
int i;
for(i = 0; i < *nr; i++)
{
scanf("%d", &p[i].age);
p[i].name = calloc(100, sizeof(char));
scanf("%s", p[i].name);
scanf("%f", &p[i].height);
}
}
int main()
{
int nr;
Person p[100];
int i;
read(p, &nr);
for(i = 0; i < nr; i++)
{
printf("%d\n", p[i].age);
printf("%s\n", p[i].name);
printf("%f\n", p[i].height);
}
return 0;
}
我不知道为什么,这不是很好stocate数据。我如何使用指针或其他任何东西,以获得访问年龄和身高,还*的名字
如果我只尝试与年龄和身高的代码工作,但是当我添加名称,它stucks。
这是我输入
3 // numbers of persons
23 // age
John Smith //firstname + lastname //1person
182.5 //height
18
Mat Plow //2person
152.6
56
Alex Grim //3person
172
任何想法?
我想我可能用得到,并与一些指针,但我不知道如何:d
谢谢!
你写'#include'吗?也显示你的意见。 –
BLUEPIXY
2014-12-07 12:04:34