Dynamic Array's

This is a discussion on Dynamic Array's within the C Programming forums, part of the General Programming Boards category; Hi I am looking to implement an Array where the size doesn't have to be stated. At the moment i ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    Dynamic Array's

    Hi

    I am looking to implement an Array where the size doesn't have to be stated.

    At the moment i have:

    Code:
    int num [8] = {76, 13, 14, 1, 55, 33, 24, 65}
    So the size is 8 but how can I code it so the array size isn't defined?

    Thanks

    Tom

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    460
    you meant like this?
    Code:
    int num [] = {76, 13, 14, 1, 55, 33, 24, 65}
    you don't need the size of the array when you initialize it. however, you'd need it later when you want to operate on the array. otherwise, you'll have to use malloc to allocate the memory, and implement something like the linked-list.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    Hi

    Thanks for the reply. I have seen the maloc function when searching google but have no idea how to use it.

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    460
    to use malloc with dynamic array, you still need the size of the array.
    Code:
    int *array = malloc( 8 * sizeof(int)); <-- allocated space for 8 intergers.  
    free(array); // call free after done with it.
    Last edited by nimitzhunter; 02-13-2011 at 04:36 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    Ok thanks for the reply.

    So if you have to define the size of maloc array then how can you have undefined array size?

    I am new to C and still learning thanks.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    So if you have to define the size of maloc array then how can you have undefined array size?
    You allocate what you need with malloc(). Expand or shrink with realloc().
    Release the memory with free().

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    That has worked thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 11:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 12:32 PM
  4. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 08:34 PM

Tags for this Thread


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