Thread: Embaracing Array question

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    58

    Embaracing Array question

    Hi everyone

    This is a REALLY silly question but:

    if I declare an array like this:

    Code:
    uint8_t myArray[5];
    I get a one dimensional array of uint8_ts...

    0
    0
    0
    0
    0

    If I declare a two dimensional array like this:

    Code:
    uint8_t myArray[5][1];
    Does this create an array with two rows in it?

    0 0
    0 0
    0 0
    0 0
    0 0

    So if I declare a two dimensional array like this:

    Code:
    uint8_t myArray[5][2];
    0 0 0
    0 0 0
    0 0 0
    0 0 0
    0 0 0

    and so on...

    The bit that is confusing me is that the first location in an array is zero, therefore if I had an array width of 1 does this mean I get two rows in the array or just one?

    Thanks to anyone who can clear this up

    D

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just like the first 5 means that you have five element rows, the second number means how many elements in the column you have. So if you want two elements in a row, your second set of brackets should have 2 in it - the valid values to index these columns would be 0 and 1. If you want three columns, use 3 for the declaration, and 0..2 for indexing when grabbing the values out.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If I declare a two dimensional array like this:

    Code:
    uint8_t myArray[5][1];
    Does this create an array with two rows in it?

    0 0
    0 0
    0 0
    0 0
    0 0
    A two dimensional array? Yes, it creates a two dimensional array. With two rows (you mean columns?)? No, the second dimension is 1 so it is more like:
    0
    0
    0
    0
    0
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM