Where: ssize is the input size of the array, student is a globally declared struct, and read_stud is the function i use to read in arrays.
when printed out, s[count].first gives me the correct first name, but s[0] and all values after that are ALSO the same first name instead of the previously inputted value. It’s changing the struct values to my most recent input!
struct student //this struct is global
{
int number;
char *first ;
char *last ;
};
struct student read_stud (int number, char *first , char *last) //my read_stud function
{
struct student s;
s.number = number;
s.first = first;
s.last = last;
return s;
}
This is in int main, where I use the function s = calloc(ssize, sizeof (struct student));
for (count = 0; count < ssize ; ++count)
{
printf ("Enter student number, name and last name: \n");
scanf ("%d %s %s", &number, &first, &last);
s[count] = read_stud (number, first, last);
}
A most heartfelt thanks to whoever can fix this for me!
You need to provide storage for the first and last names in your
studentstruct.If you know what their max lengths are, use char arrays in the struct.
Otherwise, malloc the strings and remember to free them later.