在C / C ++,为char * arrayName中的[] []指针的指针的指针或指针的指针? [英] In C/C++, is char* arrayName[][] a pointer to a pointer to a pointer OR a pointer to a pointer?

查看:106
本文介绍了在C / C ++,为char * arrayName中的[] []指针的指针的指针或指针的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白多维数组的指针的指针,但也许我错了?

例如,我以为:

的char *变种 = 字符VAR []

的char ** VAR = 的char *变种[] 字符VAR [] []

字符*** VAR = 字符VAR [] [] [] 字符*变种[] [] 的char ** VAR []

这是不正确的?我很困惑,因为我看到了一个char * [] []转换为一个简单的教科书的例子一个char **。

我粘贴下面的例子。任何人都可以清除此为我?谢谢!


  / *一个简单的字典。 * /
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&文件ctype.h GT;/ *文字和含义列表* /字符* DIC [] [40] = {
    图集,A地图的音量。
    汽车,A机动车。
    电话,通信设备。,
    飞机,一种飞行器。
    ,/ *空结束列表* /
};INT主要(无效)
{
    字符字[80],CH;
    焦炭** P;做{
    看跌期权(\\ n输入字:);
    scanf函数(%S字);
    P =(字符**)DIC;
    / *找到匹配的单词并打印其含义* /
    做{
        如果(!的strcmp(* P,字)){
            看跌期权(含义:);
            看跌(*(P + 1));
            打破;
            }        如果中断(STRCMP(* P,字)!);        P = P + 2; / *提前通过列表* /
        }而(* P);    如果(!* P)看跌期权(字没有在字典里。);
    的printf(另一个(Y / N)?);
    scanf函数(%C%* C,&安培; CH);    }而(TOUPPER(CH)='N'!);返回0;}


解决方案

有关的ç如下:


6.3.2.1左值,数组和功能代号的结果
...结果
3除非是在的sizeof 操作符的运算数或一元操作符,或者是用于初始化数组,有一个前pression一个字符串文字类型''类型的数组''转换为类型指向数组对象的初始元素,而不是一个左值'指向类型为'一个前pression。如果数组对象有注册存储类,这种行为是未定义。

有关 C ++ 语言有一点不同:


4.2阵列到指针转换[conv.array] 的结果
结果
1左值或类型数组NT·的或必然的T未知的数组的右值可以转换为类型指针T的右值。其结果是一个指针数组的第一个元素。结果
...结果
8.3.4数组[dcl.array] 的结果
...结果
7一致的规则遵循多维数组。如果E级的n维数组×Ĵ×...× K ,则E出现在一个前pression转换为一个指针( N -1)维数组等级Ĵ×...× K 。如果 * 操作符,明确或含蓄地作为下标的结果,施加到这个指针,结果是指向的(名词的-1)维阵列,其本身被立即转换成一个指针。

所以下面都持真:

 宣言防爆pression型衰减到
----------- ---------- ---- ---------
     T A [N]一个T [N] T *
                           &放大器;一个T(*)[N]
                           *在
                         一个[我] T ...  T A [M] [N]一个T [M] [N] T(*)[N]
                           &放大器;一个T(*)[M] [N]
                           *一个T [N] T *
                         一个[I] T [N] T *
                        &放大器;一个[I] T(*)[N]
                        *一个[我] T ...
                      一个[I] [J] T ...T A [M] [N] [O]一个T [M] [N] [O] T(*)[M] [N]
                           &放大器;一个T(*)[M] [N] [O]
                           *一个T [M] [N] T(*)[N]
                         一个[I] T [M] [N] T(*)[N]
                        &放大器;一个[I] T(*)[M] [N]
                        * A [I] T [N] T *
                      一个由[i] [j]的T [N] T *
                     &放大器;一个由[i] [j]的T(*)[N]
                     * A [I] [J] T ...
                   一个由[i] [j]的[K] T ...

上的图案应该明确对高维数组。

因此​​,让我们分析一下你的字典:

  / *文字和含义列表* /字符* DIC [] [40] = {
    图集,A地图的音量。
    汽车,A机动车。
    电话,通信设备。,
    飞机,一种飞行器。
    ,/ *空结束列表* /
};

这是不会设置你的字典你想要的方式;你已经基本上其设置为40个三分球1个元素的数组为char。如果你想对字符串数组,那么该声明应该是这样的:

 的char * DIC [] [2] =
{
  {图册,地图的体积},
  {汽车,A机动车},
  {电话,通信装置},
  {飞机,飞行机},
  {NULL,NULL} //空字符串和空值是不同的东西。
};

类型的 DIC 是5个元素的数组字符指针的2元阵列,或的char * [5] [ 2] 。按上述规则,前pression DIC 应衰减到的char *(*)[2] 展望 - 一个指向指针的2个元素的数组为char。

搜索这本字典的函数则是这样的:

 的char *定义(字符*来看,字符*(*字典)[2])// *不*字符***词典
{
  而((*字典)[0] = NULL&放大器;!&安培;!STRCMP((*字典)[0],期限)= 0)
    字典++;
  返回(*字典)[1];
}

