1

I have the following structure:

typedef struct
{
    uint8* buffer;
    uint32 bufferLength;
} SRequest;

and a function:

void somefunction(const SRequest* request)
{
    //The following initialization of the pointer to static structure is not working
    static SRequest* request_ptr = {.buffer = request->buffer, 
                                    .bufferLength = 0};
    .
    .
}

Is there another way I can initialize the request_ptr?

Divanov
  • 11
  • 2
  • 2
    What do you mean with "another way"? Checking for NULL and using malloc()? – Adriano Repetti Dec 23 '15 at 14:34
  • You need to allocate memory to the pointer, see this http://stackoverflow.com/questions/11709929/how-to-initialize-a-pointer-to-a-struct-in-c – terence hill Dec 23 '15 at 14:39
  • I mean another way to initialize the request_ptr than this one: static SRequest* request_ptr = {.buffer = request->buffer, .bufferLength = 0}; And my intention is not to initialize the buffer pointer to NULL – Divanov Dec 23 '15 at 14:40
  • You have to point the pointer at a valid, initialized structure. A *pointer* is, well, a *pointer*. It has to *point* to something real. – Andrew Henle Dec 23 '15 at 14:41
  • Why do you need `request_ptr` ? How do you plan to use that pointer later? – Jabberwocky Dec 23 '15 at 14:59
  • I will make a new function call with this `request_ptr` as parameter. My intention is to get the `request->buffer` pointer and depending on some conditions the `request_ptr->bufferLength` will be either equal to `request->bufferLength` or to some other pre-calculated value. – Divanov Dec 23 '15 at 15:29
  • We are probably dealing with an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) here. – Jabberwocky Dec 23 '15 at 16:33

2 Answers2

2

You are trying to initialize a pointer with a value that is not a pointer but rather a struct. That cannot possibly work.

Moreover, initializers for objects with static storage duration must be compile-time constants. This constraint makes sense when you recognize that initialization of such objects happens (logically) before program execution starts. All file-scope variables have static duration, as do local variables declared static, such as your request_ptr. You are trying to initialize your static pointer with a value that is not a compile-time constant.

It is unclear what you are really after, here, but if you are trying to create, on each call, an SRequest that points to the same buffer that the argument does, then that could be this:

void somefunction(const SRequest* request)
{
    SRequest local_request = {.buffer = request->buffer, 
                              .bufferLength = 0};
    .
    .
    .
}

Note there that I make local_request an automatic variable rather than a static one, so that its initializer is applied on every call, and that I make it a struct, not a pointer to one.

On the other hand, if your objective is to have an SRequest that is initialized on the first call to the function with values derived from the argument to that call, and thereafter persists across function calls, then you want something like this:

void somefunction(const SRequest* request)
{
    static int initialized = 0;
    static SRequest local_request;

    if (!initialized) {
        local_request.buffer = request->buffer;
        local_request.buffer_length = 0;
        initialized = 1;
    }
    .
    .
    .
}

You might be able to do without the initialized variable if a member of local_request would be suitable in its stead. For instance, perhaps it would work for your purposes to instead test weather local_request.buffer is NULL.

In any event, it's unclear to me why you would want the local variable to designate a pointer instead of a structure. There are potential reasons why you might indeed want that, but it's not evident that any of those apply.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

As the title of the question seems more explicit than the cited one by terence hill, it might be interesting to have the answer here.

You have to use "compound literal", which is defined in the C99 standard:

void somefunction(const SRequest* request)
{
    //The following initialization of the pointer to static structure is not working
    static SRequest* request_ptr = &(SRequest){
        .buffer = request->buffer,
        .bufferLength = 0};
}
Puck
  • 2,080
  • 4
  • 19
  • 30
  • Using a compound literal that way is kinda cool, but it does not work here because the initializer is not a compile-time constant. – John Bollinger Dec 23 '15 at 15:42
  • Also, it's a bit roundabout to declare a static pointer to (necessarily) static and otherwise unreferenced data, when you could instead just declare the data directly (i.e. `static SRequest request_data = ...`) and obtain a pointer to it at need via the address-of operator. – John Bollinger Dec 23 '15 at 15:44