1

I have this class declaration that should set hour, minute and second variables within my Time class:

class Time
{
public:
     int hour; //0-23
     int minute; //0-59
     int second; //0-59
};

Now, This next code should work for that class definition:

Time clock;
Time *clockPtr = &clock;

clock.hour=8;
clock.minute=12;
*clockPtr.second=0;

Will this work? I think that because the pointer is pointing to the value of the address &clock it should work. Correct me if I am wrong please.

3
  • 2
    Be careful with operator precedence. Commented Nov 4, 2014 at 20:23
  • 1
    Have you tried compiling it? Commented Nov 4, 2014 at 20:25
  • @RedX ... with all warnings on. Commented Nov 4, 2014 at 20:26

1 Answer 1

3

As mentioned in comments you'll need to take care about the precedence of the * and . dereferenccing operators. Just change that line

 *clockPtr.second=0;

to

 (*clockPtr).second=0;

or as @Thomas Matthews pointed out

 clockPtr->second=0;

LIVE DEMO

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

1 Comment

The precedence issue can be eliminated by using the -> operator: clockPtr->second = 0;

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.