473,385 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Pass-by-reference to nested function?

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;
}
Jun 27 '08 #1
4 6633
In article <02**********************************@i36g2000prf. googlegroups.com>,
S. <si********@gmail.comwrote:
>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
--
:wq
Jun 27 '08 #2
"Richard Tobin" <ri*****@cogsci.ed.ac.ukwrote in message
news:g0***********@pc-news.cogsci.ed.ac.uk...
In article
<02**********************************@i36g2000prf. googlegroups.com>,
S. <si********@gmail.comwrote:
[...]
(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.
Here is a simple way to setup fixed length string formatters in scanf what
will work with the NAME_SIZE macro:
__________________________________________________ __________________
#include <stdio.h>

#define PLACE_X(t)t
#define PLACE(t)t
#define QUOTE_X2(t)#t
#define QUOTE_X1(t)PLACE_X(QUOTE_X2)t
#define QUOTE(t)QUOTE_X1((t))

#define NAME_SIZE 10

int main() {
char name[NAME_SIZE + 1] = { '\0' };
printf("Enter Name: ");
if (scanf(QUOTE(%PLACE(NAME_SIZE)s), name) == 1) {
printf("\n\nYour Name Is: %s\n", name);
}
return 0;
}

__________________________________________________ __________________

Jun 27 '08 #3
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.

On May 11, 9:42 am, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <02f6dd75-4fd9-45f9-86dc-362fd84c5...@i36g2000prf.googlegroups.com>,

S. <sianeag...@gmail.comwrote:
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
--
:wq
Jun 27 '08 #4
S. said:
Hi all,

I have the requirement that I must pass-by-reference to my function
Tricky, that, in C. C uses pass-by-value for all parameter-passing.

<snip>
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);
Let's take a look at the student object, then, shall we?
typedef struct {
char name[20];
int age;
} Student;
Presumably that's the type.
void addStudent(Student *student);
void getAge(Student **student);

void main() {
In C, main returns int. Learn this. Mark it well. Engrave it on your heart,
your soul, and your wristwatch strap. In C, main returns int.
Student students[CLASS_SIZE];
addStudent(&students[3]);
That's fine. students[3] is a Student object, so &students[3] is a pointer
to a Student object.
return;
}

/* Pass-by-reference */
void addStudent(Student *student) {
That pointer sure looks to me as if it's being passed by value.
printf("\nStudent name: ");
scanf("%s", student->name);
Unwise. Anyone who types in a name as long as ZoroastrianPentangle (or
anything longer) will overrun memory that it shouldn't, possibly resulting
in bizarre outcomes. I recommend you do some research into fgets.
printf("\nStudent age: ");
getAge(&student);
Okay, that's legal, and passes a copy of the VALUE of the address of this
pointer to getAge().
return;
}

/* Pass-by-reference */
Sure looks like pass-by-value to me.
void getAge(Student **student) {
scanf("%d", student->age);
-requires on its left side a pointer to struct. You don't have one. You
have a pointer to pointer to struct. This will work:

int getAge(Student **student) {
return scanf("%d", &(*student)->age);
}

(note the & because scanf requires a pointer to a valid int, not the int
value itself) but one wonders why you are bothering to do this, since you
don't use the fact that, in this getAge function, student is a pointer to
a pointer. Why not lose a * and just do:

int getAge(Student *student) {
return scanf("%d", &student->age);
}

and make appropriate adjustments in the caller? If your reason is that
"then it wouldn't be pass by reference", well, it isn't pass by reference
anyway, so no loss there. In fact, why not just pass a pointer to the age?

int getAge(int *ageptr) {
return scanf("%d", ageptr);
}

In fact, why bother with the function at all?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: uli2003wien | last post by:
Dear group, PASS (SQL-Server user group) is about to start founding a branch in Vienna. I went to SQLCON and met some guys from the German PASS group and decided to become a member of PASS,...
8
by: Tcs | last post by:
I've been stumped on this for quite a while. I don't know if it's so simple that I just can't see it, or it's really possible. (Obviously, I HOPE it IS possible.) I'm trying to get my queries...
2
by: Alex Nitulescu | last post by:
Hi. I have tried to pass two parameters, like this: Response.Redirect(String.Format("NewPage.aspx?Username={0}, Pass={1}", txtUserName.Text, txtPass.Text)) But if I pass Username="Alex" and...
3
by: Brett | last post by:
I have several classes that create arrays of data and have certain properties. Call them A thru D classes, which means there are four. I can call certain methods in each class and get back an...
4
by: Marcelo | last post by:
Any suggestion? Thanks Marcelo
3
by: ILCSP | last post by:
Hello, I'm fairly new to the concept of running action pass through queries (insert, update, etc.) from Access 2000. I have a SQL Server 2000 database and I'm using a Access 2K database as my...
5
by: marshmallowww | last post by:
I have an Access 2000 mde application which uses ADO and pass through queries to communicate with SQL Server 7, 2000 or 2005. Some of my customers, especially those with SQL Server 2005, have had...
5
by: Remote_User | last post by:
Hi, Is there any way I can pass a file as a parameter? ( maybe read it into a object) I am working on C# VS2005. Thanks.
14
by: =?Utf-8?B?Umljaw==?= | last post by:
I have seen examples of passing data from FormB to FormA (Parent To Child) but I need an example of passind data from FormA to FormB. My main form is FormA; I will enter some data in textboxes and...
13
by: magickarle | last post by:
Hi, I got a pass-through query (that takes about 15 mins to process) I would like to integrate variables to it. IE: something simple: Select EmplID from empl_Lst where empl_lst.timestamp between...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.