问题描述
我正在学习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\n", aoa[0][0]); //print 0th element of 0th array.
}
int main()
{
char ***array = NULL;
parse(array);
printf("%s\n", array[0][0]);
return 1;
}
aoa(数组数组)在堆上,因此两种方法都应该相同.它的确在parse函数中打印了"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\n", string_array[i][j] );
}
printf("\n");
}
}
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!