[英]How to fix my code to read a string in a function that returns a pointer to a struct
[英]How to fix my code to return a pointer to a struct from a function
我现在正在学习指针和结构,我有点困惑。 特别是,我想制作一个 function ,它返回一个指向结构的指针。 我的代码正在编译,但它不工作。 在我输入学生人数后,它会询问姓名和年龄(不读姓名),在我输入年龄后,它会关闭。
#include <stdio.h>
#include <stdlib.h>
struct details
{
char name[100];
int age;
};
struct details * details_pointer(int n)
{
struct details pointer_x[n];
struct details *pointer = pointer_x;
for (int i=0; i<n; i++)
{
printf("Student %d:\n", i);
printf("name:\n");
fgets(pointer[i].name, 100, stdin);
printf("age:\n");
scanf("%d", pointer[i]. age);
}
return pointer;
}
int main()
{
int n;
printf("Type the number of persons:\n");
scanf("%d", &n);
struct details *student = details_pointer(n);
printf("\nName: %s\n", (*student).name);
printf("Age: %d\n", (*student).age);
system("pause");
return 0;
}
这里的问题是您返回的结构变量 ( pointer_x
) 在内部details_pointer()
function 的堆栈上本地分配,但是这个 memory 一旦返回就不再为您保留。 这意味着您(充其量)得到垃圾。
您必须在function中分配 memory 并将其返回(并记住释放它。)或将数据传递给 function 以填充它。
void get_details(int n, struct details p[n])
{
for (int i = 0; i < n; i++)
{
// do stuff with p[i]
}
}
int main()
{
...
scanf("%d", &n);
struct details students[n];
get_details(n, students);
printf("\nName: %s\n", students[0].name);
...
}
我通常更喜欢在可能的情况下将数据传递给function,因为它简化了 memory 管理。
编辑:关于您现有的details_pointer()
function 的两个注释。
1) 在fgets(pointer[i].name, 100, stdin)
中,如果已知数组的大小,最好从数组的大小中导出字节数; 在这种情况下,所以推荐fgets(pointer[i].name, sizeof(pointer[i].name), stdin)
。 您的并没有错,但是如果将来尺寸发生变化,维护起来会更容易。
2) scanf("%d", pointer[i].age)
需要获取.age
的地址来填充它的值; 您传递的是数字而不是地址,这肯定是不正确的。
好的,所以如果分配需要动态分配,那么这就是我们必须做的。
仅指定一个数组会在 function 运行时隐式分配空间,但当 function 返回时它会消失。 您需要专门分配它。
重新访问您的代码:
struct details *details_pointer(int n)
{
struct details *students = (struct details *)malloc(n * sizeof(struct details));
.... do stuff with students[0] and the like
return students;
}
这使用malloc()
- memory allocate - library function 为您获取一些空间,并且在您释放它之前它一直有效。 字节数是你想要多少东西( n
)乘以一件东西的字节数( sizeof(struct details)
)
之后:
int main()
{
struct details *student = details_pointer(n);
.. do stuff
free(student);
}
现在使用memory,并在完成后将其释放回操作系统。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.