Signup/Sign In
LAST UPDATED: FEBRUARY 20, 2021

Declaring and Initializing a pointer

In this tutorial, we will learn how to declare, initialize and use a pointer and also about NULL pointer and its uses.

Check these topics before continue reading:

Declaration of C Pointer variable

The general syntax of pointer declaration is,

datatype *pointer_name;

The data type of the pointer and the variable to which the pointer variable is pointing must be the same.

Initialization of C Pointer variable

Pointer Initialization is the process of assigning address of a variable to a pointer variable. It contains the address of a variable of the same data type. In C language address operator & is used to determine the address of a variable. The & (immediately preceding a variable name) returns the address of the variable associated with it.

int a = 10;
int *ptr;       //pointer declaration
ptr = &a;       //pointer initialization

initialize pointer variable in C

Pointer variable always points to variables of the same datatype. For example:

float a;
int *ptr = &a;       // ERROR, type mismatch

While declaring a pointer variable, if it is not assigned to anything then it contains garbage value. Therefore, it is recommended to assign a NULL value to it,

set pointer variable value

A pointer that is assigned a NULL value is called a NULL pointer in C.

 int *ptr = NULL;

Using the pointer or Dereferencing of Pointer

Once a pointer has been assigned the address of a variable, to access the value of the variable, the pointer is dereferenced, using the indirection operator or dereferencing operator *. Consider the following example for better understanding.

#include <stdio.h>

int main()
{
    int a;  
    a = 10;
    int *p = &a;     // declaring and initializing the pointer

    //prints the value of 'a'
    printf("%d\n", *p);  
    printf("%d\n", *&a);  
    //prints the address of 'a'
    printf("%u\n", &a);    
    printf("%u\n", p);     
    
    printf("%u\n", &p);    //prints address of 'p'
    
    return 0;
}


10
10
3795480300
3795480300
3795480304

Points to remember while using pointers

  • While declaring/initializing the pointer variable, * indicates that the variable is a pointer.

  • The address of any variable is given by preceding the variable name with Ampersand &.

  • The pointer variable stores the address of a variable.

  • The declaration int *a doesn't mean that a is going to contain an integer value. It means that a is going to contain the address of a variable storing integer value.

  • To access the value of a certain address stored by a pointer variable * is used. Here, the * can be read as 'value at'.

Since we have learned the basics of Pointers in C, you can check out some C programs using pointer.

Read More:



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight