【问题标题】:Unable to insert char string into char array无法将 char 字符串插入 char 数组
【发布时间】:2019-04-09 12:51:37
【问题描述】:
  int row = 5;
  int column = 10;
  char **array;
  int rowcount = 0;
  array = (int **) malloc(sizeof(int *) * row);
  char *x_ptr = array;
  for (int i = 0; i < row; i++) {
    array[i] = (int *) malloc(sizeof(int) * column);
    x_ptr[i] = (int *) malloc(column * sizeof(int));
  }
  for (int i = 0; i < row; i++)
  {

    for (int j = 0; j < column; j++) {
      if (j == 0) {
        rowcount += 1;
        char snum[5] = {'\0'};
        sprintf(snum, "%d", rowcount); //converts int to char
        for (int t = 0; t < strlen(snum); t++)
          *(x_ptr + (i * column + j) + t) = snum[t];
      } else {
        *(x_ptr + (i * column + j)) = 0;
      }
    }

只是尝试在第 0 列的数组中添加一些整数值。但是,当尝试添加诸如 10 之类的数字时,sprintf 命令将值拆分为 snum[0]=49'1' 和 snum[1]=48' 0'。但是数组只接受 snum[0] 并且 snum[1] 被完全忽略。

如果我对指针位置的理解有误,请指正。

【问题讨论】:

  • 欢迎来到stackoverflow。请阅读:How to Ask 并发布minimal reproducible example
  • 使用 C 代码的第 1 步:将编译器设置为最高警告级别,并将所有警告视为错误。您的代码会生成多个警告,例如来自不兼容指针类型的赋值赋值从没有强制转换的指针中生成整数。在尝试运行程序之前,您需要修复这些警告。
  • 您不应该通过强制转换来消除警告(在使用"%p" 打印指针值时,(void*) 除外)。如果您以其他方式进行投射以避免警告 - 您可能做错了什么。 malloc 的返回不需要强制转换,没有必要。见:Do I cast the result of malloc?
  • 1) 审查char *x_ptr = array; 2) 为什么声明charchar **array; 然后分配给intarray[i] = (int *)malloc(sizeof(int)*column);?推荐array[i] = malloc(sizeof *(array[i]) * column);以避免此类类型不匹配。
  • "但是数组只接受 .." --> 这怎么知道的?很可能用于确定这一点的代码无法恢复打印 x_ptr[i][i] - x_ptr[i][i] is not a string without a null character

标签: c pointers for-loop multidimensional-array


【解决方案1】:

好的,我已经很努力了,但是所以这段代码有很多问题,基本上是无法挽回的。请仔细查看上面的 cmets,发布一些可编译的代码,告诉我们您期望它做什么,它做什么 做什么以及这两者之间的区别。

这个谜的核心可能是您使用两种不同的技术来访问二维数组。假设您想要一个包含n 行和m 列的整数数组。你可以通过声明来做到这一点:

int **array1 = malloc(n * sizeof(int *));
for (int a=0; a<n; a++)
  array1[a] = malloc(m * sizeof(int));

这样,您可以将i 行和j 列中的整数作为array1[i][j] 访问。

或者,您可以通过声明来做到这一点:

int *array2 = malloc(n * m * sizeof(int));

并以array2[i * m + j] 访问i 行和j 列中的整数。

这是一些应该编译的代码

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

int row = 5;
int column = 10;
//  char **array;  // declaration moved into main()
int rowcount = 0;

int main(void) {  // added main() function
  int **array = malloc(sizeof(int *) * row); // include type (int **); remove cast: (int **); moved into main()
  int **x_ptr = array;  // changed type to int **; moved into main()

  // Allocate memory for array, so we can assign integers to
  //   array[i][j] where:
  //     i <= 0 < column
  //     j <= 0 < column
  //  Note that:
  //    array[i]
  //  is identical to:
  //    *(array + i)
  //  and:
  //    array[i][j]
  //  is identical to:
  //    *(*(array + i) + j)
  for (int i = 0; i < row; i++) {
    // array[i] = malloc(sizeof(int) * column);  // removed cast, you're not using array[] anyway
    x_ptr[i] = malloc(column * sizeof(int));  // removed cast
  } 

  for (int i = 0; i < row; i++)
  {

    for (int j = 0; j < column; j++) {
      if (j == 0) {
        rowcount += 1;

        // Create a character buffer.
        char snum[5] = {'\0'};

        // Write the text version of integer "rowcount" into the char
        //   buffer "snum"
        sprintf(snum, "%d", rowcount); //converts int to char

        // Copy the char value of each character into the
        //   two-dimensional integer array. We want to copy
        //   snum[t] into array[i][t]
        for (size_t t = 0; t < strlen(snum); t++)
          // *(x_ptr + (i * column + j) + t) = snum[t];
          array[i][t] = snum[t];
      } else {
        // *(x_ptr + (i * column + j)) = 0;
        array[i][j] = 0;
      }
    }
  }

  for (int i=0; i<row; i++)
    for (int j=0; j<column; j++)
      printf("Row %d, column %d has value %d\n", i, j, array[i][j]);

  return 0;
}

我将文件保存为so.c,我正在使用以下命令对其进行编译:

gcc -o so -W -Wall -pedantic so.c

这会针对您不想在原始代码中执行的许多操作打开警告。它可能无法解决您遇到的确切问题,但它应该可以为您提供一些工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-20
    • 2012-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 2011-02-07
    • 2012-03-06
    相关资源
    最近更新 更多