2

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?

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

5

You dont need to dereference your pointer here:

if(modePtr->eraseSelect ==2){
    ...
}

Comments

5

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.