C双字符指针的声明和初始化 [英] C double character pointer declaration and initialization

查看:105
本文介绍了C双字符指针的声明和初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是这样声明

char *c = "line";

char c[] = "line";

所以我做到了

char **choices = { "New Game", "Continue Game", "Exit" };

哪个给我一个不兼容的指针类型,其中

Which gives me an incompatible pointer type, where

char *choices[] = { "New Game", "Continue Game", "Exit" };

没有.对了解这一点有帮助吗?

doesn't. Any help on understanding this?

推荐答案

嗯,它们不一样.对于大多数人来说,将它们视为相同会更容易,因此每个人都开始以这种方式思考,直到遇到上述问题:-)

Well, they're not the same. It's just easier for most people to think of them as being the same so everyone starts to think that way until they run into a problem like the above :-)

我本来会写很多篇文章,但是后来我想起来……其他人一定已经做到了.他们有.这是一个很好的解释:

I was going to write something long and winded, but then I figured... Someone else must have done this already. And they have. This is a pretty good explanation:

http://www.lysator.liu.se/c/c-faq/c-2.html

最简单的思考方法是:当您执行以下操作时:

The easiest way to think about it is that when you do something like:


  char *foo = "something";

您实际上正在执行以下操作:

You're really doing something like:


  char randomblob[] = "something";
  char *foo = randomblob;

现在...这实际上不是一张准确的图片(尽管我不是编译专家).至少可以让您以更正确的方式思考问题.

Now... that's not really an accurate picture (though I'm not a compiler expert). It at least lets you think about things in a slightly more correct fashion.

因此,回到您的问题所在,如果 I 理解正确(永远不能保证),则无法在C语言中执行示例行3.一个可以在这里做正确的事情的编译器,但是gcc却做不到.但是,第4个示例做了正确的事",并为您提供了一个指针数组,每个指针本身都指向const char数组".

So, back to your problem, if I understand things right (which is never guaranteed), you can't do your example line #3 in C. You're right that someone could write a compiler that would do the right thing here, but gcc doesn't. The 4th example, however, does the "right thing" and gives you "an array of pointers that are each pointing to a const char array themselves".

我曾经浏览过一个网页,该网页会将复杂的C类型转换为英语.虽然那可能是在90年代初期,但是我敢打赌,如果您在Google上搜索得足够多,它将为您提供比我刚刚讲过的更准确的措词说明.

I once ran across a web page that would translate a complex C type into English. That was probably in the early 90s though but I bet if you google enough it would give you a more accurate wording description than the one I just whipped up.

这篇关于C双字符指针的声明和初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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