C++: Program crashes when trying to print value of pointer

0

I'm writing a program that is supposed to allow a user to enter the size of an array, enter the values for each index of the array, and print out the Min and Max Values of the array using a pointer. The program successfully determines the Max and Min Values of the array and prints out the max value with the use of the pointer but crashes when doing the exact same for the min value pointer.

Here's the code:

int main()
{
    //Variable Declaration
    int arsize  = 0;
    int i       = 0;
    int range   = 0;
    int armin, armax;
    int *ptrmin, *ptrmax;

    //User Prompt for array size, saved to variable arsize
    printf("How many elements should be stored in the array?: ");
    scanf("%d", &arsize);
    fflush(stdin);

    //The array ar is declared according to the user input
    int ar[arsize];

    for (i = 0; i < arsize; i++)
    {
        printf("Enter value for element at index %d:\t", i);
        scanf("%d", &ar[i]);
        fflush(stdin);
    }

    //For loop with if statement to determine biggest value in array 'ar'
    armax = ar[0];
    for (i = 0; i < arsize; i++)
    {
        if (armax < ar[i])
        {
            armax = ar[i];
            ptrmax = &ar[i];
        }
    }

    //For loop with if statement to determine the smallest value in array 'ar'
    armin = ar[0];
    for (i = 0; i < arsize; i++)
    {
        if (armin > ar[i])
        {
            armin = ar[i];
            ptrmin = &ar[i];
        }
    }


    //The Min and Max is printed using pointers, Range is printed regularly
    printf("\nMax:\t%d\n", *ptrmax);
    printf("Min:\t%d\n", *ptrmin);

The output is as follows:

How many elements should be stored in the array?: 2
Enter value for element at index 0:     50
Enter value for element at index 1:     100

Max:    100

Process returned -1073741819 (0xC0000005)   execution time : 4.438 s

The Program successfully prints the max value, but not the min?

c++
algorithm
loops
for-loop
pointers
asked on Stack Overflow Oct 19, 2020 by John Redman • edited Oct 19, 2020 by Vlad from Moscow

1 Answer

0

For starters variable length arrays like this

int ar[arsize];

is not a standard C++ feature. Instead use the standard container std::vector<int>.

This call

fflush(stdin);

has undefined behavior. Remove it.

In the both for loops like this

armax = ar[0];
for (i = 0; i < arsize; i++)
{
    if (armax < ar[i])
    {
        armax = ar[i];
        ptrmax = &ar[i];
    }
}

the pointers ptrmax and ptrmin were not initialized. So dereferencing the pointers in general can result in undefined behavior.

And it is the case for this input

Enter value for element at index 0:     50
Enter value for element at index 1:     100

because the minimum element is the initial element of the array the pointer to which was not set.

You could rewrite the loops the following way

ptrmax = ar;
for (i = 1; i < arsize; i++)
{
    if ( *ptrmax < ar[i])
    {
        ptrmax = ar + i;
    }
}

The variables armax and armin are redundant.

Also bear in mind that there is the standard algorithms std::min_element, std::max_element and std::minmax_element that can be used instead of the loops.

answered on Stack Overflow Oct 19, 2020 by Vlad from Moscow • edited Oct 19, 2020 by Vlad from Moscow

User contributions licensed under CC BY-SA 3.0