简体   繁体   English

C++:尝试打印指针值时程序崩溃

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

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?程序成功打印了最大值,但没有打印最小值?

For starters variable length arrays like this对于像这样的初学者可变长度数组

int ar[arsize];

is not a standard C++ feature.不是标准的 C++ 功能。 Instead use the standard container std::vector<int> .而是使用标准容器std::vector<int>

This call这个电话

fflush(stdin);

has undefined behavior.有未定义的行为。 Remove it.去掉它。

In the both for loops like this在像这样的两个 for 循环中

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.指针ptrmaxptrmin未初始化。 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.变量armaxarmin是多余的。

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.还要记住,可以使用标准算法std::min_elementstd::max_elementstd::minmax_element代替循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM