S
S.
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;
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;
printf("\nEnter student name: ");
scanf("%s",&name);
*arr_student[3].name = name;
return;
}
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;
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;
printf("\nEnter student name: ");
scanf("%s",&name);
*arr_student[3].name = name;
return;
}