gpt4 book ai didi

arrays - C程序语法

转载 作者:行者123 更新时间:2023-12-04 00:51:42 26 4
gpt4 key购买 nike

我正在处理一个旧的 C 代码库,它有一些我从未见过的奇怪语法。

char (*__kaboom)[NUM_ELEMS( Port ) ] = NULL;

这是如何运作的? __kaboom 的作用是什么?我们如何使用它?比如在打印语句中?

还有其他类似的地方

char (*name)[MAX] = NULL;
name = (char (*)[MAX]) malloc(MAXB*sizeof(char));

我是 C 编程的新手,非常感谢您的建议。谢谢

最佳答案

很可能,NUM_ELEMS(Port) 是一个扩展为 (sizeof(Port)/sizeof(Port[0])) 或类似内容的宏。如果是这样,它只有在参数是源文件中定义的数组名称(未声明 extern 并且在另一个文件中定义)不是函数参数时才能正常工作。不管它是什么,它都必须扩展为一个整数常量,或者什么都没有。

符号:

char (*variable)[SIZE] = NULL;

正在声明一个指向给定大小和类型的数组的指针,并将该指针设置为 NULL

第二个片段中的定​​义和转换在概念上是相似的。

名称__kaboom 保留供实现使用。请注意,通常情况下,您不应创建以下划线开头的函数、变量、标记或宏名称。 C11 §7.1.3 Reserved identifiers的一部分说:

  • All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
  • All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

另见 What does double underscore (__const) mean in C?

关于arrays - C程序语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65833713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com