【发布时间】:2018-09-29 06:24:36
【问题描述】:
我目前无法理解以下情况: 我有一个多维字符串数组,我想只使用指针来解决它,但是在指针上使用数组注释时总是会出现分段错误。这只是一个示例代码,我想在 pthread 中使用 3D 数组,所以我想通过结构将它作为指针传递,但它不起作用,我想知道为什么?我认为指针和数组在功能上是等效的?下面是示例代码:
#include <stdio.h>
void func(unsigned char ***ptr);
int main() {
// Image of dimension 10 times 10
unsigned char image[10][10][3];
unsigned char ***ptr = image;
memcpy(image[0][0], "\120\200\12", 3);
// This works as expected
printf("Test: %s", image[0][0]);
func(image);
return 0;
}
void func(unsigned char ***ptr) {
// But here I get a Segmentation Fault but why??
printf("Ptr: %s", ptr[0][0]);
}
提前感谢您的帮助:)
【问题讨论】:
-
你不能用函数调用来初始化数组,只能用初始化器的文字列表。
-
声明数组为数组分配空间,无需调用
malloc()。 -
我需要,因为 10x10 只是实际应用程序中的一个示例,这是动态的,取决于图像尺寸... char[3] 代表每个 0...255 的 RPG 值图像中的像素
-
C允许变长数组,所以可以写
char test[height][width][3];
标签: c arrays pointers multidimensional-array