I fixed it!
我修好了它!
typedef struct student
{
char id[11];
}Student;
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv){
int input = 0, i = 0;
FILE * fp = fopen("student.txt", "wt");
if (fp == NULL) {
printf("Error to open student.txt");
return -1;
}
scanf("%d",&input);
student = (Student *)malloc(input*sizeof(Student));
for(i=0;i<input;i++){
strcpy(student[i].id, "a"); // A is just for default.
fprintf(fp,"%s\n",student[i].id);
}
fclose(fp);
return 0;
}
I've fixed from comments and kind answers. And it does work
我已经修改了评论和善意的答案。它确实有效
Thanks for help
感谢帮助
I really appreciated it! It was really useful.
我真的很感激!这真的很有用。
3 个解决方案
#1
5
A couple of notes:
几个笔记:
With:
附:
for (i=0; i<input; i++)
student = ...
You are repeatedly overriding the value of student
.
你反复压倒学生的价值。
It's pretty much like doing:
这非常像:
x = 5;
x = 6;
x = 7;
...
Not only that, but during the process you are also forcing numerous memory leaks upon yourself.
不仅如此,在此过程中,您还会在自己身上造成大量内存泄漏。
With student = malloc(...)
, you are allocating a new memory block at each iteration, and then setting this variable to point to that memory block while "forgetting" the previous one (which you will no longer be able to deallocate).
使用student = malloc(...),您将在每次迭代时分配一个新的内存块,然后将此变量设置为指向该内存块,同时“忘记”前一个(您将无法再解除分配) 。
#2
1
This id[0] = ""
doesn't make any sense. Your compiler should give a compiler error for this or it is misconfigured/broken.
这个id [0] =“”没有任何意义。您的编译器应该为此提供编译器错误,或者它配置错误/损坏。
To assign a value to a string, use strcpy(student[i].id, "")
.
要为字符串赋值,请使用strcpy(student [i] .id,“”)。
Also, as somebody pointed out, the malloc should not be inside a loop. Just drop the first for loop and use one single malloc call. And don't cast the result from malloc, because doing so is pointless.
另外,正如有人指出的那样,malloc不应该在循环中。只需删除第一个for循环并使用一个malloc调用。并且不要从malloc转换结果,因为这样做是没有意义的。
Student* student = malloc( sizeof(Student[input]) );
At the end of the program (when school's out) add free(student)
.
在课程结束时(当学校外出时)添加免费(学生)。
#3
1
Not quite sure what you are asking, but maybe you need this:
不太确定你在问什么,但也许你需要这个:
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
char id[11];
} Student;
int main(int argc, char **argv){
int nbstudents = 0;
int i;
printf ("How many students do you want: ");
scanf("%d",&nbstudents);
Student *studentarray = malloc(nbstudents * sizeof(Student));
for(i = 0; i < nbstudents; i++)
{
printf ("Enter student id %d: ", i);
scanf("%s", studentarray[i].id);
}
printf ("Students ids entered:\n");
for(i = 0; i < nbstudents; i++)
{
printf ("Student id %d: %s\n", i, studentarray[i].id);
}
free(studentarray);
return 0;
}
Ths student
structure should probably contain other fields than id
, for example the name of the student.
学生结构应该包含除id以外的其他字段,例如学生的姓名。
#1
5
A couple of notes:
几个笔记:
With:
附:
for (i=0; i<input; i++)
student = ...
You are repeatedly overriding the value of student
.
你反复压倒学生的价值。
It's pretty much like doing:
这非常像:
x = 5;
x = 6;
x = 7;
...
Not only that, but during the process you are also forcing numerous memory leaks upon yourself.
不仅如此,在此过程中,您还会在自己身上造成大量内存泄漏。
With student = malloc(...)
, you are allocating a new memory block at each iteration, and then setting this variable to point to that memory block while "forgetting" the previous one (which you will no longer be able to deallocate).
使用student = malloc(...),您将在每次迭代时分配一个新的内存块,然后将此变量设置为指向该内存块,同时“忘记”前一个(您将无法再解除分配) 。
#2
1
This id[0] = ""
doesn't make any sense. Your compiler should give a compiler error for this or it is misconfigured/broken.
这个id [0] =“”没有任何意义。您的编译器应该为此提供编译器错误,或者它配置错误/损坏。
To assign a value to a string, use strcpy(student[i].id, "")
.
要为字符串赋值,请使用strcpy(student [i] .id,“”)。
Also, as somebody pointed out, the malloc should not be inside a loop. Just drop the first for loop and use one single malloc call. And don't cast the result from malloc, because doing so is pointless.
另外,正如有人指出的那样,malloc不应该在循环中。只需删除第一个for循环并使用一个malloc调用。并且不要从malloc转换结果,因为这样做是没有意义的。
Student* student = malloc( sizeof(Student[input]) );
At the end of the program (when school's out) add free(student)
.
在课程结束时(当学校外出时)添加免费(学生)。
#3
1
Not quite sure what you are asking, but maybe you need this:
不太确定你在问什么,但也许你需要这个:
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
char id[11];
} Student;
int main(int argc, char **argv){
int nbstudents = 0;
int i;
printf ("How many students do you want: ");
scanf("%d",&nbstudents);
Student *studentarray = malloc(nbstudents * sizeof(Student));
for(i = 0; i < nbstudents; i++)
{
printf ("Enter student id %d: ", i);
scanf("%s", studentarray[i].id);
}
printf ("Students ids entered:\n");
for(i = 0; i < nbstudents; i++)
{
printf ("Student id %d: %s\n", i, studentarray[i].id);
}
free(studentarray);
return 0;
}
Ths student
structure should probably contain other fields than id
, for example the name of the student.
学生结构应该包含除id以外的其他字段,例如学生的姓名。