Example : A Pointer Examples in C to find out the size/memory occupied by different data types using sizeof() operator.
# include <stdio.h>
# include <conio.h>

void main( )
 { 	
	//clrscr();
	printf("The memory occupied by Integer is = %d bytes\n",sizeof(int));
	printf("The memory occupied by Long is = %d bytes\n",sizeof(long));
	printf("The memory occupied by Float is = %d bytes\n",sizeof(float));
	printf("The memory occupied by Double is = %d bytes\n",sizeof(double));
	printf("The memory occupied by Character is = %d bytes\n",sizeof(char));
		
	getch();
 }
Output :

The memory occupied by Integer is = 4 bytes 
(The memory occupied by Integer is = 2 bytes, in old version of the system)
The memory occupied by Long is = 4 bytes
The memory occupied by Float is = 4 bytes
The memory occupied by Double is = 8 bytes
The memory occupied by Character is = 1 bytes
Example : Pointer Examples in C to print value using Pointer.
#include<stdio.h>
#include<conio.h>
int main()
{
        int x,*ptr;
   	x = 10;   
   	ptr = &x;   
   	printf(" %d ", x);
   	printf(" %d ",*ptr);
	printf(" %d ",*(&x));
   
   return 0;
}
Example : Pointer Examples in C to print value and address of variables using Pointer through different ways.
# include <stdio.h>
# include <conio.h>
void main( )
{
	int num = 15;
	int *ptr;
	ptr = &num;
	
	printf ("Address of variable/stored value is = %u \n", &num);
	printf ("Address of variable/stored value is = %d \n", &num);	
	printf ("Address of variable/stored value is = %u \n", ptr);	
	printf ("Address of variable/stored value is = %d \n", ptr);	
	
	printf ("\nAddress of Pointer is= %u \n", &ptr);	
	printf ("Address of Pointer is= %d \n", &ptr);
	
	printf ("\nValue stored in variable is = %d \n", num);
	printf ("Value stored in variable is = %d \n", *(&num));
	printf ("Value stored in variable is = %d", *ptr);
	
	getch();
}

Output :

Address of variable/stored value is = 6487580
Address of variable/stored value is = 6487580
Address of variable/stored value is = 6487580
Address of variable/stored value is = 6487580

Address of Pointer is= 6487568
Address of Pointer is= 6487568

Value stored in variable is = 15
Value stored in variable is = 15
Value stored in variable is = 15
Example : Pointer Examples in C to swap values and display them using Pointer.
#include<stdio.h>
#include<conio.h>

void swap(int *a,int *b)
{
    int temp;
     temp = *a;
    *a = *b;
    *b = temp;
} 
int main()
{
    int num1,num2;
     
    printf("Enter first value : ");
    scanf("%d",&num1);
    
    printf("Enter second value : ");
    scanf("%d",&num2);   
    printf("Before Swapping: num1=%d, num2=%d\n",num1,num2);     
    
    swap(&num1,&num2);   
    printf("After  Swapping: num1=%d, num2=%d\n",num1,num2);    
     
    return 0;
}
Example : Pointer Examples in C to print a String value using Pointer.
#include <stdio.h>
#include <conio.h>
int main()
{
    char str[50];
    char *ptr;
     
    printf("Enter a string value upto 50 characters: ");
    gets(str);    
    ptr=str;     
    printf("Entered string is: ");
       while(*ptr!='\0')
       {
       	printf("%c",*ptr);
	*ptr++;     // ptr++; 	
       }         
    return 0;
}
Example : Pointer Examples in C to prints values & addresses through Pointer and an Array acts as a Pointer.
# include <stdio.h>
# include <conio.h>

int main()
{
	int arr[ 5 ] = {100, 200, 300, 400, 500};
	int i;
	
	for (i = 0; i <=4; i=i+1)
	{
		printf ("Array Cell i = %d  Value of arr[i] = %d  Value of *(arr+i) = %d  ", 
                       i, arr[i], *(arr+i));
		
		printf (" Address of &arr[i] = %u  Address of arr+i = %u", &arr[i], (arr+i)); 
		
		printf("\n");
	}
}

Output :

