Pointers in C

A Pointer is a variable in C that contains address of another variable.

A pointer variable is denoted by

int *p; //This means p is a pointer to an integer variable

* is Value at Address operator

& is Address of Operator

int x =15; //x is an integer variable

int *ptr1 = &x //Now p contains the address of x..i.e ptr is a pointer to x

int **ptr2 = &ptr1 //And ptr2 contains the address of ptr1..so prt2 is a pointer to a pointer


#include <stdio.h>
#include <conio.h>

int main()
{
    int x=15;
    int *ptr1;
    int **ptr2;

    ptr1 = &x; //Now ptr contains the address of x
    ptr2 = &ptr1; //ptr2 contains the address of ptr

    printf("\nAddress of x = %u", &x);
    printf("\nValue of x = %d", x);
    printf("\nValue of x = %d", *(&x));

    printf("\n ");
    printf("\nAddress of ptr1 = %u", &ptr1);
    printf("\nValue of ptr1 / Address of x = %d", ptr1);
    printf("\nValue at address pointed by ptr1/Value of x = %d", *ptr1);

    printf("\n ");
    printf("\nAddress of ptr2 = %u", &ptr2);
    printf("\nValue of ptr2 / Address of ptr1 = %d", ptr2);
    printf("\nValue at address pointed ptr2 / Address of x= %d", *ptr2);
    printf("\nValue at address pointed by ptr1 which is pointed by ptr2/Value of x = %d", **ptr2);

    getch();
    return 0;
}

Snapshot of the o/p

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s