By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,742 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,742 IT Pros & Developers. It's quick & easy.

Pointer to struct or Struct parameters

P: n/a
What is the best way of passing a structure to a function currently I do
by it passing a pointer to the structe.

sample code below

int main()
{
struct sample_struct a_struct, *point_struct;

point_struct = &a_struct;

a_function(a_struct);
/* maybe do some stuff */
return 0;
}

int a_function(struct sample_struct *point_local)
{
/* do some stuff here using the pointer */

return 0;
}
Is there any better way of doing this?

thanks
,Fernan

--
Public PGP/GnuPG key (http://www.fernski.com)
pub 1024D/681A5498 2003-10-26 Fernan Bolando
Key fingerprint = BAC8 EA6A CA98 26F3 3DB4 74D1 988E 56F7 681A 5498
Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Fernan Bolando <fe***********@mailc.net> wrote:
What is the best way of passing a structure to a function currently I do
by it passing a pointer to the structe. sample code below int main()
{
struct sample_struct a_struct, *point_struct; point_struct = &a_struct; a_function(a_struct);
Here you're passing a copy of the structure to the function, not a
pointer to it as it's required from the the way you define the
called function (if you have set your compiler to a reasonable
warning level and have a declaration of the function is scope it
will flag this). If this wasn't a typo you should become aware of
the fact that, in contrast to arrays, you can pass whole structures
to functions - when used without a dot and an element name following
it a structure does _not_ gets converted (decays) to a pointer to
the structure. So here you would need either

a_function( point_struct );
or
a_function( &a_struct );

Since you don't seem to need the 'point_struct' variable for
anything else I would tend to get rid of it and use the second
way because you don't have to double-check that 'point_struct'
is really pointing to the structure you want to pass to the
function.
/* maybe do some stuff */
return 0;
} int a_function(struct sample_struct *point_local)
{
/* do some stuff here using the pointer */ return 0;
} Is there any better way of doing this?


Beside what probably was just a typo, there's no better way, it just
depends on what you want to do. If you don't want to change the
structure within the function you can also pass the structure itself
(i.e. a copy of it) instead of a pointer to the function. This has
the advantage that you can't make the mistake of changing anything
in the structure involuntarily, but can have the disadvantage that
it may be quite a bit slower because the whole structure must be
copied instead of just a pointer each time the function is called.

On the other hand, if you want to change the contents of the structure
from within the function (and don't want to return the structure itself
from the function), then there's no other way than passing a pointer to
the structure to the function.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2

P: n/a
Fernan Bolando wrote:

What is the best way of passing a structure to a function currently I do
by it passing a pointer to the structe.

sample code below

int main()
{
struct sample_struct a_struct, *point_struct;

point_struct = &a_struct;

a_function(a_struct); ^^^^^^^^^^^
This function is undefined at this point. Syntax error.
/* maybe do some stuff */
return 0;
}

int a_function(struct sample_struct *point_local)
{
/* do some stuff here using the pointer */

return 0;
}

Is there any better way of doing this?


Yes. Take the whole definition of a_function and move it ahead of
main, so it is defined when called. At this point you will get an
error on the type of the parameter, which you can correct either
by defining the parameter to be a struct sample_struct, or by call
with a pointer.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #3

P: n/a
In article <2n************@uni-berlin.de>,
Je***********@physik.fu-berlin.de wrote:
Fernan Bolando <fe***********@mailc.net> wrote:
What is the best way of passing a structure to a function currently I do
by it passing a pointer to the structe.
sample code below

int main()
{
struct sample_struct a_struct, *point_struct;

point_struct = &a_struct;

a_function(a_struct);


Here you're passing a copy of the structure to the function, not a
pointer to it as it's required from the the way you define the
called function (if you have set your compiler to a reasonable
warning level and have a declaration of the function is scope it
will flag this). If this wasn't a typo you should become aware of


Yes, This was a typo I really meant to pass the pointer to the function.
it a structure does _not_ gets converted (decays) to a pointer to
the structure. So here you would need either

a_function( point_struct );
or
a_function( &a_struct );

Since you don't seem to need the 'point_struct' variable for
anything else I would tend to get rid of it and use the second
way because you don't have to double-check that 'point_struct'
is really pointing to the structure you want to pass to the
function.


Is there any situation where it would be advantagoues to use the first
option?.
/* maybe do some stuff */
return 0;
}

int a_function(struct sample_struct *point_local)
{
/* do some stuff here using the pointer */

return 0;
}

Is there any better way of doing this?


Beside what probably was just a typo, there's no better way, it just
depends on what you want to do. If you don't want to change the
structure within the function you can also pass the structure itself
(i.e. a copy of it) instead of a pointer to the function. This has
the advantage that you can't make the mistake of changing anything
in the structure involuntarily, but can have the disadvantage that
it may be quite a bit slower because the whole structure must be
copied instead of just a pointer each time the function is called.

On the other hand, if you want to change the contents of the structure
from within the function (and don't want to return the structure itself
from the function), then there's no other way than passing a pointer to
the structure to the function.


My current situation is the function needs to change the contents of the
structure. The structure actually contains a linked list of integers and
float that is dynamically allocated by the called function.

I am just trying to avoid making the function global.
thanks for the quick response
,Fernan
--
Public PGP/GnuPG key (http://www.fernski.com)
pub 1024D/681A5498 2003-10-26 Fernan Bolando
Key fingerprint = BAC8 EA6A CA98 26F3 3DB4 74D1 988E 56F7 681A 5498
Nov 14 '05 #4

P: n/a
Fernan Bolando wrote:
My current situation is the function needs
to change the contents of the structure.


The regular way for a function to change the value of an object,
is to use a pointer to the object as an argument to the function.

If you passed the value of the object to the function,
then you would have to have the new value returned by the function
and assigned to the object in question.
Nov 14 '05 #5

P: n/a
Fernan Bolando <fe***********@mailc.net> wrote:
In article <2n************@uni-berlin.de>,
Je***********@physik.fu-berlin.de wrote:
it a structure does _not_ gets converted (decays) to a pointer to
the structure. So here you would need either

a_function( point_struct );
or
a_function( &a_struct );

Since you don't seem to need the 'point_struct' variable for
anything else I would tend to get rid of it and use the second
way because you don't have to double-check that 'point_struct'
is really pointing to the structure you want to pass to the
function.
Is there any situation where it would be advantagoues to use the first
option?.
I guess you would use it when you have several structures and you
select which one of them to be passed to the function only at run
time.
My current situation is the function needs to change the contents of the
structure. The structure actually contains a linked list of integers and
float that is dynamically allocated by the called function.
I am just trying to avoid making the function global.


I guess you mean the structure, not the function. And in such cases
it would be reasonable to pass a pointer to that structure to the
function - that's exactly one of the things pointers are good for.
Actually, you won't see it very often that a structure is passed
directly to a function due to the extra time required to copy it
on function invocation, passing a structure by address is done much
more often.
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #6

P: n/a
In article <2n************@uni-berlin.de>,
Je***********@physik.fu-berlin.de wrote:
Fernan Bolando <fe***********@mailc.net> wrote:
In article <2n************@uni-berlin.de>,
Je***********@physik.fu-berlin.de wrote:

I guess you mean the structure, not the function. And in such cases
it would be reasonable to pass a pointer to that structure to the
function - that's exactly one of the things pointers are good for.
Actually, you won't see it very often that a structure is passed
directly to a function due to the extra time required to copy it
on function invocation, passing a structure by address is done much
more often.


thanks very much
,Fernan

--
Public PGP/GnuPG key (http://www.fernski.com)
pub 1024D/681A5498 2003-10-26 Fernan Bolando
Key fingerprint = BAC8 EA6A CA98 26F3 3DB4 74D1 988E 56F7 681A 5498
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.