[c/c++]柔性数组

/ 32,555评论 / 128137阅读 / 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. Vernonnance说道:

    generic lisinopril online: prescription for lisinopril – zestril tab 10mg

  2. Charlesbaics说道:

    doxycycline mexico Buy doxycycline for chlamydia doxycycline 100mg acne

  3. Scottbut说道:

    cheapest 40 mg doxycycline: Buy doxycycline for chlamydia – doxycycline brand in india

  4. Scottbut说道:

    ciprofloxacin generic: cipro pharmacy – buy cipro online without prescription

  5. Charlesbaics说道:

    zithromax antibiotic zithromax z-pak zithromax order online uk

  6. Jamesweafe说道:

    http://azithromycin.bar/# buy cheap zithromax online

  7. Charlesbaics说道:

    lisinopril coupon buy lisinopril online how to buy lisinopril online

  8. BOAT TRAILERS说道:

    Great blog here! Also your website loads up fast! What web
    host are you using? Can I get your affiliate link to your host?
    I wish my web site loaded up as fast as yours lol

  9. Scottbut说道:

    lisinopril 20 mg prices: lisinopril 10 mg 12.5mg – lisinopril prinivil zestril

  10. Jamesweafe说道:

    https://azithromycin.bar/# where can i get zithromax over the counter

  11. Scottbut说道:

    2 prinivil: buy lisinopril – lisinopril 20mg for sale

  12. Charlesbaics说道:

    ciprofloxacin over the counter buy ciprofloxacin online ciprofloxacin generic price

  13. Vernonnance说道:

    zithromax price south africa: zithromax for sale 500 mg – zithromax over the counter canada

  14. Vernonnance说道:

    zithromax over the counter canada: zithromax antibiotic – purchase zithromax online

  15. Jamesweafe说道:

    http://lisinopril.auction/# lisinopril 20 mg canada

  16. Scottbut说道:

    25 mg lisinopril: prescription for lisinopril – zestril 10 mg in india

  17. Charlesbaics说道:

    where to buy lisinopril Buy Lisinopril 20 mg online prinivil 25mg

  18. Scottbut说道:

    doxycycline price comparison: doxycycline buy online – where can i get doxycycline uk

  19. Jamesweafe说道:

    http://ciprofloxacin.men/# buy cipro without rx

  20. Charlesbaics说道:

    cipro for sale Get cheapest Ciprofloxacin online buy generic ciprofloxacin

  21. Jamesweafe说道:

    http://doxycycline.forum/# doxycycline buy canada

  22. Scottbut说道:

    how much is doxycycline cost: Buy doxycycline for chlamydia – doxycycline monohydrate

  23. Vernonnance说道:

    lisinopril 10mg tabs: Over the counter lisinopril – lisinopril 5 mg over the counter

  24. Scottbut说道:

    ciprofloxacin generic: Buy ciprofloxacin 500 mg online – buy generic ciprofloxacin

  25. Charlesbaics说道:

    lisinopril 5 mg brand name buy lisinopril prinivil drug cost

  26. Jamesweafe说道:

    http://amoxicillin.best/# where to get amoxicillin over the counter

  27. Vernonnance说道:

    cheapest doxycycline online: Buy doxycycline hyclate – 100mg doxycycline

  28. Charlesbaics说道:

    zithromax 500 mg lowest price online zithromax antibiotic where can i get zithromax over the counter

  29. WilliamTut说道:

    https://tadalafil.trade/# buy generic tadalafil 20mg

  30. Juliuselinc说道:

    https://tadalafil.trade/# tadalafil coupon

  31. Timothykew说道:

    buy Levitra over the counter: Levitra online pharmacy – Cheap Levitra online

  32. WilliamTut说道:

    https://tadalafil.trade/# buy tadalafil india

  33. RichardMus说道:

    Levitra 20 mg for sale Generic Levitra 20mg Vardenafil price

  34. JosephBut说道:

    п»їkamagra: Kamagra Oral Jelly – Kamagra tablets

  35. RichardMus说道:

    tadalafil tablets 20 mg buy tadalafil 2.5 mg generic buy tadalafil 5mg online

  36. Juliuselinc说道:

    https://edpills.monster/# top rated ed pills

  37. brCEon说道:

    Most sodium is consumed in the form of sodium chloride salt levitra professional sale The work by Candrilli et al

  38. Timothykew说道:

    Buy Vardenafil 20mg online: Levitra online pharmacy – Levitra 10 mg best price

  39. JosephBut说道:

    tadalafil cheap: best tadalafil generic – tadalafil generic us

  40. Juliuselinc说道:

    https://kamagra.team/# sildenafil oral jelly 100mg kamagra

  41. Timothykew说道:

    Cheap Levitra online: Buy Vardenafil online – Levitra online pharmacy

  42. RichardMus说道:

    Kamagra tablets super kamagra п»їkamagra

  43. RichardMus说道:

    Kamagra tablets buy Kamagra Kamagra 100mg

  44. Juliuselinc说道:

    https://edpills.monster/# cheap erectile dysfunction pill

  45. Timothykew说道:

    sildenafil prices in india: 20 mg sildenafil daily – viagra sildenafil 100mg

  46. BrettDit说道:

    https://medium.com/@Evangeline50393/ubuntu-vps-с-выделенным-ip-и-ssl-сертификатом-24bb90c42603
    VPS SERVER
    Высокоскоростной доступ в Интернет: до 1000 Мбит/с
    Скорость подключения к Интернету — еще один важный фактор для успеха вашего проекта. Наши VPS/VDS-серверы, адаптированные как под Windows, так и под Linux, обеспечивают доступ в Интернет со скоростью до 1000 Мбит/с, что гарантирует быструю загрузку веб-страниц и высокую производительность онлайн-приложений на обеих операционных системах.

  47. WilliamTut说道:

    https://edpills.monster/# natural ed remedies

  48. Juliuselinc说道:

    http://tadalafil.trade/# tadalafil online 10mg

  49. JosephBut说道:

    Levitra online pharmacy: Vardenafil price – buy Levitra over the counter

发表回复

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