【问题标题】:Exception Thrown when attempting to compare using pointers尝试使用指针进行比较时抛出异常
【发布时间】:2018-04-29 05:30:28
【问题描述】:

我正在尝试创建一个循环,使指针指向数组中的不同元素(按降序排序),但我似乎无法正确进行比较,因为我总是抛出异常。我还有一个循环来打印所有指针的元素,以测试循环是否正常工作。以前从未真正使用过指针,但我尝试使用与学习指针时在其他网站上看到的相同的方式来格式化它们。这是我正在谈论的代码部分:

//What I have included
#define _CRT_SECURE_NO_WARNINGS
#define MAX 5
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

//Variables related to the loops
int nums[MAX], *ptrd[MAX];

//Loops in question (Assume nums[MAX] = {1, 2, 3, 4, 5})
for (int i = 0; i < MAX; i++)
{
    for (int j = 0; j < MAX; j++)
    {
        if (*ptrd[i] < nums[j] && nums[j] <= *ptrd[i - 1])
        {
            if (i > 0)
            {
                if (ptrd[i] == ptrd[i - 1])
                    continue;
            }
            ptrd[i] = &nums[i];
        }
    }
}
for (int i = 0; i < MAX; i++)
{
    printf("\n%d", *ptrd[i]);
}

目前的完整代码(注意这里的代码不同):

#define _CRT_SECURE_NO_WARNINGS
#define MAX 5
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>

//Variables
char input[20];
int nums[MAX], *ptrd[MAX], *ptra[MAX];
bool isValid;

//Methods
bool checkValidity();
void convertChars();
void resetInput();

int main()
{
    printf("Please enter 5 numbers (Separate each by spaces): ");
input:
    resetInput();
    scanf(" %[^\n]%*c", &input);
    isValid = checkValidity();

    if (isValid == false)
    {
        printf("Invalid input. Retry: ");
        goto input;
    }
    convertChars();

    for (int i = 0; i < MAX; i++)
    {
        for (int j = 0; j < MAX; j++)
        {
            if (*ptrd[i] < nums[j])
            {
                if (i > 0)
                {
                    if (ptrd[i] == ptrd[i - 1])
                        continue;
                    if (nums[j] <= *ptrd[i - 1])
                        ptrd[i] = &nums[i];
                }
                else
                    ptrd[i] = &nums[i];
            }
        }
    }
    for (int i = 0; i < MAX; i++)
    {
        printf("\n%d", (*ptrd)[i]);
    }

    getchar();

    return 0;
}

bool checkValidity()
{
    bool multNum = false;
    int chars = 0;

    for (int i = 0; i < 20; i++)
    {
        if (!isdigit(input[i]))
        {
            if (input[i] == ' ' && multNum == true || input[i] == NULL && multNum == true)
                chars += 1;

            if (input[i] != ' ' && input[i] != NULL)
            {
                printf("\nIncorrect characters\n");
                return false;
            }
            if (input[i] == ' ' && multNum == false)
            {
                printf("\nToo many spaces at at once\n");
                return false;
            }
            if (input[i] == ' ' && multNum == true || input[i] == NULL && multNum == true)
                multNum = false;
        }
        else if (isdigit(input[i]))
        {
            multNum = true;
        }
    }
    if (chars != 5)
    {
        printf("\nIncorrect amount of nums (%d)\n", chars);
        return false;
    }
    else
        return true;
}

void convertChars()
{
    int placeHolder, nums_ = 0, done = 0;
    for (int i = 0; i < 20 && done < 5; i++)
    {
        if (isdigit(input[i]))
        {
            placeHolder = input[i] - '0';
            nums_ = (nums_ * 10) + placeHolder;
        }
        else
        {
            nums[done] = nums_;
            nums_ = 0;
            done += 1;
        }
    }
}

void resetInput()
{
    for (int i = 0; i < 20; i++)
    {
        input[i] = NULL;
    }
}

【问题讨论】:

  • 是分段错误吗?
  • 在那个部分,是的。
  • 也许可以,但这就是为什么我放了 (if i > 0) 部分,所以它不会尝试寻找 ptrd[-1]
  • 这是抛出的异常:(在 PointerSortingBB.exe 中的 0x00391B5C 抛出异常:0xC0000005:访问冲突读取位置 0x00000000。)在 (if (*ptrd[i]
  • ptrd 指向的地方,你没有在任何地方初始化。因此,当您执行*ptrd[i] 时,显然会给出段。故障。

标签: c loops pointers exception


【解决方案1】:

您没有初始化 *ptrd[MAX],这会导致未定义的行为。使用 gdb 运行您的代码,它会崩溃并显示以下输出:

(gdb) run
Starting program: /home/pringles/Desktop/a.out 
Please enter 5 numbers (Separate each by spaces): 3 2 1 5 6

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400719 in main () at test.c:37
37              if (*ptrd[i] < nums[j])

通过打印ptrd中的元素,可以看到所有元素都指向0x0:

(gdb) print ptrd
$1 = {0x0, 0x0, 0x0, 0x0, 0x0}

【讨论】:

  • 感谢它帮助我回到了完成我的代码并弄清楚我必须做什么的轨道上!现在我终于可以进步了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-13
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
相关资源
最近更新 更多