I am not sure where am I going wrong with the following structure in C. Can you guys let me know What am I doing wrong and what would be the best approach.
我不知道我在C中的下列结构出了什么问题。你能让我知道我做错了什么,最好的方法是什么。
#include<stdio.h>
#include<string.h>
typedef struct
{
char *name;
float gpa;
int courseNo;
} STUDENT;
void createStudent(STUDENT s, char *n, float gpa, int course);
int main(void)
{
struct STUDENT s;
createStudent(s, "Dummy", 3.8f, 203);
printf("Name = %s\n", s.name);
printf("GPA = %3.1f\n", s.gpa);
printf("Course No. = %d\n", s.courseNo);
return 0;
}
void createStudent(STUDENT s, char *n, float gpa, int course)
{
strcpy(s.name, n);
s.gpa = gpa;
s.courseNo = course;
}
2 个解决方案
#1
1
Try this instead:
试试这个:
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
typedef struct
{
char *name;
float gpa;
int courseNo;
} STUDENT;
void createStudent(STUDENT* s, char *n, float gpa, int course);
int main(void)
{
STUDENT s;
createStudent(&s, "Dummy", 3.8f, 203);
printf("Name = %s\n", s.name);
printf("GPA = %3.1f\n", s.gpa);
printf("Course No. = %d\n", s.courseNo);
return 0;
}
void createStudent(STUDENT* s, char *n, float gpa, int course)
{
s->name = malloc((strlen(n)+1) * sizeof(char));
strcpy(s->name, n);
s->gpa = gpa;
s->courseNo = course;
}
#2
4
You are passing the STUDENT
object by value, not by reference, so any changes that you make to it in your createStudent
function do not effect s
. Pass a pointer instead.
您是按值传递STUDENT对象,而不是通过引用传递,因此您在createStudent函数中对其进行的任何更改都不会影响s。改为传递指针。
Normally whenever you pass a variable to a function, the variable's value is copied into the appropriate argument. This argument variable, however, is a separate variable from the one you passed however. By using pointers, you are essentially telling the function the identity of your variable, rather than just what value it holds.
通常,每当将变量传递给函数时,变量的值都会复制到相应的参数中。但是,此参数变量是与您传递的变量不同的变量。通过使用指针,您实际上是在告诉函数变量的标识,而不是它所拥有的值。
As @simonc pointed out, there is one more problem in your code. When you call strcpy
in your function, the pointer you are writing to (s.name
) is unitialized, meaning that when you assign to its data via strcpy
you are overwriting random memory, which is just asking for problems. You should allocate the memory with malloc
before writing to it.
正如@simonc指出的那样,代码中还有一个问题。当您在函数中调用strcpy时,您正在写入的指针(s.name)是单元化的,这意味着当您通过strcpy分配给它的数据时,您将覆盖随机内存,这只是在寻找问题。在写入内存之前,您应该使用malloc分配内存。
Here is the completed code:
这是完成的代码:
void createStudent(STUDENT *s, char *n, float gpa, int course)
{
s->name = malloc(strlen(n) + 1); // +1 for null terminator
strcpy(s->name, n);
s->gpa = gpa;
s->courseNo = course;
}
And then call it like this:
然后像这样调用它:
createStudent (&s, other args ... )
Note that you should deallocate the memory when you are done like so:
请注意,完成后应该取消分配内存:
free(s.name);
but only when you don't plan on using the object any more.
但只有当你不打算再使用这个对象时。
#1
1
Try this instead:
试试这个:
#include <stdio.h>
#include<string.h>
#include <stdlib.h>
typedef struct
{
char *name;
float gpa;
int courseNo;
} STUDENT;
void createStudent(STUDENT* s, char *n, float gpa, int course);
int main(void)
{
STUDENT s;
createStudent(&s, "Dummy", 3.8f, 203);
printf("Name = %s\n", s.name);
printf("GPA = %3.1f\n", s.gpa);
printf("Course No. = %d\n", s.courseNo);
return 0;
}
void createStudent(STUDENT* s, char *n, float gpa, int course)
{
s->name = malloc((strlen(n)+1) * sizeof(char));
strcpy(s->name, n);
s->gpa = gpa;
s->courseNo = course;
}
#2
4
You are passing the STUDENT
object by value, not by reference, so any changes that you make to it in your createStudent
function do not effect s
. Pass a pointer instead.
您是按值传递STUDENT对象,而不是通过引用传递,因此您在createStudent函数中对其进行的任何更改都不会影响s。改为传递指针。
Normally whenever you pass a variable to a function, the variable's value is copied into the appropriate argument. This argument variable, however, is a separate variable from the one you passed however. By using pointers, you are essentially telling the function the identity of your variable, rather than just what value it holds.
通常,每当将变量传递给函数时,变量的值都会复制到相应的参数中。但是,此参数变量是与您传递的变量不同的变量。通过使用指针,您实际上是在告诉函数变量的标识,而不是它所拥有的值。
As @simonc pointed out, there is one more problem in your code. When you call strcpy
in your function, the pointer you are writing to (s.name
) is unitialized, meaning that when you assign to its data via strcpy
you are overwriting random memory, which is just asking for problems. You should allocate the memory with malloc
before writing to it.
正如@simonc指出的那样,代码中还有一个问题。当您在函数中调用strcpy时,您正在写入的指针(s.name)是单元化的,这意味着当您通过strcpy分配给它的数据时,您将覆盖随机内存,这只是在寻找问题。在写入内存之前,您应该使用malloc分配内存。
Here is the completed code:
这是完成的代码:
void createStudent(STUDENT *s, char *n, float gpa, int course)
{
s->name = malloc(strlen(n) + 1); // +1 for null terminator
strcpy(s->name, n);
s->gpa = gpa;
s->courseNo = course;
}
And then call it like this:
然后像这样调用它:
createStudent (&s, other args ... )
Note that you should deallocate the memory when you are done like so:
请注意,完成后应该取消分配内存:
free(s.name);
but only when you don't plan on using the object any more.
但只有当你不打算再使用这个对象时。