i made a test program, this line " *ps.Working_Hours=10;" generates mistake.
why ?

#include <cstdlib>
#include <iostream>

using namespace std;

class Salary
{
      public:
             int Working_Hours;
             float hour_rate;
             float x;
             };


int main(int argc, char *argv[])
{
    Salary s;
    float yy;
    s.Working_Hours=7;
    s.hour_rate=30.5;
    Salary* ps;
    ps=&s;
    *ps.Working_Hours=10;
    cout <<  "s.hour_rate="   <<s.hour_rate<<"\n"<<"ps->s.hour_rate"<<ps->Working_Hours<<endl;
         
    system("PAUSE");
    return EXIT_SUCCESS;
}

Recommended Answers

All 3 Replies

You created ps as a pointer, and you are attempt to use pointer of pointer to assign a value... I can't remember the exact syntax for this assignment... I am sure that someone else knows the exact syntax.

*ps.Working_Hours=10;

The "dot" operator has a higher prededence than the dereference operator. You can do it this way, but you need to use parentheses to make it work properly, which tends to make it klunky.

(*ps).Working_Hours = 10;

Alternatively, you can avoid this issue completely by using the "arrow" operator to handle all of that for you:

ps->Working_Hours = 10;

thank you it was so easy.
i missed simple mistake

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.