Reputation: 3172
I have variable mode
, which I am declaring with the following line:
StatusRecord mode;
StatusRecord
is a struct
which holds several variables of different types.
I now want to create a pointer to mode
, and populate that pointer with some data by using a function to populate its attributes/ fields. I have tried doing this as follows:
StatusRecord mode;
StatusRecord *modePtr = &mode;
DataStore->getModeData(*modePtr);
Here, I am declaring the struct
variable, creating a pointer to it, and populating that pointer using the getModeData()
function. However, I now want to use an attribute of the struct ptr
I've just populated in a conditional statement:
if(*modePtr->eraseSelect ==2){
...
}
But I'm getting a compile error on this line that says:
Error: operand of '*' must be a pointer
Does this mean that the eraseSelect
attribute should be a pointer as well as 'modePtr`? How would I fix this error?
Upvotes: 2
Views: 4698
Reputation: 5369
Try this:
if(modePtr->eraseSelect ==2){
...
}
or this:
if((*modePtr).eraseSelect ==2){
...
}
So you can use "dot" syntax to reach fields of an instance or "arrow" syntax to reach fields of a pointer to the instance. In most cases, "arrow" is more suitable.
Upvotes: 7
Reputation: 50036
You dont need to dereference your pointer here:
if(modePtr->eraseSelect ==2){
...
}
Upvotes: 5
Reputation: 4343
The issue is at *modePtr->eraseSelect
.
The ->
is used to access member variables of pointers to objects. So, ptr->someMember
is equivalent to *(ptr).someMember
. You are mixing up the two, and so it doesn't make sense as you are dereferencing twice.
You should instead use modePtr->eraseSelect
instead.
Upvotes: 5