简体   繁体   English

在C中结构到二维结构指针的分配

[英]Struct to bidimensional struct pointer assignment in C

I want to get work this code and I googled and asked in efnet and freenode but I didn't find the answer. 我想用这段代码工作,我在efnet和freenode中搜索并询问,但没有找到答案。

What I want is to assign a struct woot to an another bidimensional struct woot * , and I need malloc to do that. 我要的是一个struct分配woot到另一个二维结构woot * ,我需要malloc做到这一点。

Then, how can I use malloc there and how to assign the struct? 然后,如何在其中使用malloc以及如何分配结构? Thanks. 谢谢。

#include <stdio.h>
struct omg {
    int foo;
};
struct woot {
    struct omg *localfoo;
    int foo;
};
int a = sizeof(struct woot);
int main(void){
    struct woot *what[10][10] = (struct woot *) malloc(100*a);
    struct omg hahaha[100];
    hahaha[1].foo = 15;
    what[1][6].localfoo = &hahaha[1];
}
struct woot *what[10][10] = (struct woot *) malloc(100*a);

I'm curious, does this code even compile? 我很好奇,这个代码甚至可以编译吗? ( edit: no, it doesn't.) In any case: 编辑:不,不是。)无论如何:

  1. You don't really need malloc() here, declaring struct woot *what[10][10]; 您实际上不需要在这里malloc(),声明struct woot *what[10][10]; should be enough. 应该足够了。
  2. Typecasting the returned void* pointer when calling malloc() is unneeded in C (and considered as bad form). C中不需要调用malloc()时,类型转换返回的void *指针(这被认为是错误的形式)。

(Not really an answer, I know... I would post it as a simple comment but I don't have enough points yet.) (我知道这不是一个真正的答案。我会以简单的评论发表,但我的观点还不够。)

edit: Oops, others have pointed out the same while I was writing this post. 编辑:糟糕,其他人在我撰写本文时也指出了同样的情况。

new edit: Here is a better version of your code, with some mistakes corrected: 新编辑:这是您代码的更好版本,但已纠正了一些错误:

#include <stdio.h>
#include <stdlib.h> // needed for malloc()

struct omg {
    int foo;
};

struct woot {
    struct omg *localfoo;
    int foo;
};

int main(void){
    const int a = sizeof(struct woot); /* there is no reason "a" should be global...
                                          actually, "a" is not needed at all, and, even if it
                                          were needed, it should be declared as "const" :) */
    struct woot *what[10][10];
    struct omg hahaha[100];
    hahaha[1].foo = 15;
    what[1][6]->localfoo = &hahaha[1];
    what[7][2] = malloc(a); // I would write "malloc(sizeof(struct woot))"

    return 0;   // main() should return an int, as declared!
}

You're trying to initialize an array with a scalar value (the pointer returned by malloc). 您正在尝试使用标量值(由malloc返回的指针)初始化数组。 If you really want a 10 by 10 matrix of pointers to structs (and not a 10 by 10 matrix of structs), you don't need malloc: 如果您确实想要结构的10到10的指针矩阵(而不是结构的10到10的指针矩阵),则不需要malloc:

//Statically allocated 10x10 matrix of pointers, no need for malloc.
struct woot *what[10][10];

To assign a pointer to a cell in that matrix: 要将指针分配给该矩阵中的单元格:

struct woot oneSpecificWoot;
what[1][2] = &oneSpecificWoot;

If this is really, really what you want, you could then create a bunch of woots dynamically an populate it. 如果这确实是您真正想要的,则可以动态创建一堆乌鸦以填充它。 Something like this: 像这样:

int i, j;
for(i=0; i<10; i++) {
    for(j=0; j<10; j++) {
        what[i][j] = malloc(sizeof(struct woot));
        //Of course, you should always test the return value of malloc to make sure
        // it's not NULL.
    }
 }

But if you're going to do that, you might as well just statically allocate the woot s themselves: 但是,如果要执行此操作,则不妨自行静态分配woot

//A 10x10 matrix of woots, no malloc required.
struct woot what[10][10];

The first case (a 2-D array of pointers) would be more likely if the woots are being created somewhere else, and you just want references to them in a grid lay out, or possibly if you don't know the dimensions of the grid at compile time. 如果在其他地方创建了花线,并且您只想在网格中布置对它们的引用,或者如果您不知道该花线的尺寸,则第一种情况(二维指针数组)更有可能网格在编译时。 But in your code, you're using malloc to create a fixed number of them, so you might as well just have the compiler allocate them statically. 但是在您的代码中,您正在使用malloc创建固定数量的它们,因此您最好让编译器静态地分配它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM