in c language, what is the use of this formula after the colon:(n)&(n-1)
时间: 2024-05-21 16:10:42 浏览: 10
In C language, the formula after the colon (n) is used to specify the size of an array in a structure. It is called a member array or a flexible array member. This allows the size of the array to be determined at runtime instead of compile-time.
For example, consider the following structure definition:
struct MyStruct {
int id;
char name[];
};
Here, the "name" member is an array of characters with no fixed size. It is defined using the formula "[]" after the member name. To specify the size of this array at runtime, we can use the formula "(n)" after the colon, like this:
struct MyStruct {
int id;
char name[];
} *p;
p = malloc(sizeof(struct MyStruct) + n);
Here, we allocate memory for the structure and the array using malloc(). The size of the array is determined by adding "n" to the size of the structure. This allows us to create structures with variable-length arrays, which can be useful in certain situations where the size of the data is not known in advance.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)