malloc ing array's or structures

This is a discussion on malloc ing array's or structures within the C Programming forums, part of the General Programming Boards category; I have a situation where I need to store a series of objects (they're actually 3d objects, so each structure ...

  1. #1
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53

    malloc ing array's or structures

    I have a situation where I need to store a series of objects (they're actually 3d objects, so each structure will have an array of points/faces plus position/rotation info inside)

    however I don't know how many objects there are, so I need to use malloc, And I'm getting really confused over what the syntax should be (or if I have the method totally wrong)

    struct object
    {
    int *q;
    };

    //this curently is just a placeholder while I get the rest of it working, the pointer q just points to an array of floats.

    struct object *p;

    p=malloc(16*sizeof(struct object));

    that is how I would expect to allocate an array of 16 structs to pointer p, however when I try to access it as an array, I get stuck

    I think it should be something like

    p[n]->q=malloc (16*sizeof(float));

    to allocate an array of 16 floats to the pointer q in the nth structure of the array pointed to by p.

    this just gives invalid argument of type "->" error. though I can't find any info on what I'm doing wrong (this is about then tenth attempt to get something working, there's probably something silly I'm missing, but I'm still a beginner so go easy on me.....

    thanks
    -mark
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>p[n]->q=malloc (16*sizeof(float));
    It should be
    Code:
    p[n].q = malloc(16*sizeof(float));
    since you're malloc'ing complete structures instead of pointers to structures :-)
    *Cela*

  3. #3
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53
    ahhhhhh thankyou it works now...


    could you explain one little thing.... I thought malloc returned a pointer to the memory, so why am I now accessing it this way.

    and more importantly, why did the method I tried fail :S. earlier before I realised I'd need multiple structs, I used just had

    struct object *p;
    p->q=malloc (stuff);

    and that worked fine

    but in both cases, p is defined as a pointer..... erm

    sorry for pestering

    thanks
    -mark
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >> thought malloc returned a pointer to the memory, so why am I now accessing it this way
    You only have one pointer, that's to the beginning of the memory. Imagine that you used an array instead
    Code:
    struct object p[16];
    You'd access it with the . operator because each of those 16 objects is a struct, not a pointer to a struct. It would be completely different if you allocated the memory like this
    Code:
    struct object **p;
    p=malloc(16*sizeof(struct object *));
    Then you would have to allocate memory for each object because they're only pointers, but you would then access them with the -> operator.
    Code:
    struct object *p;
    ..
    p->q=malloc (stuff);
    That's like saying
    Code:
    struct object *p;
    ..
    p[0].q=malloc (stuff);
    *Cela*

  5. #5
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53
    thanks for clearing that up

    -mark
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 05:52 PM
  2. Confused about malloc and arrays
    By mc61 in forum C Programming
    Replies: 5
    Last Post: 01-15-2008, 06:22 PM
  3. Large arrays, malloc, and strcmp
    By k2712 in forum C Programming
    Replies: 1
    Last Post: 09-24-2007, 09:22 PM
  4. structures and arrays
    By cnewbee in forum C Programming
    Replies: 1
    Last Post: 04-01-2003, 01:01 PM
  5. returning arrays of structures
    By manolo21 in forum C Programming
    Replies: 2
    Last Post: 03-31-2003, 08:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21