Showing posts with label Pointer. Show all posts
Showing posts with label Pointer. Show all posts

Thursday 4 August 2016

Pointers

Pointer to Pointer

Pointer to Pointer


#include<stdio.h>
void main()
{
                int a = 5;
                int *p;
                int **q;
                p = &a;
                q = &p;

                printf("\n a = %d", a);
                printf("\n &a = %p", &a);

                printf("\n  p = %p",  p);
                printf("\n  *p = %d",  *p);
                printf("\n  &p = %p", &p);

                printf("\n q = %p", q);
                printf("\n  *q = %p", *q);
                printf("\n  **q = %d", **q);
                printf("\n &q = %p", &q);
}

Output:
 a            = 5
 &a         = 0028FF44

 p            = 0028FF44
 *p          = 5
 &p         = 0028FF40

 q            = 0028FF40
 *q          = 0028FF44
 **q        = 5
 &q         = 0028FF3C

Pointer Arithmetic

Pointer Arithmetic


#include<stdio.h>
void main()
{
                char c = 'x', *pc;
                int i = 11, *pi;
                float f = 35.6, *pf;
                pc = &c;
                pi = &i;
                pf = &f;
                printf("\nValue of pc = Address of c = %p ", pc);
                printf("\nValue of pi = Address of i = %p ", pi);
                printf("\nValue of pf = Address of f = %p ", pf);

                pc++;
                pi++;
                pf++;

                printf("\nNow Value of pc = %p", pc);
                printf("\nNow Value of pi = %p", pi);
                printf("\nNow Value of pf = %p", pf);
}

Output:
Value of pc  =     Address of c       = 0028FF47
Value of pi   =     Address of i        = 0028FF3C
Value of pf   =    Address of f       = 0028FF34
Now Value of pc = 0028FF48
Now Value of pi = 0028FF40
Now Value of pf = 0028FF38

Pointers and address basics

Pointers and Address


#include<stdio.h>
void main()
{
                 int a = 10;
                float b = 35.6;
                int *p1;
                float *p2;
                p1 = &a;
                p2 = &b;
                printf("\nAddress of a = %p ", &a);
                printf("\nAddress of b = %p ", &b);

                printf("\nValue of a = %d %d %d", a, *p1, *(&a));
                printf("\nValue of b = %.1f %.1f %.1f", b, *p2, *(&b));

                printf("\nValue of p1 = Address of a = %p ", p1);
                printf("\nValue of p2 = Address of b = %p ", p2);

                printf("\nAddress of p1 = %p ", &p1);
                printf("\nAddress of p2 = %p ", &p2);
}

Output:
Address of a = 0028FF44
Address of b = 0028FF40
Value of a = 10    10   10
Value of b = 35.6   35.6   35.6
Value of p1 = Address of a = 0028FF44
Value of p2 = Address of b = 0028FF40
Address of p1 = 0028FF3C
Address of p2 = 0028FF38