Skip to main content

Pointer arithmetic in C

#include<stdio.h>
int main()
{
int a=24;

int* ptra = &a;
char b='3';
char* ptrb=&b;
printf("%d\n\n",ptrb); //size of char is 1
ptrb++;
printf("%d\n\n",ptrb); // add 1 to &b
ptrb--;
printf("%d\n\n",ptrb+1); // add 1 to &b
printf("%d\n\n",ptrb-1); // subtract 1 to &b
printf("%d\n\n",ptrb+1); // add 1 to &b
printf("%d\n\n",ptrb-2); // subtract 2*1 to &b
printf("%d\n\n",ptra);
printf("%d\n\n",ptra+1); // size of int is 4 so, it add 4 to &a/ptra
printf("%d\n",ptra-2); // size of int is 4 so, it subtract 2*4 to &a/ptra
return 0;
}

Comments

Popular posts from this blog

example of typecasting in c

/*Type Casting in C Typecasting allows us to convert one data type into other. */ //simple example to cast int value into the float. #include <stdio.h>    int   main (){   float   f = ( float ) 9 / 4 ;     printf ( "f : %f \n " ,  f  );     return   0 ;   }