c - "Debug assertion failed! expression: _p != nullptr"错误

标签 c command-line

error image我试图制作一个使用多个参数进行计算的程序。 除了我所附图片中的最后一张之外,一切都按我的预期进行。 每次我尝试在命令行中输入超过 2 个数字时,都会发生错误。

输入示例如下:program1.exe 12 + 13 - 2 + 1

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


int main(int argc, char *argv[])
{
    int a, b, rst;
    int i;
    char opr;

    if (argc > 100)
    {
        printf("Too many arguments!\n");
        return 0;
    }
    else if (argc <= 1)
    {
        printf("There are no arguments!\n");
        return 0;
    }

    a = atoi(argv[1]);
    rst = a;
    if (argc == 2)
    {
        a = atoi(argv[1]);
    }
    else if (argc % 2 != 0)
    {
        printf("Invalid fomula!\n");
        exit(0);
    }
    else
    {
        for (i = 1; 2*i <= argc; i++)
        {
            b = atoi(argv[2 * i]);
            opr = argv[2 * i - 1];
            switch (opr)
            {
            case '+':rst = a + b;
                break;
            case '-':rst = a - b;
                break;
            }

            rst = a;
        }
    }
    printf("%d", rst);

    return 0;
}

最佳答案

按原样运行代码,输入设置为:<program.exe> 2 + 5 这里发生空指针错误:

for (i = 1; 2*i <= argc; i++)
        {
            b = atoi(argv[2 * i]);//Null pointer for i==2
            opr = argv[2 * i - 1];
            switch (opr)

因为没有argv[4] 。这导致 undefined behavior

如果不知道程序期望的输入内容,就不可能准确地进行更正。因为您没有指定,所以我最好的猜测是程序尝试从命令行读取一组参数,例如:

argv[0]          argv[1]   argv[2]   argv[3]
<program>.exe    1         +         2

然后将读取的值转换为变量,并使用两个数字和一个数学运算符执行数学运算,例如 +- .

假设这是基本目标,以下示例将简化您的现有代码来执行这些步骤。注意对参数输入计数测试的更正,以及循环索引的更正:

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

/// test using:
// prog.exe 2 + 3 3 - 4 6 + 7

//results are:
//result 0: 5
//result 1: -1
//result 2: 13

typedef struct {
    int n1;
    char opr;
    int n2;
    int result;
} CALC;


int main(int argc, char *argv[])
{
    int i;
    // test for correct number of input arguments
    if((argc-1)%3 !=0)
    {
        printf("Wrong number of inputs.\n");
        return 0;
    }

    // read in, and assign variables (use array of struct)
    int max = (argc-1)/3;  //guaranteed viable only after determining multiples are correct from above
    //Get ALL input into array variables
    CALC calc[max];
    for (i=0;i<max;i++)
    {
        //convert inputs into usable variables
        calc[i].n1  = atoi(argv[i*3+1]);
        calc[i].opr = argv[i*3+2][0];
        calc[i].n2  = atoi(argv[i*3+3]);

        // perform calulations
        switch(calc[i].opr) {
            case '+':
                printf("result %d: %d\n", i, calc[i].n1 + calc[i].n2);
                break;
            case '-':
                printf("result %d: %d\n", i, calc[i].n1 - calc[i].n2);
                break;
        }
    }

    return 0;
}

大多数错误检查和函数返回值的测试都由您完成。

关于c - "Debug assertion failed! expression: _p != nullptr"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58201639/

相关文章:

python - 在 cmd 模块 python 中调用菜单

command-line - Anaconda Prompt 命令行命令

c++ - matPutVariable: error (matrix::serialize::WrongSize) 试图输出数据到mat文件

c# - 将 OpenSsl SSL_ctrl 函数编码到 C#

c - 使用 C 中的函数从文件读取结构体数组

arrays - 我应该使用 {} 还是 {0} 来初始化数组/结构?

java - cmd win7 上的命令 'notepad' 仅适用于 system32?

objective-c - 在 Objective-C Foundation 命令行实用程序中处理用户输入

command-line - 在 ffmpeg drawtext 过滤器中提供时间段

c - '发送失败';在 C 中使用 sendto 函数、使用 UDP 套接字时出错