For faster navigation, this Iframe is preloading the Wikiwand page for 灵活数组类型.

灵活数组类型

此條目包含指南或教學內容。 (2017年4月1日)請藉由移除或重寫指南段落來改善條目,或在討論頁提出討論。

灵活数组类型[1]C99引入的语言特性。[2]即在struct数据类型的最后一个数据成员,可以为一个未指明长度的数组类型。例如:

struct double_vector_st {
    size_t length;
    double array[]; // the flexible array member should be last
};

sizeof运算符作用于这个struct,返回灵活数组成员的偏移量。在堆上分配这种struct,应该保留灵活数组的空间。如下例:

struct double_vector_st *allocate_double_vector(size_t len) {
   struct double_vector_st *vec = malloc(sizeof *vec + len * sizeof vec->array[0]);

   if (!vec) {
       perror("malloc double_vector_st failed");
       exit(EXIT_FAILURE);
   }

   vec->length = len;

   for (size_t ix = 0; ix < len; ix++)
       vec->array[ix] = 0.0;

   return vec;
}

C++语言标准尚未支持灵活数组类型。但Visual C++2015支持。

参考文献

[编辑]
  1. ^ Lesser known C features. [2014-12-30]. (原始内容存档于2014-12-23). 
  2. ^ http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf页面存档备份,存于互联网档案馆) section §6.7.2.1, item 16, page 103
{{bottomLinkPreText}} {{bottomLinkText}}
灵活数组类型
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?