Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

c - Finding largest element in a array using call by reference ( Functions & Pointers )

I want to find the largest element in an array using call by reference method that is functions and pointers. I am getting an error at function call. Here is my try:

#include <stdio.h>
void bigg(int *a[10],int *N);

int main()
{
    int a[10],i,N,p;
    printf("Enter the total number of elements in the array:
");
    scanf("%d",&N);
    printf("Enter the elements in the array one by one:
");
    for(i=0;i<N;i++)
    {
        scanf("%d",&a[i]);
    }
    bigg(&a,&N);    
}

void bigg(int *a[10],int *N)
{
    int i,max;
    
    max = *a[0];
    
    for(i=0;i<*N;i++)
    {
        if( *a[i] > max)
        {
            max = *a[i];
        }
    }
    printf("The biggest element in the given array is: %d",max);    
}

question from:https://stackoverflow.com/questions/65661217/finding-largest-element-in-a-array-using-call-by-reference-functions-pointer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your basics of pointer is not clear. For this question it works.

#include <stdio.h>
void bigg(int a[],int *N);

int main()
{
    int a[10],i,N,p;
    printf("Enter the total number of elements in the array:
");
    scanf("%d",&N);
    printf("Enter the elements in the array one by one:
");
    for(i=0;i<N;i++)
    {
        scanf("%d",&a[i]);
    }
    bigg(a,&N);    
}

void bigg(int a[],int *N)
{
    int i,max;
    
    max = a[0];
    
    for(i=0;i<*N;i++)
    {
        if( a[i] > max)
        {
            max = a[i];
        }
    }
    printf("The biggest element in the given array is: %d",max);    
}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...