为什么取消引用2次工作和3次给出细分错误

时间:2018-10-26 05:28:05

标签: c arrays pointers segmentation-fault

它如何打印带有两个解引用的3d数组的元素。我以为它需要3取消对printf("%c",*(*(*(a+0)+1)+1));的引用。有谅解危机。

int main() 
{ 
    char a[2][3][3] = {'a','b','c','d','e','f','g', 
                           'h','i','j','k','l','m'}; 
    printf("%s ", **a); 
    getchar(); 
    return 0; 
} 

1 个答案:

答案 0 :(得分:0)

为指定的%s提供的参数应该是一个指针。对于’%s , printf uses the pointer to fet characters from memory. Therefore, for a three-dimensional array, applying two * to the name results in the correct type, a pointer to字符`。

对于%c,自变量应为带有字符值的int。将三个*应用于数组即可完成此操作。

注意:尽管**a%s提供了正确的类型,但该参数应该指向以空值终止的一维字符串中的字符。允许字符在数组维度上继续进行是不明智的。