0

I can solve the problem using pointers. The goal is to print some stuff out

struct Nerd
{
int zitnuml;
int sc_hours;
};

int main()
{
Nerd luis; 
Nerd *ptr = &luis;
ptr->sc_hours = 100; 
ptr->zitnuml = 5;   

cout << luis.sc_hours << endl;
cout << luis.zitnuml << endl;
}

///Why write the above if I can just do this instead:

int main()
{
Nerd luis;
luis.sc_hours=100;
luis.zitnuml=5;

cout << luis.sc_hours << endl;
cout << luis.zitnuml << endl;
}

both main function print the same thing, so why use pointers?

Luis Ong
  • 21
  • 5
  • *I can solve the problem without using pointers* – Luis Ong Sep 10 '19 at 04:39
  • 2
    A general rule of thumb - don't use pointers if you can avoid it. – R Sahu Sep 10 '19 at 04:40
  • 1
    "why use pointers?" Only the one who wrote this code knows... – xskxzr Sep 10 '19 at 04:42
  • @RSahu They are teaching us how to use pointers with structs and classes. So can you show me a case where I must use pointers ? – Luis Ong Sep 10 '19 at 04:43
  • @LuisOng, please read the answers in the post that's marked as duplicate. If you still have questions, you can post another question, explaining why you were not able to find the answer to your question in that post. – R Sahu Sep 10 '19 at 04:45
  • @RSahu I'll just start my project and hopefully I'll realize when to use the pointers for structs and classes. Thank you – Luis Ong Sep 10 '19 at 04:48

0 Answers0