异常尝试使用指针进行比较时抛出

时间:2018-04-29 05:30:29

标签: c loops pointers exception

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

//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;
    }
}

1 个答案:

答案 0 :(得分:0)

您没有初始化*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}