NO

Author Topic: Can one declare and initialize a pointer in one statement?  (Read 2598 times)

Zooby

  • Guest
Can one declare and initialize a pointer in one statement?
« 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;

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Can one declare and initialize a pointer in one statement?
« Reply #1 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.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Zooby

  • Guest
Re: Can one declare and initialize a pointer in one statement?
« Reply #2 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.