Re: passing structures among functions




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/Reh26hy-JHI/AAAAAAAAAAk/wvyV3Yx8gSs/s1600-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 "definitions" 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
.



Relevant Pages

  • Re: Linker error multiple definitions
    ... > I declared the functions in separate header files ... But your compiler isn't complaining about multiple *declarations*. ...
    (comp.lang.c)
  • Re: Linker error - fooView.obj LNK2005: _IID_IProvideTaskPage already defined in fooDoc.obj
    ... declarations, one in the document and one in the view. ... multithreaded libraries would refer to symbols in the single-threaded and multithreaded ... Unless someone in the header files is ... Look for simple solutions first. ...
    (microsoft.public.vc.mfc)
  • Re: Ahead of "main"?
    ... The latter style is effectively required when you move to multi-file projects, and the standard practice is to put all of your function declarations in header files so that each source file can simply #include the appropriate header files and then use whatever functions are needed. ... Actually, you should not put *all* your function definitions in header files, since generally there are some which should be local to a given source file and declared static in that source file. ...
    (comp.lang.c)
  • Re: [patch 1/3] epoll cleanups - epoll include diet ...
    ... and it certainly needs to have those syscall's prototypes in scope. ... Ack about signal.h, I forgot about the pwait code:( ... So that the compiler can verify that our declarations of sys_epoll_foo ... include in .c the header files which provide the stuff ...
    (Linux-Kernel)
  • Re: Newbie question
    ... > struct data { ... Using header guards, will prevent anything in the header file ... Declared in header files, ...
    (comp.lang.c)