Thread: Array error learning from a book

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Array error learning from a book

    I placed the multi array below in my code straight from a book and get the following error:

    Number of initializers cannot be greater than the number of aggregate members

    Code:
    int multi[3][4] = {{1,2,3,4,5,6,7,8,9,10,11,12}};
    Does anyone see why this line would cause the error?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Does anyone see why this line would cause the error?
    Yes, and the error message says what is the problem: you are trying to initialise a two dimensional array with a two dimensional array whose inner array has more elements than that specified by the two dimensional array to be initialised.

    You probably want to write:
    Code:
    int multi[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
    or if you are lazy:
    Code:
    int multi[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
    However, I am usually confused by multi-dimensional arrays, so I try not to be lazy for my own sake.
    Last edited by laserlight; 05-05-2008 at 08:29 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    115
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a phone book program
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-19-2007, 06:31 AM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 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. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM