Thread: malloc in structures

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    malloc in structures

    I was wondering if u could malloc in a structure parts at a t time.

    Code:
    typedef struct {
        char part[10];
        char part2[20];
        randomstruct * ptr;
    } struct1;
    Is there a way to malloc space for the part and part2 without touching the pointer for randomstuct
    like
    struct1 test;
    test->part = malloc(sizeof(part));
    ???????

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You would need to declare them as pointers.
    Code:
    typedef struct {
        char* part;
        char* part2;
        randomstruct * ptr;
    } struct1;
    
    struct1 test;
    test.part = malloc(10);
    test.part2 = malloc(20);
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, structures, arrays and sudoku!!
    By AmbliKai in forum C Programming
    Replies: 13
    Last Post: 10-15-2008, 03:05 AM
  2. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  3. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  4. malloc for tree structures
    By Tumbuler in forum C Programming
    Replies: 1
    Last Post: 10-31-2007, 06:43 PM
  5. malloc ing array's or structures
    By Markallen85 in forum C Programming
    Replies: 4
    Last Post: 02-03-2003, 01:23 PM