Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between int *p = &a and q = &a

Tags:

c

pointers

Learner question. I can declare and initialize a pointer in two different way

int a = 10;
int *p = &a;

also

int a = 10;
int *q;
q = &a;

I want to know what is the difference between two and how does it work in the memory?

like image 595
Anirban Nag 'tintinmj' Avatar asked Dec 13 '25 23:12

Anirban Nag 'tintinmj'


1 Answers

Those two are the same :

int *p; // declaration
p = &a; // assignment

and in the other you are combining the two steps together into one:

int *p=&a; // declaration and assignment

And if you have some compiler optimizations ON, the compiler might combine the two steps.

like image 115
brokenfoot Avatar answered Dec 15 '25 12:12

brokenfoot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!