我原本认为多维数组是指向指针的指针,但也许我错了?
例如,我认为:
char * var
= char var[]
char ** var
= char* var[]
或者 char var[][]
char *** var
= char var[][][]
或者 char* var[][]
或者 char** var[]
这样理解是错误的吗?我感到困惑是因为在一本简单的教材例子中,看到了一个 char*[][] 被强制转换为 char**。
我把例子贴在下面,有人能为我解释清楚吗?谢谢!
/* 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;
}
std::map
比您在此处使用的更好。 - Billy ONeal