c - 无法将 char 字符串插入 char 数组

标签 c pointers for-loop multidimensional-array

  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] 被完全忽略。

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

最佳答案

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

这个谜团的核心可能是您正在使用两种不同的技术来访问二维数组。假设您想要一个包含 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));

并访问 i 行和 j 列中的整数作为 array2[i * m + 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

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

关于c - 无法将 char 字符串插入 char 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53166645/

相关文章:

c - 堆栈在 32 位和 64 位处理器中有何不同

c - 重新声明枚举器

c - 为什么我不能管理 ~382MB 的内存?

c++ - 函数返回后如何删除堆分配的变量

c - 给C中的指针赋值

iOS:for循环中对象的属性为空?

char ** array = malloc(sizeof(char*)*len) vs malloc(sizeof(char)*len)

C - 如何从指向该值的指针访问值?

for-loop - go 中循环和 goroutinues 的意外行为

bash - 在 bash 中循环元组?