Thread: how to generalise arrays size

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    Question how to generalise arrays size

    I'd like to take array's size as input but not declaring before. How to do it.

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    Thats not possible in C. c++ would work. or dynamic arrays would work

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    C99 allows you to do this

    Code:
    int size;
    scanf("%d", &size);
    int array[size];
    In all versions, you can do
    Code:
    int size;
    scanf("%d", &size);
    int *array = malloc( size * sizeof(*array) );
    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.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    Tell me about this dynamic arrays. how could I possibly use them.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    @Salem : thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D arrays, size allocated at run time?
    By Kat007 in forum C++ Programming
    Replies: 8
    Last Post: 09-18-2010, 02:04 PM
  2. Generalise functions by specifying type as an argument
    By explodecomputer in forum C Programming
    Replies: 14
    Last Post: 07-06-2010, 06:50 AM
  3. Is there a limit to the size of 2-D arrays?
    By ashley in forum C Programming
    Replies: 2
    Last Post: 01-04-2007, 04:01 PM
  4. Variable size arrays
    By crag2804 in forum C Programming
    Replies: 4
    Last Post: 09-08-2002, 06:00 PM
  5. size of integer arrays
    By steve8820 in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 07:31 PM

Tags for this Thread