473,224 Members | 1,903 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,224 software developers and data experts.

Pointer to struct or Struct parameters

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
6 15202
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
5
by: caleb.vandyke | last post by:
I am working with some code that is doing some pointer to structure casts and I can't figure out how the cast is being done. Here is basically the code. #include <stdio.h> #include <stdlib.h> ...
48
by: yezi | last post by:
Hi, all: I want to record some memory pointer returned from malloc, is possible the code like below? int memo_index; int i,j; char *tmp; for (i=0;i<10;i++){
24
by: venkatesh | last post by:
hai, any can tell what is the advantge of using pointers?any increase in speed or execution time? if you know please tell what are the real time applications of data structure? thanks in...
7
by: bwaichu | last post by:
In some APIs, we see char ** or void ** as a parameter. I never distinguished between declaring a variable as char **x and passing x as the parameter from declaring a variable char *x and passing...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
7
by: william | last post by:
My question is: Specific memory block where my pointer pointing to changed strangely, seemingly that no statement changed it. Here are two examples I got: ***********1***************** I was...
3
by: George2 | last post by:
Hello everyone, In the following statements, template <class R, class Tclass mem_fun_t : public unary_function <T*, R> {
14
by: ManicQin | last post by:
Hi all. I'm trying to get the size of a variable in a struct by his relative postion i.e. /// #define offsetof(s,m) (size_t)&(((s *)0)->m) struct ThePimp{ char rings; char blings;
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.