5 Secrets of Blogger (Service) You Don't Know

Recent Posts

Declaration, Initialization & Accessing of Pointer Variable

Declaration and initialization of pointer variable In C and C++ is similar. So we can follow one of them. For better, let choose C.

Declaration, Initialization & Accessing of Pointer Variable

Declaration of Pointer Variable

Pointer variable must be declared before using it as we know in C Programming Language, every variable must be declared before using. Generally if we declare an integer type variable that hold a unique memory address from the computer system, we do it in C like below:
int variable_name;
Similarly, we can declare a pointer variable but adding an asterisk ('*' sign) after data type and before variable name. For that there are three type of pointer declaration style in C. They are:

1. int* variable_name;
2. int * variable_name;
3. int *variable_name;

Which style will you choose?
Most of expert programmer suggest to use last one. Because the following reasons:

1. If we want to declare only variable not pointer variable in line beside a pointer variable declaration then we can't do it in style 1 and 2.


int* pointer_variable, normal_variable;
int * pointer_variable, normal_variable;

Both of them have declared two variable as pointer variable but we want to declare first as pointer variable and second as normal variable.

This problem is solved by following the third style.


int *pointer_variable, normal_variable;

Here the statement tells the compiler that first variable is pointer and second is normal variable.

2. Another reason to choose the third style is it is similar to access the value of a pointer variable (we will discuss it later).

So finally we will follow the third style to declare a pointer variable. For that the standard form of declaration of pointer variable is:
data_type *pointer_variable_name;
Here data_type may be int, float, char etc.


For integer type variable: int *pointer_variable;

For float type variable: float *pointer_variable;

For character type variable: char *pointer_variable;

Initialization of Pointer Variable

At beginning we have learned that pointer can hold the address of another normal variable. In initialization the most important is pointer can point same data type. Only void pointer (discussed later) can point any data type. If we want to point integer type variable then pointer variable must be declared as integer. In case, we need to point another type variable then what will we do? Great question. You have already heard casting. By casting we can do that. We will see it after a while. Now we will learn how to initial a pointer variable.

To initial a pointer variable first we have to declare a variable whom to be pointed by a pointer variable. So the form of initialization of pointer variable is:
data_type variable_name, *pointer_variable;pointer_variable = &variable_name;
The first statement is about declaration and next statement is about initialization. This statement tells the compiler that the address of variable_name (which is a unique address of memory of computer system for declaration before) will be assigned to pointer_variable

We can declare and initial in one statement as the following:
int variable_name, *pointer_variable = &variable;
For that we have to always declare variable_name before pointer_variable declaration.

Now if we want to compile the following statement it will print the address of variable_name.
printf("Address: %u\n",pointer_variable);
So after initialization, the address and the value of variable_name and address of pointer_variable will be same.

The most interesting in pointer is it's flexibility. You can initial one pointer variable to same type different variable as same as below:
int var_one, var_two, *pointer_var;
pointer_var = &var_one;
pointer_var = &var_two;
Similarly, you can multiple pointers to point to one variable but same data type.
int variable_name, *pointer_one, *pointer_two;
pointer_one = &variable_name;
pointer_two = &variable_two;

NULL or Zero Pointer

If we initial a pointer variable with NULL or 0, this pointer can be told as NULL or Zero Pointer. It is similar as below:
int *pointer_variable = NULL;
int *pointer_variable = 0;

Get The Value Of Pointers / Accessing Variable Through Its Pointer

After initialization pointer variable, if we want to access the value of pointer, we have to  put an asterisk ('*' sign) character just before the pointer variable. If pointers_var be a pointer variable and initialized before then we can get value by following the statement:
int n = *pointer_var;
 You can run the following program to see details:
#include <stdio.h>
int main()
    int value, *ptr, n;
    value = 1000;
    ptr = &value;
    n = *ptr;
    printf("Value of Pointer: %d\n",n);

    return 0;

We have learned how to access the address of variable. But you can also get the value from the address of variable (without pointer declaration and initialization) by adding * before (&variable_name) . To check this you can run the following program.
#include <stdio.h>
int main()
    int var;

    var = 80;

    printf("The value of var: %d\n", var);
    printf("The address of var: %u\n", &var);
    printf("The value of *(&var): %d\n", *(&var));

Looking for more effecive Course?

C Programming For Beginners by Udemy icon
$15 (Original $145)
30-Day Money-Back Guarantee
  • 6 hours on-demand video
  • 1 Article
  • 2 Supplemental Resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion