是什么的char * S []和char(* S)[]有什么区别? [英] What is the difference between char *s[] and char (*s)[]?

查看:185
本文介绍了是什么的char * S []和char(* S)[]有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我读到关于C语言的书籍,二级指针困扰我很多东西。

When I read books about C language, the two level pointer bothered me a lot.

char s[5][5];
char *s[5];
char (*s)[5];

所以它们之间有什么区别呢?

so what is the difference between them?

推荐答案

在C,最好是说出来的声明。然后,变得直观。对于这一点,你跟着左右约定。这是怎么一回事:

In C, it is better to speak out the declaration. Then, it becomes intuitive. For this, you follow the right-left convention. Here is how it goes:

char *s[5];

你怎么说话的呢?对于您在开始的变量名取值的右边,然后往左边走。所以你说s是一个/一个开始,在右边,你会看到一个 [] 和你说S是一个数组...。然后你向左走,看到了 * ,并说:s是一个指针数组。这是它是什么。它州市5指针数组。您可以在此阵列中存储不同的指针。

How do you speak it? For that you start at the right of the variable name s and then go to the left. So you start by saying "s is a/an", On the right, you see a [] and you say "s is an array ... ". And then you go to the left and see the *, and say "s is an array of pointers." And that is what it is. It si an array of 5 pointers. You can store different pointers in this array.

现在的另外一个:

char (*s)[5];

您开始以同样的方式。 s是/是,然后看()。内的任何的()势必取值比什么都更接近外。因此, * 更紧密的S电子 [] 。所以现在你说,S是一个指针......现在你走出paranthesis,看到了 [] 。所以,你继续,s是一个指向数组的指针。而这正是它是什么。这是一个指针,该指针将指向数组的第一个元素。

You start the same way. "s is a/an", and then look at the (). Anything within the () is bound to s closer than anything outside. So the * is more closely bound with s than []. So You now say, "s is a pointer ..." and now you go out of the paranthesis and see the []. So you continue, "s is a pointer to an array". And that is exactly what it is. It is a pointer, which will point to the first element of an array.

现在遵循同样的逻辑,并试着猜测下面将是什么:

Now follow the same logic and try to guess what the following will be:

int (*callme)(int a, int b)
int (*callme[10])(int a, int b)

提示,最后一个可用于创建函数查找表。

Hint, the last one can be used to create lookup tables for functions.

编辑:

作为评价所提到的,也有一个在开始。我从来没有贲能弄明白speakign一个简单的方法,但一般是从上下文明确。例如,在第一示例中,在定义了阵列的类型,而在第二个例子,它定义指针。在练习我已经发布的 INT 定义类型的函数的返回值。一般用如这些定义,将有只有一个项目与未定义类型。而这就是我如何找出其中的类型去。

As mentioned in the comments, there is also a char in the beginning. I have never ben able to figure out an easy way of speakign this, but is generally clear from the context. For example, in the first example, the char defines the type of the array, while in the second example, it defines pointer. In the exercises I have posted, the int defines the type for the return values of the functions. Generally with definitions such as these, there will be exactly one item with the undefined type. And thats how I figure out where the type goes.

这篇关于是什么的char * S []和char(* S)[]有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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