Reputation: 5243
I have such a struct
struct Request
{
char command[COMMAND_LENGHT];
char firstSetName[SET_NAME_LENGTH];
char secondSetName[SET_NAME_LENGTH];
char resultSetName[SET_NAME_LENGTH];
int input[sizeof(int) * 4];
};
and I have such a method
int parseToReadRequest(char * command, struct Request ** request)
{
printf("Command is : %s\n", command);
*request.firstSetName = "firs";
return 0;
}
As far as I understood when you need to pass a struct as a param you need to mark it with **
but anyway when I try to assign a struct value firstSetName
I get an error
Expression must have struct or union type
What am I doing wrong?
EDIT I get such error
Upvotes: 1
Views: 2087
Reputation: 223739
In this expression:
*request.firstSetName = "firs";
The member access operator .
has the highest precedence, so it assumes that request
is a struct
or union
type. However it is not. It is a pointer-to-pointer-to struct.
If your intent is to modify an instance of a struct
, you don't need a double pointer. Just a single will do:
int parseToReadRequest(char * command, struct Request *request)
Then, you would need to either put parenthesis around *request
to ensure the pointer is first dereferenced before using .
:
(*request).firstSetName = "firs";
Or you can use the pointer-to-member operator ->
which is cleaner:
request->firstSetName = "firs";
However there is another issue here. You're attempting to assign a value to an array. An array can't be assigned to directly. What you want instead is to use the strcpy
function which is used to copy strings:
strcpy(request->firstSetName, "firs");
Upvotes: 4
Reputation: 2718
You have to pass the structure by reference, ie. pass the pointer to it. You need to pass a double pointer only if you need to modify the pointer itself (for example dynamically allocate a structure inside the function).
The function should be: int parseToReadRequest(char * command, struct Request * request)
And inside the function you should access the structure fields like this: request->firstSetName
Upvotes: 1