1

so i am getting some kind of error , that after my array input the code doesnt run . i wanted to see if my method is wrong for inputting an array address in as a function parameter and then using it to solve

//find max element , of an array and return its address. using a function

so this is the code :

    #include<stdio.h>
    int *ReturnMax(unsigned int *NoArray[], unsigned int NoOfTerm)
    {
        int i;
        for(i=0;i<NoOfTerm;i++)
        {
            if(*NoArray[i]>*NoArray[i+1])
            {
                return NoArray[i];
            }
            else
            {
                return NoArray[i+1];
            }
        }

    }
    int main()
    {
        int NumOfTerm,i;
        int *ReturnAddress;
        printf("Enter number of Terms:\n");
        scanf("%d",&NumOfTerm);

        int NumArray[NumOfTerm];
        printf("Enter the Array : \n");
        for(i=0;i<NumOfTerm;i++)
        {
            scanf("%d",&NumArray[i]);
        }
        *ReturnAddress=ReturnMax(&NumArray[NumOfTerm],NumOfTerm);
        printf("The Max number is %d and its Address is %d",*ReturnAddress,ReturnAddress);
        return 0;

}

2 Answers 2

2

For starters the function

int *ReturnMax(unsigned int *NoArray[], unsigned int NoOfTerm)
{
    int i;
    for(i=0;i<NoOfTerm;i++)
    {
        if(*NoArray[i]>*NoArray[i+1])
        {
            return NoArray[i];
        }
        else
        {
            return NoArray[i+1];
        }
    }

}

does not make sense.

At least the return type should be unsigned int *.

The first parameter of the function has the type unsigned int *NoArray[] . It is adjusted by the compiler to the type unsigned int **NoArray.

And you need to declare the array like

unsigned int NumArray[NumOfTerm];

You are calling the function like

*ReturnAddress=ReturnMax(&NumArray[NumOfTerm],NumOfTerm);

that is you are passing a pointer to the memory beyond the array that has the type unsigned int * because the valid range of indices is [0, NumOfTerm).

Moreover the pointer ReturnAddress is not initialized.

And in this call of printf

    printf("The Max number is %d and its Address is 

there are used invalid format specifiers.

The function can be defined the following way

unsigned int * ReturnMax( const unsigned int NoArray[], size_t NoOfTerm )
{
    const unsigned int *max = NoArray;

    for ( size_t i = 1; i < NoOfTerm; i++ )
    {
        if ( *max < NoArray[i] ) max = NoArray + i;
    }

    return ( unsigned int * )max;
}

And called like

    ReturnAddress = ReturnMax( NumArray, NumOfTerm );
    printf("The Max number is %u and its Address is %p", *ReturnAddress, ( void * )ReturnAddress);

Here is a demonstrative program

#include <stdio.h>

unsigned int * ReturnMax( const unsigned int NoArray[], size_t NoOfTerm )
{
    const unsigned int *max = NoArray;

    for ( size_t i = 1; i < NoOfTerm; i++ )
    {
        if ( *max < NoArray[i] ) max = NoArray + i;
    }

    return ( unsigned int * )max;
}

int main(void) 
{
    size_t NumOfTerm;
    unsigned int *ReturnAddress;

    printf( "Enter number of Terms: " );
    scanf( "%zu", &NumOfTerm );

    unsigned int NumArray[NumOfTerm];

    printf( "Enter the Array:\n" );

    for ( size_t i = 0; i < NumOfTerm; i++ )
    {
        scanf( "%u", &NumArray[i] );
    }

    ReturnAddress = ReturnMax( NumArray, NumOfTerm );

    printf( "The Max number is %u and its Address is %p",
            *ReturnAddress, ( void * )ReturnAddress );

    return 0;
}

If to input

10
0 1 2 3 4 5 6 7 8 9

Then the program output might look like

Enter number of Terms: 10
Enter the Array:
0 1 2 3 4 5 6 7 8 9
The Max number is 9 and its Address is 0x7ffdb3333194
Sign up to request clarification or add additional context in comments.

3 Comments

it worked , but i am still unable to understand how the above corrections corrected the segmentation fault.
@PranayKatyal This expression &NumArray[NumOfTerm] is the address of a non-existend element of the array with the index NumOfTerm. But you need to pass the address of the first element of the array. Either use the expression NumArray that is implicitly converted to pointer to the first element or the expression &NumArray[0].
ohhh thats why it showed segmentation error i guess
1

You are calling ReturnMax with:

*ReturnAddress = ReturnMax(&NumArray[NumOfTerm],NumOfTerm);

This passes to the function the address to NumArray[NumOfTerm] which is outside the bounds of your array, this is probably why your program throws an error. Also you can just pass *ReturnAddress = ReturnMax(NumArray,NumOfTerm); if you change your function to:

int *ReturnMax(unsigned int *NoArray, unsigned int NoOfTerm)

Another thing is that you don't need to dereference in if(*NoArray[i]>*NoArray[i+1]) if you want to compare the values contained. It should be

if(NoArray[i]>NoArray[i+1])

instead Also if you want to print a pointer you should use %p and not %d inside your

printf("The Max number is %d and its Address is %d",*ReturnAddress,ReturnAddress);

1 Comment

i will do the said changes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.