[c/c++]柔性数组

/ 29,228评论 / 117939阅读 / 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. RickyAlage说道:

    canadian pharmacy price checker Large Selection of Medications from Canada canadian discount pharmacy

  2. RickyAlage说道:

    reputable mexican pharmacies online cheapest mexico drugs best online pharmacies in mexico

  3. Dannytut说道:

    Do you like crash games? Play now the online casino crash game Maverick in bwin Zambia website https://bacboinzambia94938.jaiblogs.com/53400067/the-best-side-of-black-jack-in-zambia

  4. Dennisjuiff说道:

    контроллер движения с ethercat – устройство плавного пуска электродвигателя инноверт, купить термодат

  5. Marcelcrolf说道:

    https://mexicoph24.life/# reputable mexican pharmacies online

  6. RickyAlage说道:

    world pharmacy india indian pharmacy fast delivery indianpharmacy com

  7. RickyAlage说道:

    legitimate canadian online pharmacies canadian pharmacy ltd canadian pharmacy world

  8. Michaelsew说道:

    http://canadaph24.pro/# canadian pharmacy ed medications

  9. Michaelsew说道:

    https://mexicoph24.life/# mexican border pharmacies shipping to usa

  10. Jacobacupt说道:

    Бонус приветствия в Betwinner предоставляет игрокам возможность получить дополнительные средства на свой игровой счет. Этот бонус составляет 100% от суммы первого депозита, который игрок вносит на свой счет. Например, если новый клиент пополнил свой счет на 10 евро, то ему будет начислено еще 10 евро в качестве бонуса.
    https://vsiknygy.net.ua/wp-content/pages/betwinner_promokod_2020.html

  11. MeganLiard说道:

    top casino best
    top live casino

  12. Marcelcrolf说道:

    http://mexicoph24.life/# medicine in mexico pharmacies

  13. Michaelsew说道:

    https://canadaph24.pro/# canadian drug stores

  14. RickyAlage说道:

    medication from mexico pharmacy Mexican Pharmacy Online mexico drug stores pharmacies

  15. RickyAlage说道:

    purple pharmacy mexico price list mexico pharmacy mexican mail order pharmacies

  16. Marcelcrolf说道:

    http://canadaph24.pro/# canada drugs reviews

  17. RickyAlage说道:

    best rated canadian pharmacy Prescription Drugs from Canada canada drugs online

  18. RickyAlage说道:

    п»їbest mexican online pharmacies buying prescription drugs in mexico mexico pharmacy

  19. Michaelsew说道:

    http://mexicoph24.life/# mexico drug stores pharmacies

  20. Michaelsew说道:

    https://indiaph24.store/# world pharmacy india

  21. Michaelsew说道:

    https://indiaph24.store/# indian pharmacy paypal

  22. Marcelcrolf说道:

    http://indiaph24.store/# cheapest online pharmacy india

  23. StevenSen说道:

    mexican drugstore online: cheapest mexico drugs – mexican mail order pharmacies

  24. GlennPhync说道:

    Игорный бизнес в онлайн мире, словно многоликая галактика, предлагает множество вариантов для погружения в атмосферу азарта и развлечений. Некоторые вступают в это увлекательное путешествие в поисках удачи, другие – для того, чтобы отдохнуть от рутины и испытать незабываемые эмоции http://www.pawsarl.es/blog/%D1%80%D0%B5%D0%B9%D0%BD%D0%B5.html

  25. RickyAlage说道:

    best canadian online pharmacy Prescription Drugs from Canada canadian discount pharmacy

  26. Marcelcrolf说道:

    http://mexicoph24.life/# buying prescription drugs in mexico

  27. RickyAlage说道:

    canadian medications Certified Canadian Pharmacies canada drugstore pharmacy rx

  28. RickyAlage说道:

    medicine in mexico pharmacies mexico pharmacy best online pharmacies in mexico

  29. Michaelsew说道:

    http://mexicoph24.life/# mexican mail order pharmacies

  30. Michaelsew说道:

    http://indiaph24.store/# indianpharmacy com

  31. Marcelcrolf说道:

    https://canadaph24.pro/# thecanadianpharmacy

  32. Michaelsew说道:

    https://canadaph24.pro/# canada rx pharmacy

  33. RickyAlage说道:

    best canadian online pharmacy Licensed Canadian Pharmacy canadian compounding pharmacy

  34. RickyAlage说道:

    buying from online mexican pharmacy Online Pharmacies in Mexico mexican online pharmacies prescription drugs

  35. Marcelcrolf说道:

    https://mexicoph24.life/# medicine in mexico pharmacies

  36. RickyAlage说道:

    medicine in mexico pharmacies mexican pharmacy mexico drug stores pharmacies

  37. StevenSen说道:

    mexican pharmaceuticals online: mexican pharmacy – mexico drug stores pharmacies

  38. RickyAlage说道:

    canadian pharmacy service Certified Canadian Pharmacies best canadian online pharmacy

  39. Marcelcrolf说道:

    https://mexicoph24.life/# reputable mexican pharmacies online

  40. RickyAlage说道:

    mexico drug stores pharmacies medication from mexico pharmacy mexican pharmacy

  41. Michaelsew说道:

    http://mexicoph24.life/# mexico pharmacies prescription drugs

  42. RickyAlage说道:

    pharmacies in mexico that ship to usa mexican pharmacy medication from mexico pharmacy

  43. Michaelsew说道:

    https://indiaph24.store/# indian pharmacy paypal

  44. Marcelcrolf说道:

    https://indiaph24.store/# reputable indian pharmacies

  45. RickyAlage说道:

    canadian pharmacy drugs online Licensed Canadian Pharmacy reputable canadian pharmacy

  46. RickyAlage说道:

    mexican rx online cheapest mexico drugs mexico drug stores pharmacies

  47. StevenKnone说道:

    ddos сервис – пентест сайта, взлом сайта

发表回复

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