Pass-by-reference to nested function?

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;
}
 
R

Richard Tobin

S. said:
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.

You achieve the effect of pass-by-reference in C by explicitly passing
a pointer. Both your functions, addStudent() and getAge(), want to
receive a Student by reference, so they should both declare a
pointer-to-student as their argument.

So this is right:
void addStudent(Student *student) {

and this call is right:
addStudent(&students[3]);

because students[3] is of type Student, so you have to use ampersand
to get a pointer to it.

This is wrong though:
void getAge(Student **student) {

It should be Student *student, just as in addStudent(), and this call:
getAge(&student);

should just be getAge(student) because student is already a pointer.

By the way, this is a very bad idea in real code:
scanf("%s", student->name);

because it tries to read a string of unknown length. You declared
Student as:
typedef struct {
char name[20];
int age;
} Student;

(did you mean to use NAME_SIZE which you defined as 20?), so what will
happen if the user type in a 30-character name? Look up scanf() to
see how to limit the string length.

-- Richard
 
S

S.

Hi Richard,

Thank you so much. Your comments not only fixed my problem but also
helped me with understanding pointers.

You are also right about your other tips in regards to NAME_SIZE and
the name array. I am fixing those aspects of the code as we speak.

Kind regards,
S.

S. said:
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.

You achieve the effect of pass-by-reference in C by explicitly passing
a pointer. Both your functions, addStudent() and getAge(), want to
receive a Student by reference, so they should both declare a
pointer-to-student as their argument.

So this is right:
void addStudent(Student *student) {

and this call is right:
addStudent(&students[3]);

because students[3] is of type Student, so you have to use ampersand
to get a pointer to it.

This is wrong though:
void getAge(Student **student) {

It should be Student *student, just as in addStudent(), and this call:
getAge(&student);

should just be getAge(student) because student is already a pointer.

By the way, this is a very bad idea in real code:
scanf("%s", student->name);

because it tries to read a string of unknown length. You declared
Student as:
typedef struct {
char name[20];
int age;
} Student;

(did you mean to use NAME_SIZE which you defined as 20?), so what will
happen if the user type in a 30-character name? Look up scanf() to
see how to limit the string length.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top