I'm trying to declare a variable (struct typed) in a function, and manipulate it (read/write) from other functions. However, when I try to use that variable in any function that is not where I declared it, it contains only garbage.
This is my code:
#include <stdio.h>
#include <stdlib.h>
typedef struct
char name[25];
int roll;
float marks;
}Student;
void getInfo(Student student);
void display(Student student);
int main(int argc, char *argv[]) {
Student student;
getInfo(student);
display(student);
return 0;
}
void getInfo(Student student){
printf("Enter student's name: \n");
scanf("%s", &student.name);
printf("Enter student's roll: \n");
scanf("%d", &student.roll);
printf("Enter student's grade: \n");
scanf("%f", &student.marks);
}
void display(Student student){
printf("NAME: %s\n", student.name);
printf("ROLL: %d\n", student.roll);
printf("GRADE: %.2f\n", student.marks);
}
struct. The copy that is passed to the input function does not change the original, and so garbage is printed.