Pointer C Programing

Answer: In C every variable must be declared according to the type of value the variable stores. Since pointer variable contain addresses that belong to a seprate data type, they must be declared as pointers before we use them. The syntax of declaring pointer variable is:

data-type *pt_var_name;

The above declaration tells three things about the variable pt_var_name to the compiler.

  • The (*) asterisk tells that the variable pt_var_name is apointer variable.
  • pt_ var_name needs a memory location.
  • pt_var _name points to a variable of type data type.

 

For Example:

int * ptr;

declares the variable ptr as a pointer variable that points to an integer data type.

The above declaration instructs the compiler to allocate memory location for pointer variable ptr. Since the pointer variables are not initialized, they point to some unknown location.

 

Pointer Declration

The process of assigning the address of a variable to a pointer variable is known as initialization. Once a pointer variable has been, declared we can use the assignment operator to initialize the variable.

For example:

int sum;

int*ptr;      /*Declaration*/

ptr=∑ /* initialization*/

we can also combine the declaration and initialization in one statement:

int*ptr=∑

The only requirement is that the variable sum must be declared before the initialization takes place.

At the time of initialization, we must ensure that the pointer variables always point to the corresponding type of data.