Thread: Array of Arrays

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    8

    Array of Arrays

    Hello,

    I am wondering how I could make an array which contains arrays, but with a variable size.

    My first try..

    Code:
    int array[][] = {{1}, {2}};
    But this isn't proper. Any guidance?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You can only leave the left-most [] of a multi-dimensional array empty.

    For all the minor sizes, you must specify a size.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You can use VLAs, but you can't use them with initializers. So you can declare the array, but you can't initialize it:
    Code:
    int x = 3, y = 4;  // feel free to read this in from user/file or generate in some other way
    int array[x][y];  // can't use an initializer = {{1, 2, 3}, ...}, fill using loops.
    The other option is dynamic memory allocation: Question 6.16. This also requires you to fill data with loops.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-04-2012, 09:03 PM
  2. array of arrays?
    By TAZIN in forum C Programming
    Replies: 7
    Last Post: 04-25-2011, 01:17 PM
  3. Array of Arrays (possible in C)?
    By legendbb in forum C Programming
    Replies: 8
    Last Post: 05-31-2010, 09:36 PM
  4. Array of Arrays
    By Cdrwolfe in forum C++ Programming
    Replies: 15
    Last Post: 07-13-2006, 05:27 AM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 12:43 AM