S
S.
Hi all,
I have the requirement that I must pass-by-reference to my function
addStudent() and getAge() functions where my getAge() function is
within the addStudent() function. I am able to pass-by-reference to
the first function, addStudent() but then I am confused as to how I am
suppose to pass the pointer of 'student' from within the addStudent()
function to the getAge() function that is nested within the
addStudent() function.
My code below does not compile correctly with the inclusion of the
getAge() function and I receive the following compile warning:
"test2.c", line 30: warning: left operand of "->" must be pointer
to struct/union
where line 30 is:
scanf("%d", student->age);
I understand this is not the "simplest" way to achieve the inputs for
the student's name and age but it is my requirement to have the nested
functions and to pass variables by reference. Thanks for your help in
advance.
S.
#include <stdio.h>
#define NAME_SIZE 20
#define CLASS_SIZE 10
typedef struct {
char name[20];
int age;
} Student;
void addStudent(Student *student);
void getAge(Student **student);
void main() {
Student students[CLASS_SIZE];
addStudent(&students[3]);
return;
}
/* Pass-by-reference */
void addStudent(Student *student) {
printf("\nStudent name: ");
scanf("%s", student->name);
printf("\nStudent age: ");
getAge(&student);
return;
}
/* Pass-by-reference */
void getAge(Student **student) {
scanf("%d", student->age);
return;
}
I have the requirement that I must pass-by-reference to my function
addStudent() and getAge() functions where my getAge() function is
within the addStudent() function. I am able to pass-by-reference to
the first function, addStudent() but then I am confused as to how I am
suppose to pass the pointer of 'student' from within the addStudent()
function to the getAge() function that is nested within the
addStudent() function.
My code below does not compile correctly with the inclusion of the
getAge() function and I receive the following compile warning:
"test2.c", line 30: warning: left operand of "->" must be pointer
to struct/union
where line 30 is:
scanf("%d", student->age);
I understand this is not the "simplest" way to achieve the inputs for
the student's name and age but it is my requirement to have the nested
functions and to pass variables by reference. Thanks for your help in
advance.
S.
#include <stdio.h>
#define NAME_SIZE 20
#define CLASS_SIZE 10
typedef struct {
char name[20];
int age;
} Student;
void addStudent(Student *student);
void getAge(Student **student);
void main() {
Student students[CLASS_SIZE];
addStudent(&students[3]);
return;
}
/* Pass-by-reference */
void addStudent(Student *student) {
printf("\nStudent name: ");
scanf("%s", student->name);
printf("\nStudent age: ");
getAge(&student);
return;
}
/* Pass-by-reference */
void getAge(Student **student) {
scanf("%d", student->age);
return;
}