Compiler Error in C: expected ')' before '*' token - c

As the title says, keep getting this error when trying to compile. From Googling this error people have said that it is not declared in the header file but my function is static and it is not in a header file, I prototyped it.`
#include <recGbl.h>
#include <devSup.h>
#include <devLib.h>
#include <drvIpac.h>
#include <dbScan.h>
#include <epicsExport.h>
static int cardinit(cardinfo *card); // <-- line that gives the error
typedef struct cardinfo{
struct cardinfo *next;
struct io_mem_read *pMem; /* IP register (A16) mem address */
word *rambase; /* RAM conversion memory mem address*/
int isconfigured;
int doram; /* 1 if we are using the RAM to output data.
0 if we are writing to registers (AO style) */
int cardnum;
int vmeslotnum;
int ipslotnum;
/* these values mirror the hardware registers */
word csr;
word offset;
word numconv;
word clockrate;
word vectnum;
word dacval[MAXSIGNAL];
word oldispresent;
/* used to detect a reinsertion of a carrier card.
see subroutine ispresent() below. */
/* use to update process variables */
IOSCANPVT ioscanpvt;
} cardinfo;
static int Hy8402init(int vmeslot, int ipslot, int clockrate) {
cardinfo *card;
card->vmeslotnum = vmeslot;
card->ipslotnum = ipslot;
card->cardnum = 1;
card->clockrate = clockrate;
card->vectnum = 10;
cardinit(card);
return TRUE;
}
static int cardinit(cardinfo *card){
word rprobe;
int res;
volatile word *ramptr;
card->pMem= ipmBaseAddr(card->vmeslotnum,
card->ipslotnum,ipac_addrIO);
if (card->pMem==NULL){
printf("Error in %s",devstr);
printf( "%s: Cannot determine base address\n",devstr);
return FALSE;
}
res=devReadProbe(sizeof (word),(char *) card->pMem,(char *) &rprobe);
if (res!=OK){
printf("%s: NO DEVICE at %x (vmeslot %d, ipslot %d)\n",devstr,
(int)card->pMem,
card->vmeslotnum,card->ipslotnum);
return FALSE;
}
return TRUE;
}
`

cardinfo struct is still undefined on the line with error. Put a forward declaration before it:
struct cardinfo;
static int cardinit(struct cardinfo *card);

This line of code:
static int cardinit(cardinfo *card);
should be added after the definition of your cardinfo structure.

You need to put the line
static int cardinit(cardinfo *card);
after the definition of the cardinfo structure.

At that line, the compiler doesn't yet know that cardinfo is a struct. Precede it with the line struct cardinfo;

You have declared a function which has a input variable of a type which the compiler is not aware when it parses it. i.e the struct defintion follows your function declaration.
So please do a forward declaration of the structure when you want to compile such code.
In computer programming, a forward declaration is a declaration
of an identifier (denoting an entity such as a type, a variable, or a
function) for which the programmer has not yet given a complete definition.
This link has a nice article on when full declarations are not required.

Related

Trouble Including Externally Declared Enumeration - C Code

Update: The issue is resolved. Here is code that compiles properly.
---instruction.h---
#ifndef INSTRUCTION_H
#define INSTRUCTION_H
typedef enum OPCODE {ADD = 0x20,ADDI = 0x8,SUB = 0x22,MULT = 0x18,BEQ = 0x4,LW = 0x23,SW = 0x2B} opcode;
/*opcode is OPCODEs alias*/
typedef struct INSTRUCTION {
opcode op;
int rs;
int rt;
int rd;
int Imm;
} inst;
/*inst is INSTRUCTIONs alias*/
#endif // INSTRUCTION_H
---parser.c---
#include <stdio.h>
#include "instruction.h"
void parser(char *instruction)
{
/*Parse character string into instruction components*/
inst set1 = {LW,0,1,2,0};
printf("parsing");
};
int main()
{
char *instruction;
instruction = NULL;
parser(instruction);
};
/*pass in pointer for instruction being passed in*/
/*pointing to address of instruction being passed in*/
/*Parser return type is struct inst*/
I cannot seem to get my enumeration type "opcode" to be recognized in my main c file. I included the header file. I am fairly new to C, so haven't made much ground on the issue for some time now and wanted to see if anyone knew why I was getting the error messages below. My guess is the linking the header file is not working properly. Any help is much appreciated.
---instruction.h----
#ifndef INSTRUCTION_H
#define INSTRUCTION_H
typedef enum {add = 32,addi = 8,sub = 34,mult = 24,beq = 4,lw = 35,sw = 43}opcode;
extern opcode oper;
typedef struct {
opcode op;
int rs;
int rt;
int rd;
int Imm;
}inst;
#endif // INSTRUCTION_H
---Parser.c---
#include <stdio.h>
#include "instruction.h"
void parser(char *inst)
{
/*Parse character string into instruction components*/
struct inst{lw,0,1,2,0};
};
int main()
{
char *instruction;
instruction = NULL;
parser(instruction);
};
struct inst{lw,0,1,2,0};
This looks like it's supposed to be a variable declaration, but I don't see a name for the variable. Try:
struct inst name_of_the_variable = {lw,0,1,2,0};
As a side note, enum values are global constants, so it's probably not a good idea to give them names like lw that can be confused for variables. Standard practice would be to use all-caps for the names and give them a prefix… say, OPCODE_ADD, OPCODE_LW, etc.
This is not a valid variable definition:
struct inst{lw,0,1,2,0};
There's no struct inst defined, only inst, there's no variable name, and you need = to use an initializer. To create a variable of this type an initialize it, you need:
inst myinst = {lw,0,1,2,0};
Also, your function has a parameter named inst which masks the type inst. You need to give it a different name:
void parser(char *instruction)

how to call function pointer in a struct?

I am working with the xinu embedded operating system in c. I created a new header file and declared a struct:
struct callout {
uint32 time; /* Time of delay in ms */
void *funcaddr; /* Function pointer */
void *argp; /* Function arguments */
uint32 cid; /* Callout id for the specific callout */
char *sample;
};
In my main, I try to declare a struct object and function the funcaddr to a function.
void test();
process main(void) {
struct callout *coptr;
coptr->sample ="hellowolrd";
coptr->funcaddr = &test;
(coptr->funcaddr)(coptr->argp); //error here
kprintf("coptr %s \n", coptr->sample);
return OK;
}
void test() {
kprintf("this is the test function \n");
}
I try to invoke the function pointer through the struct but I am getting an error:
main.c:30:19: error: called object is not a function or function pointer
(coptr->funcaddr)();
Please show what is the correct syntax to invoke the function pointer.
You have declared funcaddr as an object pointer. To declare a function pointer it looks like this:
struct callout {
uint32 time;
void (*funcaddr)(); // <-------- function pointer
Then the rest of your code should work.
If you didn't see an error message for the line coptr->funcaddr = &test; then I would recommend adjusting your compiler settings, it's important to have the information available that the compiler can tell you.

Unable to pass C struct into function

I'm having trouble passing a struct into a function and I am running into an error:
'PWM_PINS' undeclared (first use in this function)
I am typically able to do this in a C++ compiler without any trouble. I would appreciate some advice as to what I might be doing wrong here.
I have included the relevant parts from the header and c file below.
pwm.h file:
typedef struct PWM_tag{
int PWM_1;
int PWM_2;
int PWM_3;
int PWM_4;
int PWM_5;
int PWM_6;
} PWM;
void PWM_Set( uint32_t channelNum, uint32_t cycle, PWM PWN_PINS );
pwm.c file:
#include "pwm.h"
void PWM_Set( uint32_t ChannelNum, uint32_t cycle, PWM PWN_PINS)
{
if ( ChannelNum == 1 )
{
LPC_PWM1->MR0 = cycle;
LPC_PWM1->MR1 = PWM_PINS.PWM_1;
LPC_PWM1->MR2 = PWM_PINS.PWM_2;
LPC_PWM1->MR3 = PWN_PINS.PWM_3;
LPC_PWM1->MR4 = PWM_PINS.PWM_4;
LPC_PWM1->MR5 = PWM_PINS.PWM_5;
LPC_PWM1->MR6 = PWM_PINS.PWM_6;
}
return;
}
You declared a parameter called PWN_PINS (with an N), but you are referring to PWM_PINS (with an M).
Fixing this typo will address this particular error. There may be more errors, though - it's hard to tell, because the snippet does not show essential parts, such as the declaration of LPC_PWM1 variable.
Is there misspelling in the code?
The function parameter is PWN_PINS.But the code have 5 PWM_PINS, and one PWN_PINS.
I think what you should do is to change all PWN_PINS to PWM_PINS.

Warning: parameter names (without types) in function declaration

Rookie question FYI.
Whenever I compile/run the code, extern tolayer2(rtpktTo1); I receive a warning.
The warning reads, as in the title, Warning: parameter names (without types) in function declaration
Any help appreciated.
node0.c
extern struct rtpkt {
int sourceid; /* id of sending router sending this pkt */
int destid; /* id of router to which pkt being sent
(must be an immediate neighbor) */
int mincost[4]; /* current understanding of min cost to node 0 ... 3 */
};
/* Create routing packets (rtpkt) and send to neighbors via tolayer2(). */
struct rtpkt rtpktTo1;
rtpktTo1.sourceid = 0;
rtpktTo1.destid = 1;
rtpktTo1.mincost[0] = minCost[0];
rtpktTo1.mincost[1] = minCost[1];
rtpktTo1.mincost[2] = minCost[2];
rtpktTo1.mincost[3] = minCost[3];
extern tolayer2(rtpktTo1);
prog3.c
tolayer2(packet)
struct rtpkt packet;
{
/* This has a lot of code in it */
}
The assignments to rkpktTo1.* are not apparently in a function or declaration, unless this is a code fragment. Wrap them in a function. The warning is a bit misleading.
The declaration of tolayer2() should have a return type as well as a parameter type. Since there isn't one, int is assumed. This may not be what is intended, but it should compile without warnings and errors:
node0.c
struct rtpkt {
int sourceid; /* id of sending router sending this pkt */
int destid; /* id of router to which pkt being sent
(must be an immediate neighbor) */
int mincost[4]; /* current understanding of min cost to node 0 ... 3 */
};
/* Create routing packets (rtpkt) and send to neighbors via tolayer2(). */
void function () {
struct rtpkt rtpktTo1;
rtpktTo1.sourceid = 0;
rtpktTo1.destid = 1;
rtpktTo1.mincost[0] = minCost[0];
rtpktTo1.mincost[1] = minCost[1];
rtpktTo1.mincost[2] = minCost[2];
rtpktTo1.mincost[3] = minCost[3];
}
extern void tolayer2(struct rtpkt *rtpktTo1);
prog3.c
void
tolayer2(struct rtpkt *packet)
{
/* This has a lot of code in it */
}
Passing a structure by value is often not appropriate, so I have changed it to pass by reference.
In prog3.c
tolayer2(packet)
struct rtpkt packet;
{ /* ... */ }
This is old syntax (very old: before ANSI standardized C in 1989), but perfectly legal in C89 and C99. Don't use it though: prefer
int tolayer2(struct rtpkt packet)
{ /* ... */ }
In the declaration extern tolayer2(rtpktTo1);, rtpktTo1 is a parameter name (like the error says), while you should give a type there:
extern tolayer2(struct rtpkt);
or
extern tolayer2(struct rtpkt *);
or
extern tolayer2(struct rtpkt const *);
or similar, since that is what the compiler needs to know about your function before compiling client code. The parameter name is useless to the compiler at this point and therefore optional.
(And really, you should add a return type as well, and note that extern has no meaning in your struct definition.)

Trouble with Unions in C program

I am working on a C program that uses a Union. The union definition is in FILE_A header file and looks like this...
// FILE_A.h****************************************************
xdata union
{
long position;
char bytes[4];
}CurrentPosition;
If I set the value of CurrentPosition.position in FILE_A.c and then call a function in FILE_B.c that uses the union, the data in the union is back to Zero. This is demonstrated below.
// FILE_A.c****************************************************
int main.c(void)
{
CurrentPosition.position = 12345;
SomeFunctionInFileB();
}
// FILE_B.c****************************************************
void SomeFunctionInFileB(void)
{
// After the following lines execute I see all zeros in the flash memory.
WriteByteToFlash(CurrentPosition.bytes[0];
WriteByteToFlash(CurrentPosition.bytes[1];
WriteByteToFlash(CurrentPosition.bytes[2];
WriteByteToFlash(CurrentPosition.bytes[3];
}
Now, If I pass a long to SomeFunctionInFileB(long temp) and then store it into CurrentPosition.bytes within that function, and finally call WriteBytesToFlash(CurrentPosition.bytes[n]... it works just fine.
It appears as though the CurrentPosition Union is not global. So I tried changing the union definition in the header file to include the extern keyword like this...
extern xdata union
{
long position;
char bytes[4];
}CurrentPosition;
and then putting this in the source (.c) file...
xdata union
{
long position;
char bytes[4];
}CurrentPosition;
but this causes a compile error that says:
C:\SiLabs\Optec Programs\AgosRot\MotionControl.c:76: error 91: extern definition for 'CurrentPosition' mismatches with declaration.
C:\SiLabs\Optec Programs\AgosRot\/MotionControl.h:48: error 177: previously defined here
So what am I doing wrong? How do I make the union global?
Is FILE_A.h really MotionControl.h? If so I think the fix is to define a union type in the header:
typedef
union xdata
{
long position;
char bytes[4];
} xdata;
And declare a global variable of that type elsewhere in a header file (maybe the same one):
extern xdata CurrentPosition; // in a header file
Finally define the global variable in a C file exactly once. Maybe in file_a.c:
xdata CurrentPosition;
Of course a better fix might be to pass the xdata variable you want to write out to flash to SomeFunctionInFileB() so you don't have to depend on a global variable, which are well known to be problematic when not very, very carefully used. And there seems to be no good reason to not pass the data as a parameter:
// in a header file
void SomeFunctionInFileB( xdata const* pPosition);
void SomeFunctionInFileB( xdata const* pPosition)
{
// After the following lines execute I see all zeros in the flash memory.
WriteByteToFlash(pPosition->bytes[0];
WriteByteToFlash(pPosition->bytes[1];
WriteByteToFlash(pPosition->bytes[2];
WriteByteToFlash(pPosition->bytes[3];
}
And call it like so:
int main.c(void)
{
CurrentPosition.position = 12345;
SomeFunctionInFileB( &CurrentPosition);
}
Ideally you need a typedef for the union and an extern declaration in FILE_A.h and the actual definition of the union in FILE_A.c.
-
// FILE_A.h
typedef union
{
long position;
char bytes[4];
} Position;
extern Position CurrentPosition; // declaration
-
// FILE_A.c
#include "FILE_A.h"
Position CurrentPosition; // definition
int main(void)
{
CurrentPosition.position = 12345;
SomeFunctionInFileB();
return 0;
}
-
// FILE_B.c
#include "FILE_A.h"
void SomeFunctionInFileB(void)
{
// now there will be valid data in the flash memory.
WriteByteToFlash(cp.bytes[0];
WriteByteToFlash(cp.bytes[1];
WriteByteToFlash(cp.bytes[2];
WriteByteToFlash(cp.bytes[3];
}
-
You haven't instantiated the union.
You need :
// FILE_A.c****************************************************
#include "File_a.h"
CurrentPosition cp;
int main(void)
{
cp.position = 12345;
SomeFunctionInFileB();
}
// FILE_B.c****************************************************
#include "File_a.h"
extern CurrentPosition cp;
void SomeFunctionInFileB(void)
{
// now there will be valid data in the flash memory.
WriteByteToFlash(cp.bytes[0];
WriteByteToFlash(cp.bytes[1];
WriteByteToFlash(cp.bytes[2];
WriteByteToFlash(cp.bytes[3];
}
If sizeof(long) is not 4, then endianess comes into play...
consider
union{
long position
char bytes[sizeof long];
}

Resources