S. wrote:
> Hi all,
>
> Can someone please help me with this?
>
> I have the following struct:
> typedef struct {
> char *name;
> int age;
> } Student;
>
>
> I have the following prototype declaration:
> void addStudent(Student *arr_students);
>
> In my main, I want to have an array called 'students' which is
> composed of the Student struct. I then want to pass a function that
> will allow me to create a Student record using pass-by-reference:
> int main() {
>
> Student students[CLASSSIZE]; /* array of student
> struct's? */
>
> addStudent(&students[0]);
> printf("%s",&students[0].name);
> return 0;
> }
>
> The following is my function to add a student:
> void addStudent(Student *arr_student) {
> char *name;
>
> printf("\nEnter student name: ");
> scanf("%s",&name);
> *arr_student[0].name = name;
>
> return;
> }
>
> I guess I have two questions before everyone dives into how wrong I
> have done everything.
> Q1. When I compile this I get the following error, could someone
> please help explain why this occurs and how I can fix my code to
> prevent this warning?
> "test.c", line 25: warning: improper pointer/integer
> combination: op "="
> Q2. When I run my program, I am prompted with "Enter student name: "
> and I enter in a name, I then receive the following error, can someone
> please explain why this is happening too?
> "Segmentation Fault"
>
> I have a feeling I have declared my array of Student struct wrong and
> I am also not passing the array through to the function correctly
> which is causing both my problems. I have searched all over the
> internet and in this discussion group but can't find an example
> related to what I am trying to do.
>
> Any help would be appreciated.
>
> Kind regards,
> S.
>
> Below is my program:
> #include <stdio.h>
> #define CLASSSIZE 10
>
> typedef struct {
> char *name;
name is declared as a pointer to a char (or char array).
> int age;
> } Student;
>
> void addStudent(Student *arr_students);
>
> int main() {
>
> Student students[CLASSSIZE]; /* array of student struct's?*/
>
> addStudent(&students[3]);
> printf("%s",&students[3].name);
> return 0;
> }
>
> void addStudent(Student *arr_student) {
> char *name;
name here is a local variable that can hold a pointer to a character or
character array. Only the size of a pointer is allocated to it. It is not
pointing at anything at this point, some random bit of memory maybe, or
maybe 0
> printf("\nEnter student name: ");
> scanf("%s",&name);
You are now trying to store the characters entered at the location that name
is pointing to... but, name is still pointing into lala land. No memory has
actually been allocated to hold the characters entered.
> *arr_student[3].name = name;
>
> return;
> }
Now, you can, to fix this in your current design, malloc memory in
addStudent to hold the student name and point the structure name to it, but
thinknig about it, you're going to have to set some size. 10? 20? 30?
Whatever it is, it probably won't be big enough. Anyway, why go through all
this trouble because then you have to free the memory when your program is
done. You might as well just allocate it in your structure in the first
place.
typedef struct {
char name[30];
int age;
} Student;
now you can just scanf("%s", arr_student->name )
--
Jim Langston