Pointer to struct or Struct parameters

F

Fernan Bolando

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
 
J

Jens.Toerring

Fernan Bolando said:
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
 
C

CBFalconer

Fernan said:
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.
 
F

Fernan Bolando

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?.
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
 
P

pete

Fernan said:
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.
 
J

Jens.Toerring

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
 
F

Fernan Bolando

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top