Functions and Pointer

  • How to pass pointer to Function?
  • Here pointer means passing address to the functions as arguments.
  • When we call a function by passing addresses then such call of function is called as call by reference
  • Let's make function which will take address as it argument.







  • Q:make a function reverse_number which will take integer pointer as its argument and that function should perform operation of reversing the number which is store by the integer pointer, And function should no return anything.test that function by calling it.

  • Solution:
    void reverse_number(int *n)  //formal parameters
    {  int i,j=0;
        while(*n != 0)
        {
            i = *n%10;
            j = j*10+i;
            *n = *n/10;
        }
        *n = j;
    }
    

    Let's use above function reverse_number():
    #include<stdio.h>
    void main()
    {
    int n = 325;
    reverse_number(&n); //passing address of variable 'n'.
    printf("reverse number is %d",n);
    
    }
    

    Output:
    reverse number is 523

    Note:Look clearly in the function definition that we have returned nothing and even then we have reverse the number.
    that was because of done changes at the memory location. see below image of what has happen behind the seen.



    Now we will define same function but these time function should take integer number as argument.let's define it.

    Solution:
    void reverse_number(int n)  //formal parameters
    {  int i,j=0;
        while(n != 0)
        {
            i = n%10;
            j = j*10+i;
            n = n/10;
        }
        *n = j;
    }
    

    let's use above function:
    #include<stdio.h>
    void main()
    {
    int n = 325;
    reverse_number(n); //passing address of variable 'n'.
    printf("reverse number is %d",n);
    
    }
    

    Output:
    reverse number is 325

    As you saw number remain unchange,because we did not made changes in the memory.

    Note:we could have reverse the number by returning the value of n while defining the function but we have not done that because we where just understanding the concept of pointers










  • Q:Create a function 'swap' which will take two pointers of integer variable ,that function should perform operation of swapping.

  • Defining swap function:
    void swap(int *a ,int *b)
    {
        int temp;
    
        temp = *a;
        *a  = *b;
         *b = temp;
    }
    

    Using above swap() function:
    #include<stdio.h>
    void main()
    {
    int i = 10,j= 20;
    
    swap(&i,&j);
    
    
    printf("after swapping  numbers  are i = %d and j = %d",i,j);
    
    }
    

    Output:
    after swapping numbers are i = 20 and j = 10

    Note: there is only one way of swapping number by using user defined function is that argument which are pass to function should be pointers.
    because we cannot return multiple value.


    see below image of what has happen behind the seen.














  • Q:make a functions 'max_element' which will take pointer of integer array as argument and it should return the address of maximum element in that array.

  • Defining function 'max_element':
    int * max_element(int *p,int n) //Actual Parameters
    { int i,max=0,*p1;
        for(i=0; i<n ; i++)
        { //at every iteration we are comparing the element with
            //variable max
            //if it is greater than max than we are assigning
            //that value to new max value and address of that
            //element  assigning to pointer variable p1.
            if(*(p+i) > max)
            {
                max = *(p+i);
                p1  = (p+i);
            }
        }
        return p1;
    }
    

    Using the above define function:
    #include<stdio.h>
    void main()
    {
    int *p,*result;
    int i;
    p = (int*)malloc(5*sizeof(int));
    
    for(i=0;i<5;i++)
    {
        printf("enter the element:");
        scanf("%d",p+i);
    }
    
    
    result = max_element(p,5); //Actual parameters
    
    
    printf("Maximum element in array is   %d",*result);
    
    }
    

    Enter the element:10
    Enter the element:20
    Enter the element:30
    Enter the element:40
    Enter the element:50


    Maximum element in array is 50.










  • This is how we can make programmer define function which can take pointer as argument or return pointer.
  • So , do more programs to understand the concept more better.





  • Further Topics:




    Further Topics:


    People Also Searched: