I have a problem with strcpy in C. My Code:
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;
}
and my main check.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;
}
The Problem is the printf statement returns TestABC instead of just Test. I just dont get it why. I just want the name not the name and the grades togehter in my printf statement. Can anyone help?
Studentstruct and ask yourself where you're storing those strings. I don't see any arrays, do you?