Hi, I seem to be having problems figuring out how to pass a struct to a function by reference.
I could use a little assistance since I’m still learning C.
This is a stripped down version of my code but all relevant info is here.
I get errors:
line 2288 : ERROR SC67V2.10X.C : Keyword ‘const’ can only be used with globals and static locals.
line 2290 : ERROR SC67V2.10X.C : Missing character ‘;’.
line 2304 : WARNING SC67V2.10X.C : Wrong type for parameter 1.
Any help figuring out my problem would be appreciated.
You can’t initialize a non-constant variable on the same line as the declaration. This style of initialization can only be used with constants. Quite frankly, you have no need of this pointer variable anyway, see below.
You may also notice the removal of struct before config in the ReadUB header. Once you typedef a structure, you no longer need the struct keyword to proceed the defined type, the type already contains it.
This is what I had originally started out with but when I compile I get:
line 400 : ERROR APPLIC.LIB : ',' is missing/expected.
line 400 : ERROR APPLIC.LIB : Syntax error - or garbage at end of program.
line 400 : ERROR APPLIC.LIB : Need function definition or declaration.
line 400 : ERROR APPLIC.LIB : Expects ';'.
line 400 : ERROR APPLIC.LIB : Old style function declaration missing parameter declaration list.
line 400 : ERROR APPLIC.LIB : Missing character ')'.
//-----------------------------------------------------------------------------
// Read UserBlock
/*** BeginHeader ReadUB */
xmem void ReadUB(config *pUBconfigData);//<-Line 400
/*** EndHeader ***/
//nodebug
xmem void ReadUB(config *pUBconfigData)
{
int Result;
UINT16 UBlockSize;
readUserBlock(&UBlockSize,500,2);//size of userblock data
readUserBlock(pUBconfigData,502,UBlockSize);//read in UB configData
}
All of these errors have to do with what is on lines 1-399. Error reporting is not an exact science, you need to look before line 400, or post that here.
The problem would be in your typedefs of either config or UINT16 then as everything else has proper syntax. If you can’t find it, could you post those typedefs.
It looks like the typedef for config is declared in a way that it is not in scope when the ReadUB function declaration is made. This would explain the ‘,’ is missing/expected error. If the typedef were not in scope, the parser would think config is a variable declaration, not a type for *pUBconfigData. Where do you declare the typedef of config?
If after a /*** BeginHeader func / of some other function, it may not be in scope. Look at some of our pre-defined libraries and you will see global constants and typedefs are put in their own
/** BeginHeader /
…
/** EndHeader */
block not related to any specific function to insure they are always included when the library is used.
It sounds like you added the begin/end headers within the main C file. You must move the typedef of config to the beginning of the library file, not the C file. A library file should generally not be dependent on declarations in C files to compile correctly. The whole purpose of a library file is to support code re-use, so if a library function is using a structure, the typedef should either be in the library, or in a library that it specifically uses, not in the C file. There may be a few exceptions, but generally this rule should be followed. If you must leave it in the C file, make sure it is declared BEFORE you #use the library file.