Home > Enterprise >  multi pointers in function in c
multi pointers in function in c

Time:06-25

i'm not good at english. i declare array and two pointers.

the maxPtr pointer should have array arr's maximum number adress. and minPtr pointer should have array arr's minimum number adress.

so i declare the function and this has two double-pointer to give maxPtr and minPtr proper adress.

but whenever i run this code, the program is not fully run. it doesn't output the result( printf("%d",*maxPtr) ,printf("%d", *minPtr, printf("Hi"); this program is run at vscode in mac. what make it error?

#include <stdio.h>
void MaxAndMin(int* str,int** max, int** min)
{
    int i;
    int maxnum=0,minnum=0;

    for(i=0; i<5; i  )
    {
        if(maxnum< str[i])
        {
            maxnum =str[i];
            *max = &str[i];
        }
        if(minnum > str[i])
        {
            minnum = str[i];
            *min = &str[i];
        }
    }
}

int main(void)
{
    int i,len;

    int* maxPtr;
    int* minPtr;
    int arr[5]={};

    for(i=0; i<5; i  )
    {
        printf("%d번째 정수입력 입니다.",i 1);
        scanf("%d", &arr[i]);
    }
    MaxAndMin(arr,&maxPtr,&minPtr);

    printf("%d",*maxPtr);
    printf("%d",*minPtr);
    printf("Hi");
    return 0;
}

the result is

> Executing task: ./test <

1번째 정수입력 입니다.1
2번째 정수입력 입니다.2
3번째 정수입력 입니다.3
4번째 정수입력 입니다.4
5번째 정수입력 입니다.5

Terminal will be reused by tasks, press any key to close it.

CodePudding user response:

For starters this initialization of an array

int arr[5]={};

is incorrect in C. You have to write

int arr[5]={ 0 };

Secondly using the magic number 5 within the function makes the function useless in general. You need to pass to the function the size of the array.

The initial value 0

int maxnum=0,minnum=0;

of these variables makes the function even more less useful. In general the array can contain either all elements positive or all elements negative.

And you need to flush the output buffer using for example the new line character '\n' in calls of printf.

The function can be declared and defined the following way as it is shown in the demonstration program below.

#include <stdio.h>

void MaxAndMin( const int a[], size_t n, int **max, int **min )
{
    *max = ( int * )a;
    *min = ( int * )a;


    for ( size_t i = 1; i < n; i   )
    {
        if ( **max < a[i] )
        {
            *max = ( int *)( a   i );
        }
        else if ( a[i] < **min )
        {
            *min = ( int * )( a   i );
        }
    }
}

int main( void )
{
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    const size_t N = sizeof( a ) / sizeof( *a );

    int *maxPtr = NULL;
    int *minPtr = NULL;

    MaxAndMin( a, N, &maxPtr, &minPtr );

    printf( "The maximum value is %d at position %tu\n",
            *maxPtr, maxPtr - a );   

    printf( "The minimum value is %d at position %tu\n",
            *minPtr, minPtr - a );   
}

The program output is

The maximum value is 9 at position 9
The minimum value is 0 at position 0

Pay attention to that the first parameter of the function should have the qualifier const because passed arrays to the function are not changed within the function.

CodePudding user response:

The main issue is that the minnum is set at zero, which would only work if array had a negative value.

Setting minimum = star[0] also would not work!!! Because in the case of str[0] having negative value, *min never gets changed.

Also, I recommend to always initialize all variables in the declaration, especially pointers (because they may theoretically cause accidental access to memory).

Full solution:

#include <stdio.h>
int MaxAndMin(int* str, int** max, int** min)
{
    int i;
    int maxnum = 0;
    int minnum = str[0]   1;

    for(i=0; i<5; i  )
    {
        if(maxnum < str[i])
        {
            maxnum = str[i];
            *max = &str[i];
        }
        if(minnum > str[i])
        {
            minnum = str[i];
            *min = &str[i];
        }
    }

    return 0;
}

int main(void)
{
    int i = 0;
    int len = 0;

    int* maxPtr = NULL;
    int* minPtr = NULL;
    int arr[5]={};

    for(i=0; i<5; i  )
    {
        printf("Enter number %d: ",i 1);
        scanf("%d", &arr[i]);
    }
    MaxAndMin(arr, &maxPtr, &minPtr);

    printf("%d",*maxPtr);
    printf("%d",*minPtr);
    printf("Hi");

    return 0;
}
  • Related