Array Question

This is a discussion on Array Question within the C Programming forums, part of the General Programming Boards category; Is it possible to change/assign an array size during the program? Eg: int ik[]; void initialize(size i){ /* Assign size ...

  1. #1
    Unregistered
    Guest

    Array Question

    Is it possible to change/assign an array size during the program?
    Eg:

    int ik[];

    void initialize(size i){
    /* Assign size i to array ik */
    }??????????

    I'm think we can do it somehow with pointer but don't know how.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The way you have it? No. Another way? Maybe.

    Not all compilers support dynamicly sized arrays. This was added in C99 (there have been other discussions on it, read down a few pages).

    However, the easiest way to do it, is simply use a pointer, and treat it like an array, since an array is in effect, just a pointer:

    int *myArray, myArraySize;

    myArraySize = getSizeFromUser( );
    myArray = malloc( sizeof( int ) * myArraySize );

    Vola!

    Quzah.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    I suppose I ought to reply, just so Quzah knows he beats me some of the time
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Unregistered
    Guest
    Originally posted by quzah
    The way you have it? No. Another way? Maybe.

    Not all compilers support dynamicly sized arrays. This was added in C99 (there have been other discussions on it, read down a few pages).

    However, the easiest way to do it, is simply use a pointer, and treat it like an array, since an array is in effect, just a pointer:

    int *myArray, myArraySize;

    myArraySize = getSizeFromUser( );
    myArray = malloc( sizeof( int ) * myArraySize );

    Vola!

    Quzah.
    malloc is EVIL!

    (Checks board)
    Wait, never mind. This is the C board. I will return back to the C++ board

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM

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