内存分配

发布于 2024-11-10 12:13:04 字数 144 浏览 2 评论 0原文

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

如何为结构体中的数字分配内存?

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

How can one allocate memory for numbers in the struct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷到爆炸 2024-11-17 12:13:05

如果您想在结构体的第一个元素中为一个浮点数分配空间,您可以这样做:

#include <stdlib.h>

/* ... */

val[0].numbers = malloc(1 * sizeof(float)); /* "1 * " for clarity... */

如果这就是您的意思。

If you want to allocate the space for one float in the first element of your struct, you would do it like this:

#include <stdlib.h>

/* ... */

val[0].numbers = malloc(1 * sizeof(float)); /* "1 * " for clarity... */

If that's what you meant.

任谁 2024-11-17 12:13:05

因此,您可以在不使用动态内存分配的情况下执行此操作,如下所示:

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;
 }

但我想不出您想要一个仅具有指向一个浮点的指针的结构的任何原因。

根据名称 'numbers',我想说您希望 'numbers' 指向 floats 数组,如果是这样,您可以这样做:

#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;
 }

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;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文