,你会调用它从你的主要功能如

 的char * DEF =定义(术语,DIC);

请注意,我们必须使用周围的 *字典前pression括号中的作用。数组下标运算符 [] 具有较高的precedence比引用操作 * ,我们不希望为下标为字典直接,我们希望下标处的数组字典

I understood multi-dimensional arrays as pointers to pointers, but perhaps I am wrong?

For example, I though:

char * var = char var[]

char ** var = char* var[] or char var[][]

char *** var = char var[][][] or char* var[][] or char** var[]

Is this incorrect? I was confused because I saw a char*[][] cast as a char** in a simple text book example.

I pasted the example below. Can anyone clear this up for me? Thanks!


/* A simple dictionary. */
#include <stdio.h>
#include <string.h>
#include <ctype.h>

/* list of words and meanings */

char  *dic[][40] = {
    "atlas", "A volume of maps.",
    "car", "A motorized vehicle.",
    "telephone", "A communication device.",
    "airplane", "A flying machine.",
    "", ""  /* null terminate the list */
};

int main(void)
{
    char word[80], ch;
    char **p;

do {
    puts("\nEnter word: ");
    scanf("%s", word);
    p = (char **)dic;
    /* find matching word and print its meaning */
    do {
        if(!strcmp(*p, word)) {
            puts("Meaning:");
            puts(*(p+1));
            break;
            }

        if(!strcmp(*p, word)) break;

        p = p + 2;  /* advance through the list */
        } while(*p);

    if(!*p) puts("Word not in dictionary.");
    printf("Another? (y/n): ");
    scanf(" %c%*c", &ch);

    } while(toupper(ch) != 'N');

return 0;

}

解决方案

The rule for C is as follows:

6.3.2.1 Lvalues, arrays, and function designators
...
3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

The language for C++ is a little different:

4.2 Array-to-pointer conversion [conv.array]

1 An lvalue or rvalue of type "array of N T" or "array of unknown bound of T" can be converted to an rvalue of type "pointer to T". The result is a pointer to the first element of the array.
...
8.3.4 Arrays [dcl.array]
...
7 A consistent rule is followed for multidimensional arrays. If E is an n-dimensional array of rank i × j × ... × k, then E appearing in an expression is converted to a pointer to an (n−1)-dimensional array with rank j × ... × k. If the * operator, either explicitly or implicitly as a result of subscripting, is applied to this pointer, the result is the pointed-to (n−1)-dimensional array, which itself is immediately converted into a pointer.

So the following all hold true:

Declaration        Expression        Type             Decays to
-----------        ----------        ----             ---------
     T a[N]                 a        T [N]            T *
                           &a        T (*)[N]     
                           *a        T
                         a[i]        T

  T a[M][N]                 a        T [M][N]         T (*)[N]
                           &a        T (*)[M][N]  
                           *a        T [N]            T *
                         a[i]        T [N]            T *
                        &a[i]        T (*)[N]      
                        *a[i]        T
                      a[i][j]        T

T a[M][N][O]                a        T [M][N][O]      T (*)[M][N]
                           &a        T (*)[M][N][O]
                           *a        T [M][N]         T (*)[N]
                         a[i]        T [M][N]         T (*)[N]
                        &a[i]        T (*)[M][N]  
                        *a[i]        T [N]            T *
                      a[i][j]        T [N]            T *
                     &a[i][j]        T (*)[N]
                     *a[i][j]        T
                   a[i][j][k]        T

The pattern should be clear for higher-dimensional arrays.

So let's analyze your dictionary:

/* list of words and meanings */         

char  *dic[][40] = {         
    "atlas", "A volume of maps.",         
    "car", "A motorized vehicle.",         
    "telephone", "A communication device.",         
    "airplane", "A flying machine.",         
    "", ""  /* null terminate the list */         
};

This isn't going to set up your dictionary the way you want; you've basically set this up as a 1-element array of 40 pointers to char. If you want an array of pairs of strings, then the declaration should look like this:

char *dic[][2] = 
{
  {"atlas", "A volume of maps"},
  {"car", "A motorized vehicle"},
  {"telephone", "A communication device"},
  {"airplane" , "A flying machine"},
  {NULL, NULL} // empty strings and NULLs are different things.  
}; 

The type of dic is "5-element array of 2-element arrays of pointer to char", or char *[5][2]. Going by the rules above, the expression dic should decay to char *(*)[2] -- a pointer to a 2-element array of pointer to char.

A function to search this dictionary would then look like this:

char *definition(char *term, char *(*dictionary)[2]) // *NOT* char ***dictionary
{
  while ((*dictionary)[0] != NULL && strcmp((*dictionary)[0], term) != 0)
    dictionary++;
  return (*dictionary)[1];
}

and you would call it from your main function like

char *def = definition(term, dic);

Note that we have to use parentheses around the *dictionary expression in the function. The array subscript operator [] has higher precedence than the dereference operator *, and we don't want to subscript into dictionary directly, we want to subscript into the array that dictionary points to.

这篇关于在C / C ++,为char * arrayName中的[] []指针的指针的指针或指针的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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