堆上的多维数组 - C [英] Multidimensional array on the heap - C

查看:12
本文介绍了堆上的多维数组 - C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 C 并尝试创建一个可以创建字符串数组的函数.

I am learning C and trying to make a function that would create an array of arrays of strings.

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


void parse(char ***aoa)
{
char *string = calloc(9, sizeof(char));        //create a string of size 8+1
strcpy(string, "hi world");                   // put text in that array
char **array = calloc(10, sizeof(char *));    //create an array of strings
aoa = calloc(10, sizeof(char *));             //create and array of arrays
aoa[0] = array;                               //assign string array to the 0th elements of new array
array[0] = string;                            //assign our string to 0th element of string carry
printf("%s
", aoa[0][0]);                    //print 0th element of 0th array. 
}


int main()
{
char ***array = NULL;
parse(array);
printf("%s
", array[0][0]);
return 1;
}

aoa(数组的数组)在堆上,所以这两种方法应该是一样的.它确实在解析函数中打印hi world",但在 main 中给出了 Segmentation Fault,我的代码有什么问题?

The aoa (array of arrays) is on the heap so it should be the same for both methods. It does print "hi world" in the parse function, but gives Segmentation Fault in main, what is the problem with my code?

显然我需要释放所有内容并进行错误检查,但我删除了它以显示问题的要点

Obviously I need to free() everything and do error checking, but I removed it to show the gist of the problem

推荐答案

有一个合理的经验法则是,如果您发现自己需要超过两个间接级别,那么您的程序设计就是糟糕的.(《三星级编程》)

There is a sound rule of thumb saying that if you ever find yourself in need of more than two levels of indirection, then your program design is bad. ("Three star programming")

您还使用基于指针的查找表而不是数组.这会导致分段并阻止您将分配的结果视为一块连续的内存.

You are also using pointer-based lookup-tables rather than arrays. This causes segmentation and prevents you to regard the allocated result as a chunk of contiguous memory.

除此之外,您的代码还有其他几个问题.我会考虑从头开始重写它并实际使用多维数组.这是一个工作示例:

On top of that, your code has several other issues. I would consider rewriting it from scratch and actually use multi-dimensional arrays instead. Here is a working example:

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

bool create_string_array2d (size_t x, 
                            size_t y, 
                            size_t max_length, 
                            char (**string_array)[x][y][max_length]);

void print_string_array2d (size_t x,
                           size_t y,
                           size_t max_length,
                           char string_array[x][y][max_length]);

static void fill_with_junk (size_t x,
                            size_t y,
                            size_t max_length,
                            char string_array[x][y][max_length]);

int main()
{
  const size_t X = 9;
  const size_t Y = 10;
  const size_t MAX_CHARS = sizeof("hi world xx yy");

  char (*array)[X][Y][MAX_CHARS];
  bool result;

  result = create_string_array2d(X, Y, MAX_CHARS, &array);

  if(result == false)
  {
    printf("out of memory, halt & catch fire");
    return 0;
  }

  fill_with_junk(X, Y, MAX_CHARS, *array);

  print_string_array2d(X, Y, MAX_CHARS, *array);

  free(array);

  return 0;
}


bool create_string_array2d (size_t x, 
                            size_t y, 
                            size_t max_length, 
                            char (**string_array)[x][y][max_length])
{
  *string_array = calloc(1, sizeof(char[x][y][max_length]));

  return string_array != NULL;
}

void print_string_array2d (size_t x,
                           size_t y,
                           size_t max_length,
                           char string_array[x][y][max_length])
{
  for(size_t i=0; i<x; i++)
  {
    for(size_t j=0; j<y; j++)
    {
      printf("%s
", string_array[i][j] );
    }
    printf("
");
  }
}

static void fill_with_junk (size_t x,
                            size_t y,
                            size_t max_length,
                            char string_array [x][y][max_length])
{
  for(size_t i=0; i<x; i++)
  {
    for(size_t j=0; j<y; j++)
    {
      char junk [sizeof("hi world xx yy ")] = "hi world ";
      char num  [sizeof("xx ")];
      sprintf(num, "%.2d ", (int)i);
      strcat(junk, num);
      sprintf(num, "%.2d", (int)j);
      strcat(junk, num);

      strcpy(string_array[i][j], junk);
    }
  }
}

这篇关于堆上的多维数组 - C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