-->
Showing posts with label Program to understand pointer passed to function..WAP to pass array to function.. Show all posts
Showing posts with label Program to understand pointer passed to function..WAP to pass array to function.. Show all posts

Program to understand pointer passed to function.

using codeblocks
----------------------------------------------------------------------------------------------
//program to pick the greatest number using pointer passed to function
#include <stdio.h>            //header file
void max_number(int a[],int *);
int main()
{
    int a[100];                 //array declaration
    int i;
    int total;                   //variables declaration
    printf("enter value of total\n");
    scanf("%d",&total);              //input of total value of array
    printf("enter elements of array\n");
    for(i=0;i<total;i++)                   //execution of loop
    {
        printf("enter value for location=%d\t",i);
        scanf("%d",a+i);                  //input of element using array. here array acts as pointer.
                                          //It gets input using address starting from location 0.
    }
    max_number(a,&total);                //calling the function with passing of array and pointer
    return 0;
}
void max_number(int a[],int*p)
{
    int i,max;
    max=*(a+0);                          //It stores value stored in location 0

    for(i=0;i<*p;i++)                    //value of pointer being used
    {
       if(max<*(a+i))                   //comparison takes place
       {
         max=*(a+i);                    //assigning value
       }
    }
    printf("max value=%d",max);       //display of maximum value

}

----------------------------------------------------------------------------------------------------------
using turbo c++
------------------------------------------------------------------------------------------------------------------
//program to pick the greatest number using pointer passed to function
#include <stdio.h>            //header file
#include<conio.h>
void max_number(int a[],int *);
int main()
{
    int a[100];                 //array declaration
    int i;
    int total;                   //variables declaration
    printf("enter value of total\n");
    scanf("%d",&total);              //input of total value of array
    printf("enter elements of array\n");
    for(i=0;i<total;i++)                   //execution of loop
    {
        printf("enter value for location=%d\t",i);
        scanf("%d",a+i);                  //input of element using array. here array acts as pointer.
                                          //It gets input using address starting from location 0.
    }
    max_number(a,&total);                //calling the function with passing of array and pointer
   getch();
    return 0;
}
void max_number(int a[],int*p)
{
    int i,max;
    max=*(a+0);                          //It stores value stored in location 0

    for(i=0;i<*p;i++)                    //value of pointer being used
    {
       if(max<*(a+i))                   //comparison takes place
       {
         max=*(a+i);                    //assigning value
       }
    }
    printf("max value=%d",max);       //display of maximum value

}