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.