C - Pointer Pointer Declaration


You can declare a pointer to a variable of type int with the following statement:

int *pnumber;

The type of the variable with the name pnumber is int*.

It can store the address of any variable of type int.

You can also write the statement like this:

int* pnumber;

The statement just creates the pnumber variable but doesn't initialize it.

You should always initialize a pointer when you declare it.

You can initialize pnumber so that it doesn't point to anything by rewriting the declaration like this:

int *pnumber = NULL;

NULL is a constant for point value and is the equivalent of zero for numeric value.

NULL is a value that's not to point to any location in memory.

To initialize your variable pnumber with the address of a variable you've already declared, you use the address of operator, &:

int number = 99;
int *pnumber = &number;

Now the value of pnumber is the address of the variable number.

The declaration of number must precede the declaration of the pointer that stores its address.

You can declare regular variables and pointers in the same statement, for example:

double value, *pVal, fnum;

This statement declares two double-precision floating-point variables, value and fnum, and a variable pVal of type "pointer to double."

Related Topics