C language

Pointer arithmetic

Addition of a number to pointer: Pointer+ number

When we add some integer to the pointer then the address stored in the pointer is incremented by         

                        number * sizeof (datatype of pointer)

Assume space required for data types int & char are 2 & 1 bytesresp.

And we represent a particular block of memory filled with random values (not shown) as:

www.exploreroots.com

If we declare the pointers as

int *x:

int *y:

int i=3;// starting address of i is 9 and is 2 byte wide.

x=&i;

printf(“value at x is %d”, *x); // output we get is 3 i.e. value stored at location 9 & 10    

x=x+1; // now x points to space with starting address 11

printf(“value at x is %d”, *x); // output we get is a random value i.e. value stored at location 11 & 12

x=x+3; // we add 3*2=6 more i.e. now x points to address 17

printf(“value at x is %d”, *x); // output we get is a random value i.e. value stored at location 17 & 18

If we declare the pointers as

char *x:

char *y:

char i // starting address of i is 9 and is 1 byte wide.

x=&i;

printf(“value at x is %d”, *x); // output we get is the value stored at location 9

x=x+1; // we add 1*1=1 and now x points to space with starting address 10

printf(“value at x is %d”, *x); // output we get is a random value i.e. value stored at location 10

x=x+3; // we add 3*1=3 more i.e. now x points to address 13

Subtraction of a number from the pointer :      Pointer- number

When we subtract some integer from the pointer then the address stored in the pointer is decremented by         

                        number * sizeof (datatype of pointer)

Assume space required for data types int & char are 2 & 1 bytesresp.

And we represent a particular block of memory filled with random values (not shown) as:

www.exploreroots.com

If we declare the pointers as

int *x:

int *y:

int i=3;// starting address of i is 9 and is 2 byte wide.

x=&i;

printf(“value at x is %d”, *x); // output we get is 3 i.e. value stored at location 9 & 10    

x=x-1; // now x points to space with starting address 7

printf(“value at x is %d”, *x); //output we get is random value i.e. value stored at location 7& 8h

x=x-3; // we subtact 3*2=6 more i.e. now x points to address 1

printf(“value at x is %d”, *x); // output we get is random value i.e. value stored at location 1&2

If we declare the pointers as

char *x:

char *y:

char i // starting address of i is 9 and is 1 byte wide.

x=&i;

printf(“value at x is %d”, *x); // output we get is the value stored at location 9

x=x-1; // we subtract 1*1=1 and now x points to space with starting address 8

printf(“value at x is %d”, *x); // output we get is a random value i.e. value stored at location 10

x=x-3; // we subtract 3*1=3 more i.e. now x points to address 7

Subtraction of two pointers:          Pointer – Pointer

When we subtract two pointers, we get the number of variables of datatype as same as pointer that are stored between the two pointers.

If we use int pointers:

int *x;

int*y;

int i=1;

int y=2;

int d;

Suppose address of i is 3432 and that of j is 4456 and int requires 2 bytes

x=&I;

y=&j;

d= y –x; // answer get is (4456 – 3432)/2 = 1024/2 = 512.

// we divide by 2 as int takes 2 bytes

If we use char pointers:

 char *x;

char*y;

char i;

char y;

char d;

Suppose address of i is 3432 and that of j is 4456 and char requires 1 bytes

x=&I;

y=&j;

d= y –x; // answer get is (4456 – 3432)/1 = 1024/1 = 1024.

// we divide by 1 as char takes 1 bytes

All other types of operations on pointers like addition of pointersmultiplying pointer by numberdividing pointer by number are illegal.

8 Replies to “Pointer arithmetic

  1. Pingback: AR-15 Trigger
  2. You really make it appear really easy together with your presentation but I find this
    topic to be actually one thing that I think I might never
    understand. It sort of feels too complicated and very broad for
    me. I am having a look forward to your subsequent put up, I’ll try
    to get the hang of it!

  3. Pingback: do not trust
  4. что означает время 2211 когда лучше делать
    заговоры по лунному календарю на снятся мертвые родители как
    живые
    убегать и прятаться во сне, убегать от кого-то во сне
    по фрейду шуточный гороскоп для мужчины близнеца

Leave a Reply

Your email address will not be published. Required fields are marked *