[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::decc

Title:DECC
Notice:General DEC C discussions
Moderator:TLE::D_SMITHNTE
Created:Fri Nov 13 1992
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2212
Total number of notes:11045

2141.0. "Passing structure to sub-routine: How?" by SBUOA::GUILLERMO (But the world still goes round and round) Mon Apr 07 1997 17:20

    if( anyone can help )
    	On Alpha-AXP UNIX:
    
    	I've declared a structure globally as struct struct_type a
    
    	declared a function prototype as
    		extern int function_prototype( struct struct_type b )
    
    	declared a global structure variable as struct struct_type a1.
    
    	In the main code body the members of a1 are populated,
    
    	then function is called as function( a1 );
    
    	Within the function, the argument is defined as follows:
    
    		int function( struct struct_type b ).
    
    	The compiler complains that:
    		1.) struct_type declared within and is limited to this
    		    function prototype
    [ I thought I declared it above main, outside of any sub-routine, i.e.
      global. ]
    
    		2.) b <- Reference an expression of void type or an
    		    incomplete type.
    
        and complains about #2 18 times.
    
    	Can someone show me the righteous path? Thanks!
    
    
    
    
T.RTitleUserPersonal
Name
DateLines
2141.1Need an exampleWIBBIN::NOYCEPulling weeds, pickin&#039; stonesMon Apr 07 1997 17:3012
>    [ I thought I declared it above main, outside of any sub-routine, i.e.
>      global. ]

Well, for some reason the compiler didn't think so.
Can you show a short example of a source file that demonstrates the problem?

If the first time the compiler sees "struct s" is inside the function prototype,
then it declares the type "struct s" only for the duration of that prototype,
so you can't use the same type inside the function, and you can't use the same
type to call the function.  (You can use a type that looks the same, but it
isn't "the same type.")  But if you declare "struct s" before the function
prototype, its declaration lasts through to the end of the compilation.
2141.2ExampleSBUOA::GUILLERMOBut the world still goes round and roundMon Apr 07 1997 18:2457
    re:.1
    
    Hi,
    
    >If the first time the compiler sees "struct s" is inside the function
    prototype,
    
    	But the first time "struct s" is declared is before the declared
    	prototype.
    
    >(You can use a type that looks the same, but it isn't "the same type.") 
    
    	I understand this from the example I'm trying to follow in
    	"C - The complete reference" by Herbert Schild.
    
    Here's a snapshot of what I'm trying to do:
    
    
    /* Main module */
    
    /* declarations */
    
    char partinfo[42];
    
    struct a_partrec {
    
    	char partno[12];
    	char remainder[30];
    
    };
    
    struct a_partrec partrec;
    
    extern int sqlInsertPart( struct a_partrec partval );
    
    int ExtractPartInfo( void );
    
    unsigned int main(int argc, char *argv[])
    {
    strcpy( partinfo, argv[1] );
    
    ExtractPartInfo();
    
    sqlInsertPart( partrec );
    }
    
    int ExtractPartInfo()
    {
    	/* copy partinfo to structure members e.g. partrec.partno */
    }
    
    [ Now in a separate .pc module which handles database I/O, the
    module is defined]
    
    int sqlInsertPart( struct a_partrec partval );
    
                                     
2141.3Compiler needs to see it first **in every compilation**WIBBIN::NOYCEPulling weeds, pickin&#039; stonesMon Apr 07 1997 18:4244
>    [ Now in a separate .pc module which handles database I/O, the
>    module is defined]
>    
>    int sqlInsertPart( struct a_partrec partval );

You need to define struct a_partrec before this prototype too, either
in the same .pc file, or (better) in a .h file that's #include'd both
in tha main module and in the .pc file:

part.h:
=======    
    char partinfo[42];
    
    struct a_partrec {
    
    	char partno[12];
    	char remainder[30];
    
    };

main.c:
=======
    #include "part.h"

    struct a_partrec partrec;
    
    extern int sqlInsertPart( struct a_partrec partval );
    
    int ExtractPartInfo( void );
    
    unsigned int main(int argc, char *argv[])
    {
    strcpy( partinfo, argv[1] );
    
    ExtractPartInfo();
    
    sqlInsertPart( partrec );
    }

insert.pc:
==========
    #include "part.h"

    int sqlInsertPart( struct a_partrec partval );
2141.4SBUOA::GUILLERMOBut the world still goes round and roundMon Apr 07 1997 19:093
    Thanks much.
    
    
2141.5SBUOA::GUILLERMOBut the world still goes round and roundTue Apr 08 1997 14:329
    Here's another bonus for any fellow knowledge seekers out there.
    
    I thought via another (painful) experience that you didn't need to
    "null terminate" variables that were slated to be used in Oracle
    expressions.
    
    Well, you d**n sure need to null terminate them when using a structure,
    otherwise, you wind up appending all sorts of unintended things to
    your members. (oo-er).