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

  • Home
  • SEARCH
  • 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 5971945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:35:19+00:00 2026-05-22T20:35:19+00:00

typedef struct { float *numbers; float val1; float val2; } Values; Values val[16]; How

  • 0
typedef struct {
    float *numbers;
    float val1;
    float val2;
} Values; 
Values val[16];

How can one allocate memory for numbers in the struct?

  • 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-22T20:35:20+00:00Added an answer on May 22, 2026 at 8:35 pm

    So, you could do this without using dynamic memory allocation like this:

    typedef struct {
        float *numbers;
        float val1;
        float val2;
    } Values; 
    
    #define MAX_SIZE 16
    
    int main() {
        Values val[MAX_SIZE];
        float myfloats[MAX_SIZE];
        int i;
        for(i=0;i<MAX_SIZE;i++) {
            val[i].numbers=&myfloats[i];
        }
        return 0;
     }
    

    But I can’t think of any reason why you’d want a structure with a pointer to just one float.

    Based on the name 'numbers', I’d say you want 'numbers' to point to an array of floats, if so, you could do this:

    #include <malloc.h>
    
    typedef struct {
        float *numbers;
        float val1;
        float val2;
    } Values; 
    
    #define MAX_SIZE 16
    
    int main() {
        Values val[MAX_SIZE];
        size_t numberOfFloats = 10;
        int i;
    
        // for each of the members of the val array
        for(i=0;i<MAX_SIZE;i++) {
            // allocate using calloc (this will set all of the floats to 0.0)
            val[i].numbers=calloc(numberOfFloats,sizeof(float));
    
            // check the allocation worked...
            if(!val[i].numbers) {
                // insert proper error handling here.
                printf("oops\n");
                return -1;
            }
    
        }
    
        // you access the variables like this
        for(i=0;i<MAX_SIZE;i++) {
            int number;
            for(number=0; number < numberOfFloats; number++) {
                printf("Value %d, Number %d = %f\n",i,number,val[i].numbers[number]);
            }
        }
    
        // don't forget to play nice and clean up afterwards
        for(i=0;i<MAX_SIZE;i++) {
            free(val[i].numbers);
        }
        return 0;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so I have struct like this typedef struct { float x; float y;
template struct A{ typedef float atype; typedef typename tB::btype typeB; }; template struct B{
I'm making a vector/matrix library. (GCC, ARM NEON, iPhone) typedef struct{ float v[4]; }
This is my code. struct Vector { float x, y, z, w; }; typedef
I have two vector classes: typedef struct D3DXVECTOR3 { FLOAT x; FLOAT y; FLOAT
I have a matrix struct: typedef struct Matrix { float m[16]; } Matrix; When
You can see what I'm trying to do below: typedef struct image_bounds { int
I'm trying to do this: typedef struct { float x; float y; } coords;
If I have the following: typedef struct _MY_STRUCT { int a; float b; }
Having problems with the output of this formula: typedef struct { float t, Vx,

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.