【问题标题】:Printing a jagged array in c在c中打印锯齿状数组
【发布时间】:2019-02-18 20:11:03
【问题描述】:

所以我正在用 C 制作一个锯齿状数组。我认为填充数组的代码没问题,我遇到的问题是打印。

代码如下

#include <stdio.h>

int *length;
int **row;

int main(int argc, char *argv[]) {

    //make an array that contains the lengths of the columns for each row
    length = malloc(argc * sizeof (int));
    for (int k = 0; k < argc; k++) {
        length[k] = atoi(argv[k]);
       // printf("%d ", lengths[k]);
    }
    row = malloc(argc * sizeof (int));

    // fill the columns
    int fill = 1;
    for (int i = 0; i < argc; i++) {
        row[i] = malloc(sizeof (int) * length[i]);
        for (int j = 0; j < length[i]; j++)
            row[i][j] = fill;

    }

    //print it
    for (int i = 0; i < argc; i++)
        for (int j = 0; j < length[i]; j++)
            printf("%d", row[i][j]);

    return 0;


}

这个程序接受命令行参数,所以如果我输入:

./jagged 1 3 5 1

我应该得到:

1
1 1 1
1 1 1 1 1
1

相反,我的编译器只是说

RUN FAILED

【问题讨论】:

    标签: c arrays printf jagged-arrays


    【解决方案1】:

    argc == 5,而argv[0] 包含./jagged

    atoi 对于argv[0] 将失败,并且其错误行为未定义。如果程序继续,0 是返回的值,您将存储 0 作为长度之一。这意味着您将执行malloc(sizeof(int) * 0);,这也可能导致麻烦。

    要解决这些问题,您有两种选择:

    1. i = 1; i &lt; argc; i++ 循环以避免argv[0]
    2. 在使用argcargv 之前添加一行--argc, ++argv;

    您还应该考虑使用strtol 而不是atoi,因为它具有明确定义的错误行为。

    除了您的程序逻辑之外,您只是忘记包含&lt;stdlib.h&gt; 才能使用mallocatoi as @Vishal explained

    【讨论】:

      【解决方案2】:

      使用一些限制性编译器标志,例如--pedantic --std=c11 -Wall -Wextra 用于 gcc。这将帮助您找到一些错误,例如缺少自己。

      包括stdlib.h,以便您拨打malloc()

      argv[0] 不是您的第一个命令行参数,它是您的二进制文件的名称。所以你必须调整循环。

      为了简化没有给出参数的情况,请包含 assert.h 并使用 assert(argc &gt; 1); 检查 main 开头的参数数量。

      您对row 的分配不正确,而是依赖于平台,因为row 的元素是int * 类型而不是int。而是分配row = malloc((argc - 1) * sizeof(int *));

      【讨论】:

        【解决方案3】:

        您需要包含stdlib 才能使用malloc 函数并在第一个块之后打印\n

            #include <stdio.h>
            #include <stdlib.h>
            int *length;
            int **row;
        
            int main(int argc, char *argv[]) {
        
                //make an array that contains the lengths of the columns for each row
                length = malloc(argc * sizeof (int));
                for (int k = 0; k < argc; k++) {
                    length[k] = atoi(argv[k]);
                   // printf("%d ", lengths[k]);
                }
                row = malloc(argc * sizeof (int));
        
                // fill the columns
                int fill = 1;
                for (int i = 0; i < argc; i++) {
                    row[i] = malloc(sizeof (int) * length[i]);
                    for (int j = 0; j < length[i]; j++)
                        row[i][j] = fill;
        
                }
        
                //print it
                for (int i = 0; i < argc; i++)
                {
                    for (int j = 0; j < length[i]; j++){
                        printf("%d", row[i][j]);
                    }
                    printf("\n");
                }
        
                return 0;
        
        
            }
        

        【讨论】:

        • 谢谢,在您的回答和 Chrono 的回答之间,我能够让程序正常工作。
        • 乐于助人
        【解决方案4】:

        错误来源

        1> argv[0] 一定会失败。

        2> 没有为malloc 包含库(需要stdlib.h)。

        【讨论】:

        • 使用length作为数组没有什么问题。 *(length + i) == length[i] 由 C 标准指定。
        • @ChronoKitsune 我认为它需要增加 - 谢谢!
        猜你喜欢
        • 2015-11-30
        • 2021-12-01
        • 2020-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-10
        • 1970-01-01
        • 2018-03-04
        相关资源
        最近更新 更多