繁体   English   中英

如何在 C 中使用 POINTERS 创建一个二维字符数组?

[英]How can I create a 2D array of chars with POINTERS in C?

我正在尝试创建一个程序,用户输入项目数(行)并为每个项目指定一个名称(scanf),最多 30 个字符。

一旦我在 C 上学习这个,我想用指针的指针创建这个代码。

我在代码方面遇到了一些困难。

二维数组的草图。

代码片段:

PS: #define MAX 31


  char **items = NULL, *columns = NULL, name[MAX];
  int rows, aux_rows;

  printf("\nNumber of items:\n");
  scanf("%d", &rows);

  items = (char **) malloc(rows * sizeof(char*));

  for (aux_rows = 0; aux_rows < rows; aux_rows++){
    columns[aux_rows] = (char *) malloc(MAX * sizeof(char));
  }

  for (aux_rows = 0; aux_rows < rows; aux_rows++){
    printf("\nName of item %d:\n", (aux_rows + 1));
    scanf("%s", name);
    *items[aux_rows] = name;
  }

分配了items而不是columns 并使用strcpy复制字符。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 31

int main()
{
    char **items = NULL, *columns = NULL, name[MAX];
    int rows, aux_rows;

    printf("\nNumber of items:\n");
    scanf("%d", &rows);

    items = (char **)malloc(rows * sizeof(char *));

    for (aux_rows = 0; aux_rows < rows; aux_rows++)
    {
        items[aux_rows] = malloc(MAX * sizeof(char));
    }

    for (aux_rows = 0; aux_rows < rows; aux_rows++)
    {
        printf("\nName of item %d:\n", (aux_rows + 1));
        scanf("%s", name);
        strcpy(items[aux_rows], name);
    }
    return 0;
}
$ gcc array2d.c
$ ./a.out      

Number of items:
2

Name of item 1:
Hello 

Name of item 2:
World!
$ 
*items[aux_rows] = name;

在两个方面是错误的。

*[]都取消引用它们的一元操作数。 如果itemschar **items[n]char **items[n]char

这试图将一个数组分配给每个缓冲区的第一个元素。

其次,数组不能通过赋值来复制。 使用strcpy将字符串从一个缓冲区复制到另一个缓冲区。

也就是说,您可以简单地将字符串直接读取到预先分配的缓冲区中,并取消临时缓冲区。


在这一行中,

columns[aux_rows] = (char *) malloc(MAX * sizeof(char));

columns应该是items


一些注意事项:

sizeof (char)保证为1 它的使用是多余的。

malloc的返回不应在 C 中强制转换。

malloc可能会失败。 scanf可能会失败。 你应该养成不忽略返回值的习惯。

scanf("%s", ...)gets一样危险 至少,使用字段宽度说明符来限制输入(应该是缓冲区的大小减一)。

char foo[128];
if (1 != scanf("%127s", foo))
    /* handle error */;

请注意,使用%s限制输入不包含任何空格。 scanf通常是一个糟糕的工具,考虑使用fgets的基于行的方法。

话虽如此,使这合理安全的最小更改:

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

#define MAX 31

void die(const char *msg)
{
    fprintf(stderr, "%s\n", msg);
    exit(EXIT_FAILURE);
}

int main(void)
{
    size_t rows;

    printf("Number of items: ");

    if (1 != scanf("%zu", &rows))
        die("Failed to read input.");

    char **items = malloc(sizeof *items * rows);

    if (!items)
        die("Failed to allocate memory.");


    for (size_t i = 0; i < rows; i++) {
        if (!(items[i] = malloc(MAX)))
            die("Failed to allocate row.");

        printf("Name of item %zu: ", i + 1);

        if (1 != scanf("%30s", items[i]))
            die("Failed to read item input.");
    }

    for (size_t i = 0; i < rows; i++) {
        puts(items[i]);
        free(items[i]);
    }
}

暂无
暂无

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

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