Home > database >  How to solve this problem( throw an exception )
How to solve this problem( throw an exception )

Time:01-08

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>

void input_count(int*);
int* input_values(int);
void show_result(int**, int);

int main()
{
    int count = 0;
    input_count(&count);
    int* array = input_values(count);
    show_result(&array, count);
    return 0;
}

void input_count(int* count)
{
    do
    {
        printf("배열의 개수는? (0보다 커야합니다) ");
        scanf("%d", count);
    } while (*count <= 0);
}
int* input_values(int count)
{
    int* array = (int*)malloc(sizeof(int) * count);
    for (int i = 0; i < count;   i)
    {
        printf("%d번째 값은? ", i);
        scanf("%d", array   i);
    }
    return array;
}
void show_result(int** array, int count)
{
    int max = 0, min = INT_MAX;

    int* max_address = NULL, *min_address = NULL;

    for (int i = 0; i < count;   i)
    {
        if (*array[i] > max)
        {
            max_address = *array   i;
            max = *max_address;
        }
        if (*array[i] < min)
        {
            min_address = *array   i;
            min = *min_address;
        }
    }
    printf("최대 원소의 주소: %p, 값: %d\n", max_address, max);
    printf("최소 원소의 주소: %p, 값: %d\n", min_address, min);
}

Hello! I'm korean. i dont speak English well. I've only been studying programming for 10 days, so my skills are lacking. but i want to solve this problem.

The show_result function throws an exception[enter image description here] Exception thrown(0x00007FF6BE02596B, Main.exe): 0xC0000005: 0xFFFFFFFFFFFFFFFF 위치를 읽는 동안 액세스 위반이 발생했습니다.

enter image description here enter image description here

i think Null is the problem but i dont know the mean that is back reference.

CodePudding user response:

There is no sense to pass the pointer to teh dynamically allocated array by reference through a pointer to it

show_result(&array, count);

because the pointer is not changed within the function show_result.

So declare the function like

void show_result( const int *, size_t );

and call it like

show_result( array, count);

The if statements

    if (*array[i] > max)

and

    if (*array[i] < min)

use invalid expressions. You have to write at least like

    if ( ( *array )[i] > max)

and

    if ( ( *array )[i] < min)

You will not have such a problem if will declare the function as shown above.

Also setting the variable max to 0

int max = 0, min = INT_MAX;

does not make sense. As the element type of the array is int then it can contain all elements set by negative numbers. In this case you will get a wrong result.

The function can be defined for example the following way

void show_result( const int *array, size_t count) 
{
    const int *max_address = array; 
    const int *min_address = array;

    for ( size_t i = 1; i < count;   i )
    {
        if ( *max_address < array[i] )
        {
            max_address = array   i;
        }
        else if ( array[i] < *min_address )
        {
            min_address = array   i;
        }
    }

    if ( count != 0 )
    {
        printf( "최대 원소의 주소: %p, 값: %d\n", ( const void * )max_address, *max_address );
        printf( "최소 원소의 주소: %p, 값: %d\n", ( const void * )min_address, *min_address );
    }
    else
    {
        // output a message that an empty array is passed 
    }
}
  • Related