stdcerr
stdcerr

Reputation: 15598

return static structure array pointer with get()

I have a file in my project that has a local structure array defined like MyStruct_t myStruct[SIZE] which I can use well within that module. Now I would like to make it accessible to a different code module, so I came up with a function:

MyStruct_t *GetStruct(void) {
  return myStruct;
}

But when I call this from my different module, I get an error expression must be a modifiable lvalue. My caller looks like:

void myFunc(void) {
    MyStruct_t locStruct;
    locStruct = GetStruct();
}

How can I do this best?

Upvotes: 0

Views: 101

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 310940

For example you can do the following way

void myFunc( void ) 
{
   MyStruct_t locStruct[SIZE];
   MyStruct_t *p = GetStruct();
   memcpy( locStruct, p, SIZE * sizeof( MyStruct_t ) );
}

Or it might be enough to have a pointer to the first element of the array

void myFunc( void ) 
{
   MyStruct_t *locStruct = GetStruct();
}

In this case the syntax for accessing elements of the array will be the same as for example locStruct[i]

Upvotes: 1

ooga
ooga

Reputation: 15501

If myStruct is global in the other module, then you might be looking for the extern keyword. Just put this in the other module:

extern MyStruct_t myStruct[];

(I've assumed MyStruct_t is a typedefed name. Otherwise you'd need to put the keyword struct before it, of course.)

This also requires access to the declaration of MyStruct. Put that in a header file and include it in both modules.

Here's a simple example:

// main.h ------------------------------------------------

typedef struct {
  int a, b, c;
} AStruct;

// func.h ------------------------------------------------

void func(void);

// main.c ------------------------------------------------

#include <stdio.h>
#include "main.h"
#include "func.h"

AStruct as[100];

int main(void) {
    func();
    printf("%d,%d,%d\n", as[0].a, as[0].b, as[0].c);
    return 0;
}

// func.c ------------------------------------------------

#include "main.h"
#include "func.h"

extern AStruct as[];

void func(void) {
    as[0].a = 1;
    as[0].b = 2;
    as[0].c = 3;
}

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726489

GetStruct returns a pointer to myStruct[SIZE] element, not a MyStruct_t. Unless you really need a copy, make locStruct a pointer, like this:

MyStruct_t *locStruct = GetStruct();

note that locStruct is an array of MyStruct_t, of size SIZE.

If you do want to make a copy, dereference GetStruct()'s result:

MyStruct_t locStruct = *GetStruct();

This would produce a copy of the initial element of myStruct array.

Since GetStruct does not provide additional services except accessing myStruct, you might as well make myStruct global (extern) instead of static.

Finally, you could change GetStruct to access a specific element. This would also take care of detecting overruns - i.e. attempts at getting an element past the SIZE or at a negative index:

bool GetStruct(int index, MyStruct_t *ptr) {
    if (index < 0 || index >= SIZE) {
        return false;
    }
    *ptr = myStruct[index];
    return true;
}

Now the call of GetStruct() would look like this:

MyStruct_t locStruct;
if (GetStruct(5, &locStruct)) {
    // All good
} else {
    // Error
}

Upvotes: 2

James M
James M

Reputation: 16718

GetStruct returns a pointer, so your locStruct variable should be a pointer, too.

void myFunc(void) {
    MyStruct_t *locStruct;
    locStruct = GetStruct();
} 

Upvotes: 1

Related Questions