Post Closed as "duplicate" by user40980, durron597, Ixrec, user22815, gnat of
2 added 111 characters in body
source | link

I am writing an application for my graduation coursework, in C, using GTK+ UI library. Sometimes I ask for user input, which I need to save for later use.

The doubt comes on how to store this data. I didn't want to create global variables because I was told countless times not to use them. However, I need to interchange data between functions which don't call each other directly (because of slot-signal pattern, I don't have a function that's always accessible after the event loop starts), thus needing some independent storage.

For this purpose I designed a getter-like function which reads similar to this:

char *store_data(char *data, int clear) {
    static char *stored_data = NULL;

    if(data == NULL) {
        if(clear) {
            stored_data = NULL;
        } else {
            return stored_data;
        }
    } else {
        stored_data = data;
    }

    return NULL;
}

Is such a construct OK or should I simply use a global variable instead?

I am writing an application for my graduation coursework, in C, using GTK+ UI library. Sometimes I ask for user input, which I need to save for later use.

The doubt comes on how to store this data. I didn't want to create global variables because I was told countless times not to use them. However, I need to interchange data between functions which don't call each other directly, thus needing some independent storage.

For this purpose I designed a getter-like function which reads similar to this:

char *store_data(char *data, int clear) {
    static char *stored_data = NULL;

    if(data == NULL) {
        if(clear) {
            stored_data = NULL;
        } else {
            return stored_data;
        }
    } else {
        stored_data = data;
    }

    return NULL;
}

Is such a construct OK or should I simply use a global variable instead?

I am writing an application for my graduation coursework, in C, using GTK+ UI library. Sometimes I ask for user input, which I need to save for later use.

The doubt comes on how to store this data. I didn't want to create global variables because I was told countless times not to use them. However, I need to interchange data between functions which don't call each other directly (because of slot-signal pattern, I don't have a function that's always accessible after the event loop starts), thus needing some independent storage.

For this purpose I designed a getter-like function which reads similar to this:

char *store_data(char *data, int clear) {
    static char *stored_data = NULL;

    if(data == NULL) {
        if(clear) {
            stored_data = NULL;
        } else {
            return stored_data;
        }
    } else {
        stored_data = data;
    }

    return NULL;
}

Is such a construct OK or should I simply use a global variable instead?

1
source | link

Global variable vs. local-static variable for storing state

I am writing an application for my graduation coursework, in C, using GTK+ UI library. Sometimes I ask for user input, which I need to save for later use.

The doubt comes on how to store this data. I didn't want to create global variables because I was told countless times not to use them. However, I need to interchange data between functions which don't call each other directly, thus needing some independent storage.

For this purpose I designed a getter-like function which reads similar to this:

char *store_data(char *data, int clear) {
    static char *stored_data = NULL;

    if(data == NULL) {
        if(clear) {
            stored_data = NULL;
        } else {
            return stored_data;
        }
    } else {
        stored_data = data;
    }

    return NULL;
}

Is such a construct OK or should I simply use a global variable instead?