C 指针:指向固定大小的数组 [英] C pointers : pointing to an array of fixed size

查看:28
本文介绍了C 指针:指向固定大小的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题问了那里的 C 专家:

This question goes out to the C gurus out there:

在 C 中,可以按如下方式声明指针:

In C, it is possible to declare a pointer as follows:

char (* p)[10];

.. 基本上说明这个指针指向一个 10 个字符的数组.像这样声明指针的巧妙之处在于,如果您尝试将不同大小的数组的指针分配给 p,则会出现编译时错误.如果您尝试将一个简单的 char 指针的值分配给 p,它也会给您一个编译时错误.我用 gcc 试过这个,它似乎适用于 ANSI、C89 和 C99.

.. which basically states that this pointer points to an array of 10 chars. The neat thing about declaring a pointer like this is that you will get a compile time error if you try to assign a pointer of an array of different size to p. It will also give you a compile time error if you try to assign the value of a simple char pointer to p. I tried this with gcc and it seems to work with ANSI, C89 and C99.

在我看来,像这样声明一个指针会非常有用——尤其是在将指针传递给函数时.通常,人们会像这样编写这样一个函数的原型:

It looks to me like declaring a pointer like this would be very useful - particularly, when passing a pointer to a function. Usually, people would write the prototype of such a function like this:

void foo(char * p, int plen);

如果您需要一个特定大小的缓冲区,您只需测试 plen 的值.但是,您不能保证将 p 传递给您的人真的会在该缓冲区中为您提供充足的有效内存位置.您必须相信调用此函数的人正在做正确的事情.另一方面:

If you were expecting a buffer of an specific size, you would simply test the value of plen. However, you cannot be guaranteed that the person who passes p to you will really give you plen valid memory locations in that buffer. You have to trust that the person who called this function is doing the right thing. On the other hand:

void foo(char (*p)[10]);

..会强制调用者给你一个指定大小的缓冲区.

..would force the caller to give you a buffer of the specified size.

这看起来很有用,但我从未在我遇到过的任何代码中看到这样声明的指针.

This seems very useful but I have never seen a pointer declared like this in any code I have ever ran across.

我的问题是:人们不声明这样的指针有什么理由吗?我没有看到一些明显的陷阱吗?

My question is: Is there any reason why people do not declare pointers like this? Am I not seeing some obvious pitfall?

推荐答案

我想添加到 AndreyT 的答案中(以防有人偶然发现此页面以寻找有关此主题的更多信息):

I would like to add to AndreyT's answer (in case anyone stumbles upon this page looking for more info on this topic):

当我开始更多地使用这些声明时,我意识到在 C 中存在与它们相关的主要障碍(显然不是在 C++ 中).在这种情况下,您希望为调用者提供一个指向您写入的缓冲区的常量指针是很常见的.不幸的是,在 C 中声明这样的指针时这是不可能的.换句话说,C 标准(6.7.3 - 第 8 段)与以下内容不一致:

As I begin to play more with these declarations, I realize that there is major handicap associated with them in C (apparently not in C++). It is fairly common to have a situation where you would like to give a caller a const pointer to a buffer you have written into. Unfortunately, this is not possible when declaring a pointer like this in C. In other words, the C standard (6.7.3 - Paragraph 8) is at odds with something like this:


   int array[9];

   const int (* p2)[9] = &array;  /* Not legal unless array is const as well */

C++ 中似乎没有这种约束,这使得这些类型的声明更加有用.但是在 C 的情况下,每当您想要一个指向固定大小缓冲区的 const 指针时,就必须回退到常规指针声明(除非缓冲区本身被声明为 const 开始).您可以在此邮件线程中找到更多信息:链接文本

This constraint does not seem to be present in C++, making these type of declarations far more useful. But in the case of C, it is necessary to fall back to a regular pointer declaration whenever you want a const pointer to the fixed size buffer (unless the buffer itself was declared const to begin with). You can find more info in this mail thread: link text

在我看来,这是一个严重的限制,这可能是人们通常不会在 C 中声明这样的指针的主要原因之一.另一个是大多数人甚至不知道您可以声明指针就像 AndreyT 指出的那样.

This is a severe constraint in my opinion and it could be one of the main reasons why people do not usually declare pointers like this in C. The other being the fact that most people do not even know that you can declare a pointer like this as AndreyT has pointed out.

这篇关于C 指针:指向固定大小的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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