-3

While i was looking over linux device driver code, i enountered the following code, i could not understand why they mentioned like static strucure.

static struct resource imx_uart1_resources[] = {
[0] = {
    .start = 0x00206000,
    .end = 0x002060FF,
    .flags = IORESOURCE_MEM,
     },
 [1] = {
.start = (UART1_MINT_RX),
.end = (UART1_MINT_RX),
.flags = IORESOURCE_IRQ,
  },
};

Can anyone explain what is the need of "static Structure" and explain what its necessity ?

Photon001
  • 145
  • 9
  • 1
    There's plenty of other examples on SO about what static means. In terms of why it is necessary, you haven't shown enough code for anyone to give you an answer. – Joe Feb 26 '16 at 07:56
  • 3
    Possible duplicate of [What does "static" mean in a C program?](http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program) – Joe Feb 26 '16 at 07:56
  • No @Joe. My question is what is the difference between struct and static struct little edit has been made . TIA – Photon001 Feb 26 '16 at 08:05
  • Well static has the same behaviour regardless of whether it is applied to a struct or anything else. In terms of why this one is static we would need the context of its use to know. – Joe Feb 26 '16 at 08:07
  • `imx_uart1_resources` is the thing that is static. It's an array of `struct resource`. – molbdnilo Feb 26 '16 at 10:07

1 Answers1

2

The keyword static has many meanings that depend on the context where the keyword is used.

If the array of the structure is declared inside a function then it means that the array has the static storage duration instead of the automatic storage duration.

Consider the following example

#include <stdio.h>

struct A
{
    int x;
};

struct A * f( void )
{
    struct A a = { 10 };

    return &a;
}

int main( void ) 
{
    struct A *pa = f();
    printf( "pa->x = " );
    printf( "%d\n", pa->x );
}

Here function f defines an object of type struct A with the automatic storage duration and returns pointer to this object. As the object will not alive after exiting the function then the program has undefined behaviour. For example running this program using an inline compiler I got the following result

pa->x = 1073981248

On the other hand if within the function the object will have the static storage duration then the program will be well-formed because the object willl be alive after exiting the function

#include <stdio.h>

struct A
{
    int x;
};

struct A * f( void )
{
    static struct A a = { 10 };

    return &a;
}

int main( void ) 
{
    struct A *pa = f();
    printf( "pa->x = " );
    printf( "%d\n", pa->x );
}

The program output will be as expected

pa->x = 10

The object will keep its value between calls of the function. For example

#include <stdio.h>

struct A
{
    int x;
};

struct A * f( void )
{
    static struct A a = { 10 };

    return &a;
}

int main( void ) 
{
    struct A *pa = f();

    printf( "pa->x = " );
    printf( "%d\n", pa->x );

    pa->x = 20;

    pa = f();

    printf( "pa->x = " );
    printf( "%d\n", pa->x );
}

The program output is

pa->x = 10
pa->x = 20

If the array of the structure is declared outside any function then it means that (apart from the static storage duration by default) the array name has internal linkage that is it is invisible outside the compilation unit where it is declared.

If several compilation units include the same declaration of the array with keyword static then each compilation unit has its own separate instance of the array.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335