2D char数组和char **(OR,3D char数组和char ***等)之间的差异 [英] Difference between 2D char Array and char** (OR, 3D char Array and char*** etc)

查看:129
本文介绍了2D char数组和char **(OR,3D char数组和char ***等)之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我已经审查了这些:

<一个href=\"http://stackoverflow.com/questions/2565039/how-are-multi-dimensional-arrays-formatted-in-memory\">How在多维数组在内存格式化?

存储器映射在C <二维数组/ p>

从那里,它被称为是一个二维数组的不一样的的为char **,但在内存中他们期待的完全相同的。这听起来很奇怪,所以我已经研究了以下内容:

 的#include&LT;&stdio.h中GT;焦炭FUNC(字符** M){
    返回米[0] [0]; //仅适用于CHAR **,在其他SO问题已经讨论
}诠释主(){    // char的一个​​[4] [2]; //二维字符数组    INT行= 4,列= 2; // **炭
    焦炭** A =的malloc(行*的sizeof(字符*));
    INT I;
    对于(i = 0; I&LT;排;我++){
        一个由[i] =的malloc(COL *的sizeof(字符));
    }    //检查输出
    的printf(&安培; A =%U \\ n,&安培; A);
    的printf(&安培; A [0] =%U \\ n,&安培; A [0]);
    的printf(&安培; A [0] [0] =%U \\ n,&安培; A [0] [0]);
    输出(A =%U \\ N,一);
    输出(A [0] =%U \\ n,一个[0]);    //输出(A [0] [0] =%U \\ n,一个[0] [0]);在[0] [0],这里的垃圾值// char值    //字符M = FUNC(一); //仅适用于CHAR **,在其他SO问题已经讨论    返回0;
}

对于char可能的输出**:

 &安培; A = 3209288 //&放大器;一
   &放大器;一个[0] = 4083720 //及(*(A + 0))=一
&放大器;一个[0] [0] = 4083784 //及(*(*(A + 0)+ 0))= *一
       A = 4083720 //一
    一个[0] = 4083784 // *(A + 0)= *一

有关二维字符数组可能的输出:

 &安培; A = 3473104 //&放大器;一
   &放大器;一个[0] = 3473104 //一个
&放大器;一个[0] [0] = 3473104 // *一
       A = 3473104 //一
    一个[0] = 3473104 // *一

这是很容易理解的char **输出。但是,尽管它在其他SO问题讨论二维字符数组的输出看起来怪怪的。我想不出任何数据类型的指针的 X 时,


  

X = &放大器; X = * X


和所有的3样东西物理驻留在相同的内存块。希望我的困惑是可以理解的。任何人能解释的奥秘?


解决方案

在使用数组的名字,除了&放任何Ex pression;阵列的sizeof阵列这个名称将是全自动转换为指针数组(的char [5] [10] 的第一个元素将被转换为字符(*)[10] )。这个指针地址将等于整个阵列的ADRESS。结果
所以,的sizeof(字符[5] [10])== 50,不存在附加的指针。

 改编的char [5] [10];&放大器;一个= 3473104 //整个阵列,地址(炭(*)[5] [10])
&放大器;第一行的[0] = 3473104 //地址,(炭(*)[10])
&放大器;第一个字符的[0] [0] = 3473104 //地址,(字符*)
一个= 3473104 //一,(炭[5] [10]),转换为与&amp;一个[0],(炭(*)[10])
一个[0] = 3473104 //一个[0],(字符[10]),转化ロ与&amp;一个[0] [0],(字符*)

Firstly, I've already reviewed these:

How are multi-dimensional arrays formatted in memory?

Memory map for a 2D array in C

From there, it is known that a 2D array is not the same as char** but in memory they look exactly same. This sounds strange and so I've researched the following:

#include <stdio.h>

char func(char** m) {
    return m[0][0]; //only works for char**, already discussed in the other SO question
}

int main() {

    //char a[4][2]; //a 2D char array

    int row = 4, col = 2;   //char**
    char** a = malloc(row * sizeof(char*));
    int i;
    for (i = 0; i < row; i++) {
        a[i] = malloc(col * sizeof(char));
    }

    //checking the output
    printf("      &a = %u\n", &a);
    printf("   &a[0] = %u\n", &a[0]);
    printf("&a[0][0] = %u\n", &a[0][0]);
    printf("       a = %u\n", a);
    printf("    a[0] = %u\n", a[0]);

    //printf(" a[0][0] = %u\n", a[0][0]);   //char value in a[0][0], here a garbage value

    //char m = func(a); //only works for char**, already discussed in the other SO question

    return 0;
}

Possible output for char** :

      &a = 3209288  //                  &a
   &a[0] = 4083720  // &(*(a+0))      =  a
&a[0][0] = 4083784  // &(*(*(a+0)+0)) = *a
       a = 4083720  //                   a
    a[0] = 4083784  // *(a+0)         = *a

Possible output for 2D char array :

      &a = 3473104  // &a
   &a[0] = 3473104  //  a
&a[0][0] = 3473104  // *a
       a = 3473104  //  a
    a[0] = 3473104  // *a

It is easy to understand the output of char**. But the output of 2D char array looks strange though it was discussed in the other SO question. I cannot think of a pointer x of whatever data-type when,

x = &x = *x

and all the 3 things physically reside in the same block of memory. Hope that my confusion is understandable. Can anyone explain the mystery?

解决方案

When you use name of the array is any expression except &array and sizeof array this name will be automaticly converted to pointer to the first element of the array (char [5][10] will be converted to char (*)[10]). Address of this pointer will be equal to the adress of entire array.
So, "sizeof (char [5][10]) == 50", there is no additional pointers.

char arr[5][10];

&a = 3473104 // Address of entire array, (char (*)[5][10])
&a[0] = 3473104 // Address of first row, (char (*)[10])
&a[0][0] = 3473104 // Address of first char, (char *)
a = 3473104 // "a", (char [5][10]), converted to "&a[0]", (char (*)[10])
a[0] = 3473104 // "a[0]", (char[10]), converted ro "&a[0][0]", (char *)

这篇关于2D char数组和char **(OR,3D char数组和char ***等)之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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