Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 668099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:00:10+00:00 2026-05-14T00:00:10+00:00

Hi I am trying to use C to implement a simple struct: 2 boxes,

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:00:11+00:00Added an answer on May 14, 2026 at 12:00 am
    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use a 3d texture in opengl to implement volume rendering. Each
I am trying to implement web services that use different security mechanisms to do
I was trying to use a struct to parse socket data when implement a
I'm trying to implement a simple tagging system (messing around in php)... I use
I am trying to implement a simple merge, without the use of anything pre-defined.
I am trying to implement simple algorithm with use of C#'s Dictionary : My
I am trying to use llvm to implement a simple, dumb c-like language. And
I'm trying to use $.sortable to implement a simple puzzle: <html> <head> <link href=http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css
I am trying to use django-social-auth to implement google openid login into my app,
I am trying to use the Soundcloud API to implement an integrated export function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.