1

I want to get work this code and I googled and asked in efnet and freenode but I didn't find the answer.

What I want is to assign a struct woot to an another bidimensional struct woot *, and I need malloc to do that.

Then, how can I use malloc there and how to assign the struct? 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];
}
Toby
  • 9,696
  • 16
  • 68
  • 132
argos.void
  • 11
  • 2
  • 1
    In general, it's not a good idea to cast the return value of `malloc` in C (doesn't apply to C++). – Mehrdad Afshari Dec 24 '10 at 01:11
  • then http://cseweb.ucsd.edu/~j2lau/cs5a/week8.html ... – argos.void Dec 24 '10 at 01:16
  • 1
    We have a post on this specific topic on Stack Overflow: http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc Unless your code is supposed to get compiled as C++ code, this is frowned upon. In C, `void*` is implicitly converted to any pointer type. You may want to show this link to the instructor who wrote that page. – Mehrdad Afshari Dec 24 '10 at 01:18
  • i have a very basic C, and this code is for C. I really dont know what to do – argos.void Dec 24 '10 at 01:26
  • Work on your choice of identifiers! – bitmask Jan 11 '12 at 14:38

2 Answers2

0
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]; should be enough.
  2. Typecasting the returned void* pointer when calling malloc() is unneeded in C (and considered as bad form).

(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!
}
FDFlock
  • 73
  • 1
  • 6
0

You're trying to initialize an array with a scalar value (the pointer returned by 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:

//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 woots themselves:

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

brianmearns
  • 9,581
  • 10
  • 52
  • 79