473,856 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing structures among functions

The following link shows a chart I created about passing structures
among functions. Would you review it and tell me if it requires any
corrections?

http://bp2.blogger.com/_lZhqNsiakm4/...00-h/gif_1.gif

Thank you.
sofeng

Mar 3 '07 #1
4 3473

On Fri, 2 Mar 2007, sofeng wrote:
>
The following link shows a chart I created about passing structures
among functions. Would you review it and tell me if it requires any
corrections?

http://bp2.blogger.com/_lZhqNsiakm4/...00-h/gif_1.gif
FYI, many readers of Usenet *will not* go to a Web page just because
a pseudonymous poster asks them to, especially when the page has such
an obviously machine-generated URL. (And perhaps doubly so when the
URL is actually the URL of an HTML page, despite the ".gif" ending.
That's just unnecessary.)

Note to prospective Web-goers: I used 'wget' to grab the GIF, so if
there's any malicious code on that Web page, I didn't encounter it.
Don't think I'm endorsing the page.

Okay, now on to your question. Mistakes ordered from most significant
to least significant:

* All your ".h" files are missing the include guards.
http://en.wikipedia.org/wiki/Include_guard
* "get_data.h " refers to DATA, so it needs to #include "defs.h".
* "get_data.c " refers to get_data(), so it needs to #include "get_data.h ".
* "DATA", in all caps, looks like a preprocessor macro. It is better to
use something non-macro-ish, such as "Data" or "data_t". Except...
* The use of "typedef" is superfluous, and most comp.lang.c regulars
will counsel you not to use it. Give the struct a tag, such as "struct
data", and use that instead.
* You spelled "definition s" as "definitons " in one place.
* "float" is rarely used; "double" would be more newbie-friendly, if your
goal is to show what real code looks like.
* "Header files should only contain declarations." should read
"Header files should contain only declarations." (And macro definitions,
and comments.)
* Burn your GIFs and use PNG instead; it's cooler. :)

HTH,
-Arthur
Mar 3 '07 #2
Arthur J. O'Dwyer wrote:
Note to prospective Web-goers: I used 'wget' to grab the GIF, so if
there's any malicious code on that Web page, I didn't encounter it.
Don't think I'm endorsing the page.
Better to just avoid the windows/IE combination :)

--
Ian Collins.
Mar 3 '07 #3
On Mar 2, 5:16 pm, "Arthur J. O'Dwyer" <ajonos...@andr ew.cmu.edu>
wrote:
On Fri, 2 Mar 2007, sofeng wrote:
The following link shows a chart I created about passing structures
among functions. Would you review it and tell me if it requires any
corrections?
http://bp2.blogger.com/_lZhqNsiakm4/...AAAk/wvyV3Yx8g...

