Thread: Struct array malloc and realloc

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    10

    Question Struct array malloc and realloc

    I'm trying to get a struct to malloc (one time its size) this is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_STRING 128
    
    typedef char String[MAX_STRING];
    
    typedef struct {
            String Name;
            double Size;
    } ArrayType;
    
    typedef ArrayType Array[];
    
    int main(void) {
    
    Array = malloc(sizeof(*Array));
    
    }
    isn't this the right way?
    Also, i cant figure out how to realloc the array.
    I would really appreciate any help.
    Last edited by pseudonoma; 03-01-2008 at 06:59 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    What are you doing with that last typedef?

    What are you trying to do? It's a little confusing just looking at your code.

    Basically, you need a type that you want to malloc that you sizeof and pass to malloc, and a pointer to that type that you assign the result of malloc to.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    So should it be:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX_STRING 128
    
    typedef char String[MAX_STRING];
    
    typedef struct {
            String Name;
            double Size;
    } ArrayType;
    //--------------------------------------------------------------------
    int main(void) {
    
    ArrayType *Array = malloc(sizeof(Array));
    
    }
    As much as I try to understand pointers, i get more confused

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    more like sizeof(*Array) or sizeof(ArrayType)

    sizeof(Array) would just give you the size of the pointer to ArrayType, which is probably only a couple of bytes (typically 4 on 32-bit processors, 8 on 64-bit processors), which is probably too small for ArrayType.

    free it when you're done.

    >As much as I try to understand pointers, i get more confused

    pointers are just variables that hold an address to another variable. When you access the other variable through the pointer, that's called dereferencing.

    dereference with the * operator. like this:
    Code:
    int i; /* 'integer' i */
    int *p; /* 'pointer to integer' p */
    
    p = &i; /* put the address of the variable i into the pointer to integer p. */
    *p = 15 ; /* dereferences p, and assigns 15 to the integer i */
    
    printf("&#37;d %d\n" , *p, i ); /* prints "15 15" */
    http://www.cprogramming.com/tutorial/c/lesson6.html
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Last edited by robwhit; 03-02-2008 at 01:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To malloc or not realloc on struct members
    By gh0st in forum C Programming
    Replies: 4
    Last Post: 11-17-2006, 05:27 AM
  2. malloc, realloc
    By figo2476 in forum C Programming
    Replies: 3
    Last Post: 04-28-2006, 10:11 PM
  3. Linked list versus malloc and realloc.
    By Bajanine in forum C Programming
    Replies: 2
    Last Post: 06-20-2005, 08:08 PM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. Need help on malloc and realloc
    By YevGenius in forum C Programming
    Replies: 8
    Last Post: 03-06-2004, 01:55 AM