Thread: Array of size "n"

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    2

    Array of size "n"

    Hello,

    I am trying to create an array of size "n" where n is to be determined halfway through the program (after various data is entered and the result calculated).

    I have defined the two variables as:

    int n;
    double x[n];

    but I get an error message each time I try and build the program.

    I'm relatively new to C so please don't use too technical language.

    Thank you in advance for your help!
    Jo

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    I believe only C99 supports variable arraysizes (without the use of dynamic memory). The functions you want to look up are malloc and free.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    int n;
    double *x;
    
    // Get n from user
    
    x = malloc(sizeof(double) * n);
    
    // now you can use x as an array
    
    free(x);
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    2

    Thank you!

    Yay!

    It works. Thank you very much for your help!

    Jo

  5. #5
    Quote Originally Posted by itsme86
    Code:
    int n;
    double *x;
    
    // Get n from user
    
    x = malloc(sizeof(double) * n);
    
    // now you can use x as an array
    
    free(x);
    And of course, you will test the value returned by malloc() against NULL before using it.
    Emmanuel Delahaye

    "C is a sharp tool"

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    5
    I thought I'd just mention calloc() and realloc(). calloc() is similar to malloc, but it clears the memory allocated. And realloc() "reallocates" memory. i.e. change the size of allocated memory.

  7. #7
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    >I thought I'd just mention calloc() and realloc().
    It's best to avoid them unless you're sure you know what you're doing. calloc can be misleading when all bits zero is not meaningful for the type pointed to, and realloc is surprisingly tricky to use because it may or may not invalidate pointers to within the memory and mysterious bugs can arise because, depending on the arguments, realloc can function like itself, or malloc, or free. There are just too many what-if's for those two functions for my comfort.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  2. Array size
    By tigrfire in forum C Programming
    Replies: 5
    Last Post: 11-14-2005, 08:45 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM