使用C另一个结构灵活的内部结构长度数组 [英] flexible length struct array inside another struct using C

查看:133
本文介绍了使用C另一个结构灵活的内部结构长度数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我想用C来实现一个简单的结构:结果
2箱,每个包含不同数量的颗粒;粒子的确切数目传递在main()。

我写了下面code:

  typedef结构粒子{
    浮X;
    浮ÿ;
    浮VX;
    浮动VY;
}粒子;typedef结构{箱
    粒子P [];
}框;无效make_box(盒*中,诠释number_of_particles);诠释主(){
    盒B1,B2;
    make_box(安培; B1,5); //创建一个包含5粒一盒
    make_box(安培; B2,10); //创建一个包含10粒一盒
}

我试图实现与以下code make_box

 无效make_box(结构箱*中,诠释no_of_particles){
    粒子PO [no_of_particles]
    PO [0] .X = 1;
    PO [1] .X = 2;
    //等等等等...
    箱形指p =婆;
}

它总是给我无效使用灵活的数组成员。大大AP preciated,如果有人可以在此提供一些线索。


解决方案

 无效make_box(结构箱*中,诠释no_of_particles){
    粒子PO [no_of_particles]
    // ...
    箱形指p =婆;
}

PO 是一个局部变量,并为它的存储在堆栈中自动分配;返回它的地址是一个坏主意。而应该从堆中分配内存,记住释放内存当你用箱子完成的:

 的#include<&stdlib.h中GT;
#包括LT&;&stdio.h中GT;typedef结构Particle_ {
    浮X;
    浮ÿ;
    浮VX;
    浮动VY;
}粒子;typedef结构Box_ {
    粒子* P;
}箱;无效make_box(盒*中,诠释no_of_particles);无效make_box(盒*中,诠释no_of_particles){
    粒子* PO =(颗粒*)malloc的(no_of_particles *的sizeof(粒子));
    PO [0] .X = 1;
    PO [1] .Y = 2;
    //等等等等...
    箱形指p =婆;
}无效destroy_box(盒*盒){
    自由(箱形指p);
}
诠释主(){
    盒B1,B2;
    make_box(安培; B1,5); //创建一个包含5粒一盒
    make_box(安培; B2,10); //创建一个包含10粒一盒    //做的工作...
    的printf(框B1,点0,X:%5.2f \\ n,b1.p [0] .X);
    的printf(框B2,点1,Y:%5.2f \\ n,b2.p [1] .Y);    destroy_box(安培; B1);
    destroy_box(安培; B2);    返回0;
}

Hi I am trying to use C to implement a simple struct:
2 boxes, each contains different number of particles; the exact number of particles are passed in main().

I wrote the following code:

typedef struct Particle{
    float x;
    float y;
    float vx;
    float vy;
}Particle;

typedef struct Box{
    Particle p[];
}Box;

void make_box(Box *box, int number_of_particles);

int main(){
    Box b1, b2;
    make_box(&b1, 5);  //create a box containing 5 particles
    make_box(&b2, 10); //create a box containing 10 particles
}

I've tried to implement make_box with the following code

void make_box(struct Box *box, int no_of_particles){
    Particle po[no_of_particles];
    po[0].x = 1;
    po[1].x = 2;
    //so on and so forth...
    box->p = po;
}

It's always giving me "invalid use of flexible array member". Greatly appreciated if someone can shed some light on this.

解决方案

void make_box(struct Box *box, int no_of_particles){
    Particle po[no_of_particles];
    //...
    box->p = po;
}

po is a local variable, and the storage for it is allocated automatically on the stack; returning its address is a bad idea. You should instead allocate memory from the heap, and remember freeing that memory when you're done with the boxes:

#include <stdlib.h>
#include <stdio.h>

typedef struct Particle_ {
    float x;
    float y;
    float vx;
    float vy;
} Particle;

typedef struct Box_ {
    Particle *p;
} Box;

void make_box(Box *box, int no_of_particles);

void make_box(Box *box, int no_of_particles){
    Particle *po = (Particle *) malloc ( no_of_particles*sizeof(Particle) );
    po[0].x = 1;
    po[1].y = 2;
    //so on and so forth...
    box->p = po;
}

void destroy_box(Box *box){
    free(box->p);
}


int main(){
    Box b1, b2;
    make_box(&b1, 5);  //create a box containing 5 particles
    make_box(&b2, 10); //create a box containing 10 particles

    // do the job...
    printf("box b1, point 0, x: %5.2f\n", b1.p[0].x);
    printf("box b2, point 1, y: %5.2f\n", b2.p[1].y);

    destroy_box(&b1);
    destroy_box(&b2);

    return 0;
}

这篇关于使用C另一个结构灵活的内部结构长度数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