Thread: array of a variable size

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    10

    array of a variable size

    One thing I never learned to do is create a variable sized array. The compiler always complained about it.

    I can do int theArray[10];

    but how do I do

    int theArray[numOfInts] ?

    Let's say numOfInts is being read from a file. How could I pass it to the above, as a constant or using #define somehow?

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    use malloc to allocate to how much every memory u want.

    Code:
    int *array;
    var = 100;
    
    array = malloc ( sizeof int * var );
    Perhaps if u need to extend the array size if realloc function to extend the array size at runtime. And dont forget to check the return value of malloc.

    ssharish

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    That should be:
    Code:
    array = malloc(var * sizeof *array);
    Or to simply fix the code above:
    Code:
    array = malloc(sizeof(int) * var);
    When sizeof is applied to a type, the type has to be in parentheses. They aren't needed when sizeof is applied to an expression.

    To the OP: If you use malloc() like this, you should call free() with array when you're done with it:
    Code:
    int *array = malloc(sizeof *array * 5), i;
    if(array == NULL) { /* Do something, memory couldn't be allocated. */ }
    for(i = 0; i < 5; i++) array[i] = i;
    free(array);

  4. #4
    Devil™
    Join Date
    Oct 2007
    Location
    IIT-Kharagpur, India
    Posts
    104
    Quote Originally Posted by dougwilliams View Post
    One thing I never learned to do is create a variable sized array. The compiler always complained about it.

    I can do int theArray[10];

    but how do I do

    int theArray[numOfInts] ?

    Let's say numOfInts is being read from a file. How could I pass it to the above, as a constant or using #define somehow?
    you cannot do that as it is..
    coz C dosent allow you to give variable length of arrays.. it is something like C compiler allocates memory for your array during compilation

    but there is a way to get it done..
    That is called Dynamic Memory Allocation(DMA)..
    you get memory allocated for you array even when the program is running..
    Many ppl suggest to keep normal fixed arrays also this way.. dont worry now..

    you can use two functions
    malloc();
    calloc();
    both are nearly the same with little difference.. for now use malloc();

    you can get many tutorials on internet for using malloc (DMA)

    if you want to go easy..
    Code:
    int theArray[noOfInts];
    
    //replace the above line with the following 2 lines
    int *theArray;
    theArray = malloc(noOfInts * sizeof(int));
    
    //if you want to use your array use it as normal array like
    
    theArray[10]=20;
    printf("&#37;d\n",theArray[5]);
    
     //remember that you can only index from 0 to (noOfInts -1)
     printf("%d\n",theArray[noOfInts]);
    //this might give segmentation fault when you run it, but not during compiling
    
    //P.S: dont forget to include stdlib.h at the top of your file
    similarly if you want an array of chars, just replace the int in above code with char..
    C's Motto: who cares what it means? I just compile it!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM