Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"expected specifier-qualifier-list before ‘*’ token" error in variadic function

Tags:

c

pointers

struct

I am trying to write a function which will configure a peripheral of a microcontroller in C. To do so, I have used va_arg. Here is the function:

void init_peripheral(int ID, ...){
    va_list device;
    va_start(device, ID);
    io * temp; // No error here
            //io is a structure 
            //IO, USART, LCD are 01, 02, 03.
    (*temp).portB.set = set_portB;

    if( ID == IO ){
        io* config_io; // error:expected specifier-qualifier-list before ‘*’ token
        config_io = va_arg(device, *io);
        (*config_io).portB.set = set_portB;
        (*config_io).portB.clr = clr_portB;
        (*config_io).portB.mkin = mkin_portB;
        (*config_io).portB.mkout = mkout_portB;

        (*config_io).portC.set = set_portC;
        (*config_io).portC.clr = clr_portC;
        (*config_io).portC.mkin = mkin_portC;
        (*config_io).portC.mkout = mkout_portC;

        (*config_io).portD.set = set_portD;
        (*config_io).portD.clr = clr_portD;
        (*config_io).portD.mkin = mkin_portD;
        (*config_io).portD.mkout = mkout_portD;
    }

    else if( ID == LCD ){       
        lcd *config_lcd;
        config_lcd = va_arg(device, *lcd);
         //Set necessary params here
    }

    else if( ID == USART){
        usart *config_usart;
        config_usart = va_arg(device, *usart);
        (*config_usart).init = usart_init;
        (*config_usart).transmit = usart_transmit;
        (*config_usart).receive = usart_receive;
    }


    va_end(device);
}

I don't get an error for the line io * temp, but I do get an error for io * config_io;
Here is the io struct:

struct __io__{
    struct __port__ portB;
    struct __port__ portC;
    struct __port__ portD;
};
typedef struct __io__ io;
like image 933
Vishwanath Avatar asked Dec 04 '25 15:12

Vishwanath


2 Answers

The problem is actually in the next line, with the va_arg macro. You're sending a dereferenced pointer instead of a type:

config_io = va_arg(device, *io);       // Should be io*

config_lcd = va_arg(device, *lcd);     // Should be lcd*

config_usart = va_arg(device, *usart); // Should be usart*

Here's your code, which has the compilation problems.
Here's the fixed version.

like image 138
Eitan T Avatar answered Dec 06 '25 05:12

Eitan T


The code looks correct. And since you can declare temp, I see no reason why io_config should suddenly fail. What you should try:

  1. Add #under config_io before the function; maybe a macro config_io is defined.

  2. Replace io with struct __io__ just to see whether that would compile. If it doesn't move the variable declaration outside the if() block; maybe your compiler version doesn't support it.

  3. Tell the compiler to show you the preprocessed source code (i.e. after all macros have been expanded). Maybe that gives you a hint.

like image 26
Aaron Digulla Avatar answered Dec 06 '25 03:12

Aaron Digulla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!