Search code examples
cstructdeclarationtypedefstorage-class-specifier

Typedef structure's member being static or not in c


I think this none sense doesnt that? because after creating instance from that variable. It will initialize that variable whole, will not behave differently to members an will initialize all of them, doesn't that? As I know static variables are global variable but their usage are restricted to specified function or source file.

typedef struct
{
    const uint32_t mainpointer;
    static uint32_t currentpointer;    
    uint16_t num_el;    
    uint8_t num_byte;    
}arrayPushCirc_doublebuffer_instance;

void main(void)
{
arrayPushCirc_doublebuffer_instance struction;
function1(struction);
}

Solution

  • In C data members of a structure may not have storage class specifiers.

    So this declaration of a data member

    static uint32_t currentpointer;
    

    is invalid in C.

    According to the C Grammar data member declarations may have only the following specifiers

    specifier-qualifier-list:
        type-specifier specifier-qualifier-listopt
        type-qualifier specifier-qualifier-listopt
    

    On the other hand, you may use the storage class specifier static for declarations of objects in the file scope or a function scope.

    For example

    #include <stdint.h>
    
    typedef struct
    {
        const uint32_t mainpointer;
        uint32_t currentpointer;    
        uint16_t num_el;    
        uint8_t num_byte;    
    }arrayPushCirc_doublebuffer_instance;
    
    static arrayPushCirc_doublebuffer_instance struction; 
    
    int main( void )
    {
        //...
    }
    

    Pay attention to that according to the C Standard the function main without parameters shall be declared like

    int main( void )