Can one declare and initialize a pointer in one statement?

Started by Zooby, April 04, 2016, 12:27:17 AM

Previous topic - Next topic

Zooby

Hello, I am teaching myself C programming, and I have a question concerning the declaration and initialization of pointers.

Essentially, can one declare and initialize a pointer in one statement, like so?

int var;
int *p_var = &var;

I mean for the aforementioned to be the same as:

int var, *p_var;
p_var = &var;

frankie

This is legal and is called Initialized variable declaration.
This is a very basic thing, you may want read a tutorial.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Zooby

Thank you for the response.

I know it's pretty basic, and I know you can initialize variables when you declare them; it was just that, I was just introduced to pointers, and I was worried that in my example, it might change the value of var rather than p_var. Since you would do p_var=&var instead of *p_var=&var, but when you declare a pointer, you have the asterisk.