C Programming

Introduction to Pointers

In C, variables are used to hold data values during the execution of a program. Every variable when declared occupies certain memory location. Pointers are used to hold the memory addresses.

Pointers involve two operators:

& ( ampersand ) : Used to find address of a variable. For example

// The output of this program can be different
// in different runs. Note that the program
// prints address of a variable and a variable
// can be assigned different address in different
// runs.
#include <stdio.h>

int main()
{
    int x;

    // Prints address of x
    printf("%p", &x);

    return 0;
}

* (Astrik) : Used for two things

1) Declaration (To declare a pointer variable)


// C program to demonstrate declaration of
// pointer variables.
#include <stdio.h>
int main()
{
    int x = 10;
    // 1) Since there is * in declaration, ptr
    // becomes a pointer varaible (a variable
    // that stores address of another variable)

    // 2) Since there is int before *, ptr is
    // pointer to an integer type variable
    int *ptr;


    // & operator before x is used to get address
    // of x. The address of x is assigned to ptr.
    ptr = &x;

    return 0;
}

We can also initialize pointer during declaration.


// C program to demonstrate that a pointer
// variable can be initialized during
// declaration
#include <stdio.h>

int main()
{
    int x = 10;

    // ptr is initialized with address of x
    int *ptr = &x;

    return 0;
}

2) Dereferencing (To get value at an address)

// C program to demonstrate * operator as a
// dereferenicng operator.
#include <stdio.h>

int main()
{
    int x = 10;

    // ptr is initialized with address of x
    int *ptr = &x;

    // This line prints address of x (or value
    // of ptr)
    printf("ptr = %p\n", ptr);

    // To get the value at address ptr, we use *
    // This line prints value at address stored
    // by ptr.
    printf("*ptr = %d\n", *ptr);

    // We can also use ptr as lvalue (Left hand
    // side of assignment)
    *ptr = 20;   // Value at address is now 20

    // This prints 20
    printf("After doing *ptr = 20, *ptr is %d\n", *ptr);

    // This also prints 20 (So we could change
    // value of x using its address. This is
    // power of pointers!!)
    printf("After doing *ptr = 20, x is %d\n", x);

    return 0;
}

We can create pointer to any data type.

int *p; //an integer pointer

float *p ; // float pointer

char *p; // A character pointer

Exercise : Guess the output of below program

#include<stdio.h>

int main()
{
    // A pointer variable which stores the address of
    // an integer
    int *p;

    // An integer
    int n ; 
    n = 10 ;

    // Ppinter stores the address of integer n
    p = &n ;

    // Printing the respective values of n and p
    printf("Value of n = %d\n", n) ;
    printf("Value of pointer p = %u\n", p);
    printf("The value of n using pointer p = %d\n", *p);

    // Printing the address of p and n
    printf("Address of n = %u\n", &n) ;
    printf("Address of n using pointer p = %u\n", p);
    printf("Address of p = %u\n", &p);
    return 0 ;
}

Recent Comments

Feedback

For any feedback,email us at feedback@geeksforgeeks.org

Report a Bug

For any report or bug,email us at support@geeksforgeeks.org