Pelles C forum

C language => Beginner questions => Topic started by: Zooby on April 04, 2016, 12:27:17 AM

Title: Can one declare and initialize a pointer in one statement?
Post by: Zooby on April 04, 2016, 12:27:17 AM
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;
Title: Re: Can one declare and initialize a pointer in one statement?
Post by: frankie on April 04, 2016, 01:18:01 PM
This is legal and is called Initialized variable declaration.
This is a very basic thing, you may want read a tutorial (http://www.tutorialspoint.com/cprogramming/c_variables.htm).
Title: Re: Can one declare and initialize a pointer in one statement?
Post by: Zooby on April 05, 2016, 12:15:04 AM
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.