tags:

views:

59

answers:

4

Hello,

gcc 4.4.4 c89

I have the following code and 2 structures that have to be filled.

I have 3 functions that will fill the handles for each of the devices.

However, the device_type structure will need to increment from where the last function finished.

For example:

load_resources() starts at 0 and finishes at 9
    dev_types starts at 0 and finishes at 9

load_networks()  starts at 0 and finishes at 9
    dev_types starts at 10 and finishes at 19

load_controls()  starts at 0 and finishes at 9  
    dev_types starts at 20 and finishes at 29

However, as I don't want to use a static or global variable is there any way I can increment a value for this. So it will start where the last function finished.

Many thanks for any suggestions,

#define NUMBER_OF_DEVICES 10
#define NUMBER_OF_TYPES 3 /* resources
                             networks
                             controls */

int events(int evt);

int load_resources();
int load_networks();
int load_controls();

static struct device_table {
    int resource_handle;
    int network_handle;
    int control_handle;
} dev_tbl[NUMBER_OF_DEVICES];

struct device_types {
    size_t id;
    int dev_handle;
    int dev_type;
}dev_types[NUMBER_OF_DEVICES * NUMBER_OF_TYPES];

enum dev_name_types {RESOURCE, NETWORK, CONTROL};

/* Simulates the API calls, by returning a dummy handle */
int get_resources();
int get_networks();
int get_controls();

int main(void)
{
    srand(time(NULL));

    load_resources();
    load_networks();
    load_controls();

    return 0;
}

int load_resources()
{
    size_t i = 0;

    for(i = 0; i < NUMBER_OF_DEVICES; i++) {
        dev_tbl[i].resource_handle = get_resources();
        printf("dev_tbl[i].resource_handle [ %d ]\n", dev_tbl[i].resource_handle);
        dev_types[i].id = i;
        dev_types[i].dev_handle = dev_tbl[i].resource_handle;
        dev_types[i].dev_type = RESOURCE;
    }
}

int load_networks()
{
    size_t i = 0;

    for(i = 0; i < NUMBER_OF_DEVICES; i++) {
        dev_tbl[i].network_handle = get_networks();
        printf("dev_tbl[i].network_handle [ %d ]\n", dev_tbl[i].network_handle);
        dev_types[i].id = i;
        dev_types[i].dev_handle = dev_tbl[i].network_handle;
        dev_types[i].dev_type = NETWORK;
    }
}

int load_controls()
{
    size_t i = 0;

    for(i = 0; i < NUMBER_OF_DEVICES; i++) {
        dev_tbl[i].control_handle = get_controls();
        printf("dev_tbl[i].control_handle [ %d ]\n", dev_tbl[i].control_handle);
        dev_types[i].id = i;
        dev_types[i].dev_handle = dev_tbl[i].control_handle;
        dev_types[i].dev_type = CONTROL;
    }
}
+1  A: 

One option is to have each of your functions take the base index as a parameter, and returning the first available index:

int index = 0;
index = load_resources(index);
index = load_network(index);
// and so on

With this yout functions would look something like this:

int load_resources(int base_index) 
{
    size_t i = 0;
    size_t index;

    for(i = 0; i < NUMBER_OF_DEVICES; i++) {
        index = base_index + i;
        dev_tbl[index].resource_handle = get_resources();
        //
    }

    return index + 1;
}

By the way, your functions do have int return types, but do not return anything.

Schedler
That was just a sample code I typed up. So just a mistake of missing the return types.
robUK
+1  A: 

Hold a pointer or index variable within the structure itself. It's an indicator for how far the structure has been filled up generally.

Flinsch
+2  A: 

Why not change the prototypes to something like:

void load_resources(int*)

(they're not actually returning anything) then, in your main code, have:

int base = 0;
load_resources (&base);
load_networks (&base);
load_controls (&base);

Each function is then responsible for using and updating *base like this:

void load_resources (int *pBase) {
    size_t i = 0;

    for(i = 0; i < NUMBER_OF_DEVICES; i++, (*pBase)++) {  // <-- see here!
        dev_tbl[i].resource_handle = get_resources();
        printf("dev_tbl[i].resource_handle [ %d ]\n", dev_tbl[i].resource_handle);
        dev_types[*pBase].id = i;
        dev_types[*pBase].dev_handle = dev_tbl[i].resource_handle;
        dev_types[*pBase].dev_type = RESOURCE;
    }
}
paxdiablo
+1  A: 

There are only two ways to persist data from one scope to the next - on the stack and on the heap. Since you have already ruled out any global variables here, that would require you to use stack variables, by passing in a parameter and returning a value, or passing in a pointer to a parameter.

Nate