FYI, many readers of Usenet *will not* go to a Web page just because
a pseudonymous poster asks them to, especially when the page has such
an obviously machine-generated URL. (And perhaps doubly so when the
URL is actually the URL of an HTML page, despite the ".gif" ending.
That's just unnecessary.)

Note to prospective Web-goers: I used 'wget' to grab the GIF, so if
there's any malicious code on that Web page, I didn't encounter it.
Don't think I'm endorsing the page.

Okay, now on to your question. Mistakes ordered from most significant
to least significant:

* All your ".h" files are missing the include guards.http://en.wikipedia.org/wiki/Include_guard
* "get_data.h " refers to DATA, so it needs to #include "defs.h".
* "get_data.c " refers to get_data(), so it needs to #include "get_data.h ".
* "DATA", in all caps, looks like a preprocessor macro. It is better to
use something non-macro-ish, such as "Data" or "data_t". Except...
* The use of "typedef" is superfluous, and most comp.lang.c regulars
will counsel you not to use it. Give the struct a tag, such as "struct
data", and use that instead.
* You spelled "definition s" as "definitons " in one place.
* "float" is rarely used; "double" would be more newbie-friendly, if your
goal is to show what real code looks like.
* "Header files should only contain declarations." should read
"Header files should contain only declarations." (And macro definitions,
and comments.)
* Burn your GIFs and use PNG instead; it's cooler. :)

HTH,
-Arthur
Thank you much for the feedback. I have made the changes suggested.
How does it look now? Here is the link for the png file:
http://bp1.blogger.com/_lZhqNsiakm4/...00-h/png_1.png
The text contained in the diagram is below:

main.c:
- main.c contains the main function which calls the other functions
that operate on the data structure.
- It also defines (allocates memory) for the data structure at the
file level.
- The data structure is declared using the static keyword so that it
will have static duration (i.e. it will exist from program start to
finish), but it will have internal linkage (i.e. it won't be global).
- The address operator, &, is used to create a pointer to the data
structure which is passed to the other functions.

#include "defs.h"
#include "get_data.h "
#include "modify_dat a.h"

static struct data the_data;

int main()
{
get_data(&the_d ata);
modify_data(&th e_data);

return 0;
}

get_data.c:
- get_data() fills the data structure
- it is passed a pointer to the data structure defined in main.c

#include "defs.h"
#include "get_data.h "

int get_data(struct data *data_ptr)
{
data_ptr->item1 = 1;
data_ptr->item2 = 2;
data_ptr->item3 = 3.0;
data_ptr->item4 = 4.0;

return 0;
}

modify_data.c:
- modify_data() modifies the data structure
- It is similar to get_data()

#include "defs.h"
#include "modify_dat a.h"

int modify_data(str uct data *data_ptr)
{
data_ptr->item2 += 1;
data_ptr->item4 += 1.0;

return 0;
}

defs.h:
- contains the structure data type declaration

#ifndef DEFS_H_
#define DEFS_H_

struct data
{
int item1;
int item2;
double item3;
double item4;
};

#endif /*DEFS_H_*/

get_data.h:
- contains the get_data() function declaration

#ifndef GET_DATA_H_
#define GET_DATA_H_

#include "defs.h"

int get_data(struct data *data_ptr);

#endif /*GET_DATA_H_*/

modify_data.h:
- contains the modify_data() function declaration

#ifndef MODIFY_DATA_H_
#define MODIFY_DATA_H_

#include "defs.h"

int modify_data(str uct data *data_ptr);

#endif /*MODIFY_DATA_H_ */

NOTE: Header files should contain only declarations, macro
definitions, and comments. They should not contain variable
definitions because if the header file is included in multiple
locations, there would be multiple definitions of the same variable.

Mar 6 '07 #4
sofeng wrote, On 06/03/07 02:07:

<snip>
Thank you much for the feedback. I have made the changes suggested.
How does it look now? Here is the link for the png file:
http://bp1.blogger.com/_lZhqNsiakm4/...00-h/png_1.png
The text contained in the diagram is below:
I'm still not following the link (too lazy) but since you've included
the code it does not matter :-)
main.c:
- main.c contains the main function which calls the other functions
that operate on the data structure.
- It also defines (allocates memory) for the data structure at the
file level.
- The data structure is declared using the static keyword so that it
will have static duration (i.e. it will exist from program start to
finish), but it will have internal linkage (i.e. it won't be global).
- The address operator, &, is used to create a pointer to the data
structure which is passed to the other functions.

#include "defs.h"
#include "get_data.h "
#include "modify_dat a.h"

static struct data the_data;
There is no need for this to be a file scope variable. Better would be
to declare it inside main and not static. This will also "hide" it from
any other functions you (or someone else) later write in the source file
containing it.
int main()
As you are not using command line parameters better to be explicit
int main(void)
{
struct data the_data;
Or, if you want it to be initialised
struct data the_data = {0};
get_data(&the_d ata);
modify_data(&th e_data);

return 0;
}

get_data.c:
- get_data() fills the data structure
- it is passed a pointer to the data structure defined in main.c

#include "defs.h"
#include "get_data.h "

int get_data(struct data *data_ptr)
Since this only ever returns the one value why have it return anything
at all? Better would be
void get_data(struct data *data_ptr)
Then loose the return at the bottom.
{
data_ptr->item1 = 1;
data_ptr->item2 = 2;
data_ptr->item3 = 3.0;
data_ptr->item4 = 4.0;

return 0;
}

modify_data.c:
- modify_data() modifies the data structure
- It is similar to get_data()

#include "defs.h"
#include "modify_dat a.h"

int modify_data(str uct data *data_ptr)
Same comment as last one. Better
void modify_data(str uct data *data_ptr)

<snip stuff that looked OK at a glance>
NOTE: Header files should contain only declarations, macro
definitions, and comments. They should not contain variable
definitions because if the header file is included in multiple
locations, there would be multiple definitions of the same variable.
typedefs are also OK in headers when it makes sense to use them, which
it does sometimes.
--
Flash Gordon
Mar 6 '07 #5

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

Similar topics

6
2315
by: Garma | last post by:
According to what I have learnt so far, instantiating global objects should be the last resort. Is there any reasons why so? Sometimes some objects or their pointers have to be shared among several other objects, I'd like to know how this is handled commonly. Could someone elaborate on this? Another situation is some objects could be shared among several threads. How to handle it commonly?
5
9037
by: lugal | last post by:
This might be more appropriate here. I'm new to C++, coming from a background in another languages that allowed a similar solution to work (Python). I wrote the following code in C++ based on the Python code found here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478 //beginning #include <vector>
2
1648
by: MJL | last post by:
I am working on a small project that involves the manipulation of dynamically allocated memory for strings. I was wondering why the string.h functions are the way they are and why not as follows: mystrcat(char** s1,char** s2); where s1's memory allocation can now be handled inside of the function and s2 can be freed and pointed to NULL inside the function. I'm not saying the built in functions are not useful as they are, but
10
3180
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences in levels of indirection, so I feel it must have something to do with the way I am representing the array in the call and the return. Below I have commented the problem parts. Thanks in advance for any help offered. Pete
4
3849
by: sam1967 | last post by:
How do I get a function to return a GMP integer type mpz_t when i try it i get an error message. i am trying mpz_t hooch (int x) { mpz_t y; ........
2
3206
by: jason | last post by:
Hello everyone, I have a C# object library which encapsulates data. Data is keyed by way of Guid values, which are stored in the objects as read-only Guid properties. However, we have a Classic ASP application that has to read these Guid values, and sometimes pass them back into the the object libraries (for LoadByID type functions).
7
2162
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but they do requrie much more memory than a simple pointer.
6
3885
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is definined as taking a char * as an argument? For example the C++ header says SDCERR GetCurrentConfig(DWORD *num, char *name); I am using Uint for the *num parameter, which returns the correct value but for *name, I always get back a string of 6 squares....
13
3213
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the functions that require a structure to be passed to them. The function declaration in the .h file is of the form: SDCERR GetConfig(char *name, SDCConfig *cfg); where SDCConfig is a structure defined in the .h file. I am not much of a C (or C#)...
0
10695
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10777
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9530
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7930
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5759
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4573
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4172
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3198
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.