字符指針strcpy的問題,我有一個問題,在strcpy的C.我的代碼:在C
student.h
#include <stdlib.h>
#include <string.h>
typedef struct {
char *name; /**< char pointer to name of Student */
char *grades; /**< char pointer to grades of labs */
float mark; /**< float as mark of labs */
} Student;
Student *new_student(char *, char *);
student.c
include "student.h"
Student *new_student(char *name, char *grades) {
if (name == NULL || strlen(name) == 0) return NULL;
char *marks = "";
//if (grades == NULL) grades = "";
if(grades == NULL){
marks= "";
}
else{
marks= grades;
}
Student *test;
test = (Student*) malloc(sizeof(Student));
(void)strcpy(&test->name, name);
(void)strcpy(&test->grades, noten);
return test;
}
和我的主要檢查。c
#include <stdlib.h>
#include "student.h"
int main() {
Student *s;
s = new_student("Test", "ABC");
printf("%s",&s->name);
/*(void)test_student(0, NULL);*/
return EXIT_SUCCESS;
}
問題是printf語句返回TestABC而不是Test。我只是不明白爲什麼。我只想在我的printf語句中使用名稱而不是名稱和成績。誰能幫忙?
看看你'Student'結構和問問你自己在哪裏存儲這些字符串。我沒有看到任何陣列,是嗎? –