Monday, October 3, 2022

C pointers

                                         C pointers

 C pointers are a powerful tool that gives strength to the C language more than others.

What is the output?

#include<stdio.h>

void crazy(int,int);

void crazy(int m, int n)

 {

int *ptr;

m=0;

ptr=&m;

n=*ptr;

*ptr=1;

printf("%d %d",m,n);

}

void main()

{

            crazy(2,2);

}

O/p:




What is the output?

#include<stdio.h>

 int sum(int num1,int num2)

{

            return num1+num2;

}

int main()

{

            int (*f2p)(int,int);

            f2p=sum;

            //usng function pointer

            int op1=f2p(10,13);

            int op2=sum(10,13);

            printf("Output by function pointer : %d\n",op1);

            printf("Output by function name %d",op2);

            return 0;

}

O/p:


What is the output?

#include<stdio.h>

#include <stdio.h>

int main()

{

            int a[] = {2, 4, 6, 8, 10};

            int i,sum = 0,*b=a+4;

            for (i = 0;i<5;i++){

            sum = sum +(*b–i)–*(b–i);

            printf("%d %d",*b,*(b-1));

            //printf ("%d\n",sum);

                                                                        }

return 0;

}

 2D array element access using pointer

int main()

{

    int i,j,TWODarray[ROW][COL];

    PassTWODarraytoFn(TWODarray);

     /* In the below section the display of the 2Darray has been obtained*/

    for (i = 0; i < ROW; i++) {

        for (j = 0; j < COL; j++) {

            printf("%3d", TWODarray[i][j]);

        }

        printf("\n");

    }

     return 0;

}

O/p:




    Printing a 2D array elements using pointers

#include<stdio.h>

main()

{

int s[5][2] = {

{ 1234, 56 },

{ 1212, 33 },

{ 1434, 80 },

{ 1312, 78 }

                   } ;

 

int ( *p )[2] ;

int i, j, *pint ;

for ( i = 0 ; i <= 3 ; i++ )

{

p = &s[i] ;

pint = p ;

printf ( "\n" ) ;

for ( j = 0 ; j <= 1 ; j++ )

printf ( "%d ", *( pint + j ) ) ;

}

}


Taking input from users to a 2D array and passing it to a function using a pointer

#include <stdio.h>
main()
{
       int i,j,m=3,n=3;
       int TwoDarray[3][3];
       printf("\n2D array input for  3x3 matrix :\n");
       printf("Provide input :\n");
       for(i=0;i<3;i++)
{
      for(j=0;j<3;j++)
      {
      printf("Value - [%d],[%d] : ",i,j);
      scanf("%d",&TwoDarray[i][j]);
      }
  }
       passarrtofn(m,n,(int *)TwoDarray);
}

void passarrtofn(int m,int n,int *TwoDarray)
{
  int i,j;
  int (*ptr)[3];
  ptr=TwoDarray;
  for(i=0;i<m;i++)
  {
      for(j=0;j<n;j++)
      {
      printf("\n Value - [%d],[%d] : ",i,j);
      printf("%d",*(*(ptr + i) + j));
      }
  }  
 printf("\n\n");
}




 Dynamic memory allocation using pointers

#include<stdio.h>  
#include<stdlib.h>  
int main(){  
int n,i,*ptr,sum=0; 
   
printf("Enter number of elements: ");    
scanf("%d",&n);    
ptr=(int*)malloc(n*sizeof(int));  //memory allocated using malloc    
if(ptr==NULL)                         
    {    
        printf("Sorry! unable to allocate memory");    
        exit(0);    
    }    
printf("Enter elements of array: ");    
for(i=0;i<n;++i)    
    {    
        scanf("%d",ptr+i);    
        sum+=*(ptr+i);    
    }    
printf("Sum=%d",sum);    
free(ptr);     
return 0;  
}    




Passing a 1D character(string) ptr array to a function using function

#include<stdio.h> 
#define SIZE 12 
void arrayfn(char*); 
void main(){ 
 
    int i; 
    char* array[SIZE]={'P','r','o','g','r','a','m','m','i','n','g'}; 
 
    for(i=0;i<SIZE;i++){ 
        arrayfn(&array[i]); 
    } 
 
 
void arrayfn(char* c){ 
 
    printf("%c ",*c); 



                                        

How to declare a pointer?

#include<stdio.h>
main( )
{
int i=3 ;
int *j ;
j = &i ;
printf("%d\n",i);
printf("%d\n",&i);
printf("%d\n",&j);
printf("%d\n",j);
printf("%d\n",*j);
printf("%d\n",*(&i));
/*printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ;
printf ( "\nAddress of j = %u", &j ) ;
printf ( "\nValue of j = %u", j ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *( &i ) ) ;
printf ( "\nValue of i = %d", *j ) ; */


}

                          Call by reference

//Array and pointers but let us pass it to a function
main()
{
int array[]={1,2,3,4,5};
passarrtofn(5,&array[0]);// calling function
}
 passarrtofn(int p,int *q)
 {
  int i;
//arrayptr=&q;// address of array[0] is stored in array pointer
for(i=0;i<=p-1;i++){
printf("%d",*q);
q++;
}
 }

  Character array and pointer

main()
{
char country[]="India";
char *ptrtoarray=country;
while(*ptrtoarray!='\0')
{
printf("%c",*ptrtoarray);
ptrtoarray++;
}
}

Pointer to an array and pass that array to a function

main()
{
int array[]={1,2,3,4,5};
passarrtofn(5,&array[0]);
}
passarrtofn(int p,int *q)
{
int i;
for(i=0;i<p-1;i++)
{
printf("%d",*q);
q++;
}
}












No comments:

Post a Comment

GRAPH ALGORITHMS : FLOYD-WARSHAL -Displaying DISTANCE MATRIX and PI MATRIX using C PROGRAM

Modify the following Floyd-Warshall algorithm by introducing a π( pi ) table as per the class’s discussion. Here you can write one function...