Thread: question about a piece of code

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    34

    Exclamation question about a piece of code

    char board[9]={0,1,2,3,4,5,6,7,8};

    I know what the char board[9] is and does but what does {0,1,2,3,4,5,6,7,8} do i thought you can only give it one value

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    The {...} part is the initaliser list. It allows you to initialise all of the elements of the array at once. Thus, board[ 0 ] = 0, board[ 1 ] = 1, etc.

    If there is just one number within the braces, then the whole array is filled with that number.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by XSquared
    If there is just one number within the braces, then the whole array is filled with that number.
    No. If there are less than N initializers, N being the size of the array, then all the rest are filled with zero.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    34
    i see

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    8

    Re: question about a piece of code

    Originally posted by librab103
    char board[9]={0,1,2,3,4,5,6,7,8};

    I know what the char board[9] is and does but what does {0,1,2,3,4,5,6,7,8} do i thought you can only give it one value
    char board [9] is an array of datatype 'character' which can store upto 9 alphabets(without spaces).

    Hence in your example, the variable board can store something like 'functions'.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Actually, it can only hold a word 8 letters long, because of the need for NULL-termination. But that's not the context it's being used in.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    85
    also keep in mind that it is illegal for an initializer to be completely empty or to be longer than the array it initializes!

    cheers, Ben
    Medical Robotics: "Pursuing perfection in healthcare through innovations in robotics and information technologies for medicine and surgery."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Tutorial sample code question
    By Kalleos in forum C Programming
    Replies: 2
    Last Post: 01-16-2009, 12:20 PM
  2. Problem with simple piece of code
    By Furious5k in forum C++ Programming
    Replies: 10
    Last Post: 12-12-2008, 05:25 PM
  3. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 10:35 AM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. question regarding code found on this board
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-23-2002, 08:03 PM