Array Cell i = 0  Value of arr[i] = 100  Value of *(arr+i) = 100   Address of &arr[i] = 2358832  
Address of arr+i = 2358832
Array Cell i = 1  Value of arr[i] = 200  Value of *(arr+i) = 200   Address of &arr[i] = 2358836  
Address of arr+i = 2358836
Array Cell i = 2  Value of arr[i] = 300  Value of *(arr+i) = 300   Address of &arr[i] = 2358840  
Address of arr+i = 2358840
Array Cell i = 3  Value of arr[i] = 400  Value of *(arr+i) = 400   Address of &arr[i] = 2358844  
Address of arr+i = 2358844
Array Cell i = 4  Value of arr[i] = 500  Value of *(arr+i) = 500   Address of &arr[i] = 2358848  
Address of arr+i = 2358848

---------------------   OR   -----------------------

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

int main()
{
	int arr[ 3 ][ 3 ] = {{10, 20, 30}, {40, 50, 60}, {70, 80, 90}};
	int i,j;
	
	for (i = 0; i <3; i=i+1)
	{
	   for (j = 0; j <3; j=j+1)
	   {
	     printf ("Array Cell [i][j] = %d %d  Value of arr[i][j] = %d  Value of *(*(arr+i)+j)=
                  %d ", i,j, arr[i][j], *(*(arr+i)+j));
		
	     printf (" Address of &arr[i][j] = %u  Address of ((arr+i)+j) = %u", &arr[i][j],((arr+i)+j));
             printf("\n");		
	   }
	     printf("\n");
	}
	return 0;
}

Output :

Array Cell [i][j] = 0 0  Value of arr[i][j] = 10  Value of *(*(arr+i)+j)= 10   
Address of &arr[i][j] = 2358816  Address of ((arr+i)+j) = 2358816
Array Cell [i][j] = 0 1  Value of arr[i][j] = 20  Value of *(*(arr+i)+j)= 20   
Address of &arr[i][j] = 2358820  Address of ((arr+i)+j) = 2358828
Array Cell [i][j] = 0 2  Value of arr[i][j] = 30  Value of *(*(arr+i)+j)= 30   
Address of &arr[i][j] = 2358824  Address of ((arr+i)+j) = 2358840

Array Cell [i][j] = 1 0  Value of arr[i][j] = 40  Value of *(*(arr+i)+j)= 40   
Address of &arr[i][j] = 2358828  Address of ((arr+i)+j) = 2358828
Array Cell [i][j] = 1 1  Value of arr[i][j] = 50  Value of *(*(arr+i)+j)= 50   
Address of &arr[i][j] = 2358832  Address of ((arr+i)+j) = 2358840
Array Cell [i][j] = 1 2  Value of arr[i][j] = 60  Value of *(*(arr+i)+j)= 60   
Address of &arr[i][j] = 2358836  Address of ((arr+i)+j) = 2358852

Array Cell [i][j] = 2 0  Value of arr[i][j] = 70  Value of *(*(arr+i)+j)= 70   
Address of &arr[i][j] = 2358840  Address of ((arr+i)+j) = 2358840
Array Cell [i][j] = 2 1  Value of arr[i][j] = 80  Value of *(*(arr+i)+j)= 80   
Address of &arr[i][j] = 2358844  Address of ((arr+i)+j) = 2358852
Array Cell [i][j] = 2 2  Value of arr[i][j] = 90  Value of *(*(arr+i)+j)= 90   
Address of &arr[i][j] = 2358848  Address of ((arr+i)+j) = 2358864
Example : Pointer Examples in C to print values & addresses through various ways along with pointer to pointer.
# include <stdio.h>
# include <conio.h>
void main( )
{
		int num = 100;
		
		int *ptr1;
		int **ptr2;  //Pointer to Pointer
		
		ptr1 = &num;
		ptr2 = &ptr1;
		
		printf ("Address of num = %u \n", &num);		
		printf ("Address of num = %u \n", ptr1);		
		printf ("Address of num = %u \n", *ptr2);
		
		printf ("\nAddress of ptr1 = %d \n",  ptr2);
		printf ("Address of ptr1 = %u \n", &ptr1);		
		printf ("Address of ptr1 = %u \n", ptr2);
		
		printf ("\nAddress of ptr2 = %u \n", &ptr2);
		
		printf ("\nValue of num = %d \n", num);
		printf ("Value of num = %d \n", *(&num));
		printf ("Value of num = %d \n", *ptr1);
		printf ("Value of num = %d", **ptr2);

	getch();
}

Output : 
Address of num = 6487628
Address of num = 6487628
Address of num = 6487628

Address of ptr1 = 6487616
Address of ptr1 = 6487616
Address of ptr1 = 6487616

Address of ptr2 = 6487608

Value of num = 100
Value of num = 100
Value of num = 100
Value of num = 100

Loading

Categories: C

0 Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.