Using Pointers or variables

Aug 19, 2016 at 10:24pm
Hey,
I am a beginner user of c++.
and I have a question concerning pointers.
Is there any deference for using regular variables, while programming, or pointers to avoid messing up with the variables?
Aug 19, 2016 at 10:59pm
Could you give a snippet of code? I am not sure that I can understand what you are asking without seeing a code example. Deference is not the same as dereference, so I am confused by your question.
Aug 20, 2016 at 1:19am
for example :
ex1:
1
2
3
4
int a;
cout<<"Insert value: ";
     cin>>a;
cout<<"the input is : "<<a<<endl;

ex2:
1
2
3
4
5
int a, *pa;
pa=&a;
cout<<"Insert value :";
    cin>>*pa;
cout<<"the input is : "<<a<<endl

I would like to know if on a "Much longer and complicated program" it is better to use pointers to get values or just use regular variables
Aug 20, 2016 at 1:24am
Use values (regular variables).
Use pointers only if something can't be done without using pointers.
Aug 20, 2016 at 1:27am
Between those two examples you just posted, ex1 is much better. I actually prefer variables instead of pointers in almost every situation. Variable references are always guaranteed to be allocated, whereas pointer references lack that guarantee.
Topic archived. No new replies allowed.