[c/c++]柔性数组

/ 26,340评论 / 108555阅读 / 0点赞

柔性数组

在C99标准之后,在结构体中最后一个元素允许是未知大小的,称之为柔性数组

struct CoolArr_s{
	int len;
	char arr[];
};
#include "stdio.h"
#include "stdlib.h"

struct CoolArr_s
{
        int len;
        char arr[];
};

int main()
{
        //这里给arr的长度为10个char的大小
        struct CoolArr_s* p = (struct CoolArr_s*)malloc(sizeof(int) + sizeof(char) * 10);
        for (int i = 10; i--;)
        {
                (p->arr)[i] = i + '0';
                printf("%d " ,(p->arr)[i]);
        }
        printf("\n CoolArr_s: %d, *p: %d\n", sizeof(struct CoolArr_s), sizeof(*p));
        return 0;
}

在C++中的柔性数组

注意:在c++中并不建议使用柔性数组,而应考虑使用array代替。即使它可以使用在struct和class中,但部分编译器是不支持这种操作的。

#include <iostream>
#include <array>
using namespace std;

int main()
{
        //数组长度
	const int len_arr = 10;
        //定义数组
	array<int, len_arr> arr;
        //array::data()返回数组的首地址
        //array::begin()返回的是迭代器iterator
	int *arr_p = arr.data();
	for (int i = len_arr; i--;)
	{
		arr_p[i] = i;
		cout << arr[i] << " ";
	}
	cout << endl;
	return 0;
}
  1. Scottfrurn说道:

    mexico drug stores pharmacies medicine in mexico pharmacies medicine in mexico pharmacies

  2. RaymondLiata说道:

    mexican pharmacy mexican border pharmacies shipping to usa pharmacies in mexico that ship to usa

  3. Scottfrurn说道:

    best online pharmacies in mexico medicine in mexico pharmacies medicine in mexico pharmacies

  4. RaymondLiata说道:

    medicine in mexico pharmacies mexico drug stores pharmacies mexican pharmaceuticals online

  5. Coreynuh说道:

    http://mexicanpharmacy.cheap/# mexican online pharmacies prescription drugs

  6. Coreynuh说道:

    http://mexicanpharmacy.cheap/# mexican border pharmacies shipping to usa

  7. Normancah说道:

    buying from online mexican pharmacy buying from online mexican pharmacy mexico pharmacy

  8. RaymondLiata说道:

    mexico pharmacy mexican pharmaceuticals online medicine in mexico pharmacies

  9. Scottfrurn说道:

    mexican mail order pharmacies buying from online mexican pharmacy mexico pharmacies prescription drugs

  10. Michaelmub说道:

    http://mexicanpharmacy.cheap/# buying from online mexican pharmacy

  11. ManuelNut说道:

    gГјnstige online apotheke: kamagra tabletten – online apotheke preisvergleich

  12. StevenGat说道:

    http://kamagrakaufen.top/# internet apotheke

  13. Frankencut说道:

    https://viagrakaufen.store/# Viagra online bestellen Schweiz

  14. RaymondTek说道:

    online apotheke preisvergleich kamagra jelly kaufen deutschland online apotheke versandkostenfrei

  15. Frankencut说道:

    https://kamagrakaufen.top/# internet apotheke

  16. RaymondTek说道:

    gГјnstige online apotheke online apotheke gunstig online-apotheken

  17. StevenGat说道:

    http://viagrakaufen.store/# Viagra Alternative rezeptfrei

  18. ManuelNut说道:

    online apotheke deutschland: kamagra oral jelly kaufen – п»їonline apotheke

  19. StevenGat说道:

    http://potenzmittel.men/# gГјnstige online apotheke

  20. RaymondTek说道:

    internet apotheke versandapotheke deutschland online apotheke preisvergleich

  21. Frankencut说道:

    http://kamagrakaufen.top/# online apotheke deutschland

  22. ManuelNut说道:

    Viagra online bestellen Schweiz: viagra bestellen – Viagra Alternative rezeptfrei

  23. Frankencut说道:

    https://kamagrakaufen.top/# internet apotheke

  24. RaymondTek说道:

    п»їonline apotheke cialis rezeptfreie kaufen online apotheke deutschland

  25. StevenGat说道:

    https://kamagrakaufen.top/# gГјnstige online apotheke

  26. RaymondTek说道:

    online apotheke preisvergleich п»їonline apotheke versandapotheke versandkostenfrei

  27. StevenGat说道:

    https://kamagrakaufen.top/# online apotheke preisvergleich

  28. ManuelNut说道:

    online apotheke versandkostenfrei: kamagra oral jelly kaufen – versandapotheke versandkostenfrei

  29. RaymondTek说道:

    online-apotheken Online Apotheke Deutschland online apotheke preisvergleich

  30. RaymondTek说道:

    versandapotheke versandkostenfrei п»їonline apotheke versandapotheke versandkostenfrei

  31. StevenGat说道:

    http://potenzmittel.men/# online apotheke deutschland

  32. StevenGat说道:

    https://kamagrakaufen.top/# online apotheke gГјnstig

  33. Frankencut说道:

    https://cialiskaufen.pro/# internet apotheke

  34. Deeleanny说道:

    I did HCG throughout cycle and according to the girl my vomiting dolphine was bigger, I was on 1000 a week, but it s smaller than ever now during PCT, so I bet it was just some extra bloodflow propecia cost When people consume unusual diets, e

  35. Frankencut说道:

    https://potenzmittel.men/# versandapotheke versandkostenfrei

  36. RaymondTek说道:

    gГјnstige online apotheke potenzmittel online apotheke versandkostenfrei

  37. RaymondTek说道:

    gГјnstige online apotheke internet apotheke versandapotheke versandkostenfrei

  38. StevenGat说道:

    https://potenzmittel.men/# online apotheke preisvergleich

  39. StevenGat说道:

    https://apotheke.company/# online apotheke gГјnstig

  40. Frankencut说道:

    https://potenzmittel.men/# online apotheke deutschland

  41. RaymondTek说道:

    versandapotheke versandkostenfrei potenzmittel kaufen gГјnstige online apotheke

  42. Frankencut说道:

    http://kamagrakaufen.top/# п»їonline apotheke

  43. KevinWon说道:

    pharmacie ouverte 24/24 Levitra 20mg prix en pharmacie Pharmacie en ligne livraison gratuite

  44. LarryBom说道:

    https://cialissansordonnance.pro/# acheter medicament a l etranger sans ordonnance

  45. LarryBom说道:

    http://viagrasansordonnance.store/# Sildénafil Teva 100 mg acheter

  46. MarioRip说道:

    farmacia online madrid: vardenafilo – farmacia barata

  47. LarryBom说道:

    https://cialissansordonnance.pro/# pharmacie en ligne

  48. Eliseokab说道:

    Meilleur Viagra sans ordonnance 24h: Meilleur Viagra sans ordonnance 24h – Quand une femme prend du Viagra homme

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注