将 b 声明为指向 int 数组 10 的指针的目的是什么

发布于 2024-12-12 16:37:22 字数 721 浏览 0 评论 0原文

看到我看到了一些这样的代码

int (*b)[10];

declare b as pointer to array 10 of int

所以我想问你这样做的目的是什么?

为什么我们不直接编写

int array[10];

并用作数组的传递地址。

两者相同还是有区别?

编辑: 不要与

int *b[10];  // this one declare b as array 10 of pointer to int

int (*b)[10];  // this one declare b as pointer to array 10 of int

再编辑

混淆,参见int (*b)[10];

1>将为int的10个元素和地址分配内存该内存将分配给 b ?

2> 不会为数组分配内存。这里说 b 能够保存 10 个元素的 int 数组的地址?

哪个选项是正确的? 如果有人是正确的,那么为什么应该使用这种复杂的语法而不是使用不同的方法?

see i have seen some code like

int (*b)[10];

which means

declare b as pointer to array 10 of int

so i want to ask you what is the purpose of doing this ?

why don't we write instead of that just

int array[10];

and use as passing address of array.

both are same or any difference ?

Edit :
Dont be confuse with

int *b[10];  // this one declare b as array 10 of pointer to int

and

int (*b)[10];  // this one declare b as pointer to array 10 of int

one more Edit

see int (*b)[10];

1>will allocate memory for 10 elements of int and the address of that memory will be assigned to b ?

or

2>there will be no memory allocated for array. here it says b is capable of holding address of and int array of 10 element ?

which option is right ?
if any one is right then why one should use this complex syntax rather then using different method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城泪 2024-12-19 16:37:22

其实是有区别的。在 int b[10] 中,b 是一个指针常量(这意味着它可以用来修改其底层数据,但不能更改为指向其他内容)。另一方面,int (*b)[10] 中的指针可以更改为指向其他内容(当然也可以更改其基础数据) )。所以区别在于 bint (*b)[10];您在此警告下一个看到您代码的开发人员,此b可能指向代码中的其他位置。这就是为什么 - 显然 - 不要这样做,除非你真的打算改变 b 所指向的内容(否则你只是会混淆谁会追随你) 。

关于你的问题,我使用sizeof(b)进行了检查。

int (*b)[10] ==> sizeof(b) 为 4(这意味着没有分配内存 - 选项 2)。

int b[10] ==> sizeof(b) 为 40(这意味着内存按预期分配 - 选项 1)。

看这个:

int (*b)[10];

int x[10];
b = &x;

这是编译的。将数组的大小更改为其他任何值。它不会编译!这意味着您非常正确:-D。选项 2 写得很完美:这个指针只能指向一个大小为 10 的数组,没有其他

[根据问题所有者的要求添加]

这样的语法有什么好处?这只是一个已经存在的功能,也许对某人有用。这就是我的理解方式(如果错误请纠正我):例如,你可以说: string (*names_of_players_in_soccer_team)[11]; 其优点是 - 显然 - 将数组限制为正好 11 个名称来处理应用程序逻辑 - 团队实际上必须正好有 11 个名称。这为那些阅读你的代码的人提供了更多的可读性,并强调了它的正确性......

There's a difference actually. In int b[10], b is a pointer constant (which means it can be used to modify its underlying data but it can't be changed to point to something else). The pointer in int (*b)[10] on the other hand can be changed to point to something else (as well as being able to change its underlying data of course). So the difference is that by saying that b is int (*b)[10]; you're hereby warning the next developer who sees your code that this b can point to something else somewhere in the code. That's why - obviously - don't do that except if you really intend to change what b is pointing to (otherwise you're just confusing who'll come after you).

Regarding your question, I checked using sizeof(b).

int (*b)[10] ==> sizeof(b) is 4 (which means no memory allocated - Option 2).

int b[10] ==> sizeof(b) is 40 (which means memory is allocated as expected - Option 1).

Look at this:

int (*b)[10];

int x[10];
b = &x;

This compiled. Change the size of the array to anything else. It'll not compile! This means that you're extremely correct :-D. Option 2 is written perfectly: This pointer can only point to an array of size 10 and nothing else.

[Added as per request of question owner]

What's the advantage of such a syntax? It's just a feature that's already there, and maybe it's useful for somebody. This is how I understand it (and please correct me if wrong): For example, you can say: string (*names_of_players_in_soccer_team)[11]; and the advantage is - obviously - restricting the array to be exactly 11 names to handle the application logic - the team must actually have exactly 11 names. This gives more readability for those who'll read your code, and emphasizes its correctness...

澜川若宁 2024-12-19 16:37:22

int (*b)[10]是一个指向int数组的指针,int b[10]一个int数组。 (下次可能会选择不同的名称?)也许您想要malloc数组存储,b = malloc(sizeof(*b))。为此,您需要一个指向数组类型的指针。

int (*b)[10] is a pointer to an array of int, int b[10] is an array of int. (Next time maybe choose different names?) Perhaps you want to malloc the array storage, b = malloc(sizeof(*b)). For this you would need a pointer to array type.

入怼 2024-12-19 16:37:22

C 允许使用不带下标的数组名称作为右值,表示数组基址的地址。它还允许在通常访问数组时使用偏移量隐式取消引用指针。因此,当且仅当 b 永远不需要指向不同的数组或同一数组的不同元素时,这些声明在逻辑和语法上都是等效的。从技术上讲,它们的不同之处在于 int b[10] 需要 sizeof(int)*10 字节来保存数组,而 int (*b)[10 ] 需要额外的 sizeof(int*) 字节来保存指向所述数组的指针。如果该数组是在函数外部声明的,则访问 int b[10] 的元素可能会使用更小的操作码和更少的寄存器。

我建议在您计划使用它时声明它。如果您不知道为什么它可能是指针,请将其声明为数组。如果需要在堆上动态分配、重新定位或声明为函数参数,则它应该是指针。

C allows the use of an array's name without a subscript as an rvalue meaning the address of the base of the array. It also allows a pointer to be implicitly dereferenced with an offset as an array is typically accessed. The declarations are therefore logically and syntactically equivalent if and only if b never needs to point to a different array or different element of the same array. Technically, they are dissimilar in that int b[10] requires sizeof(int)*10 bytes to hold the array, and int (*b)[10] requires an additional sizeof(int*) bytes to hold a pointer to said array. If the array is declared outside of a function, accessing elements of int b[10] may use a smaller opcode and one fewer registers.

I recommend declaring it as you plan to use it. If you don't know why it might be a pointer, declare it as an array. If it needs to be dynamically allocated on the heap, reseated, or is declared as a function parameter, it should be a pointer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文