Is there a way to...

This is a discussion on Is there a way to... within the C Programming forums, part of the General Programming Boards category; If you have an array can you allocate it to a certain amount then if the user fills up the ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    11

    Is there a way to...

    If you have an array can you allocate it to a certain amount then if the user fills up the array can you use malloc to adjust the array to add more elements if needed? Thanks for any replies.

    -CDuddley

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Yes, in fact, there's a function similar to malloc that does just that. Look up the function realloc(). It's one of the three allocation functions in C.

    -Prelude

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    11

    Thanks...

    Thanks a bunch man ...

    -CDuddley

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Anytime
    I would have detailed it in my post, but my copy of K&R isn't on me as it should be and I don't remember the exact definition.

    -Prelude

  5. #5
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    fyi!

    you need to include stdlib.h
    and the ptype is

    void *realloc(void *ptr, size_t size);

    change the size of mem pointed to by ptr

    there will be no effect if there is not enough availabe memory

    hth
    Monday - what a way to spend a seventh of your life

Popular pages Recent additions subscribe to a feed

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