Thread: newbee question:can you initialise an array after it's declaration & if so how?

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    newbee question:can you initialise an array after it's declaration & if so how?

    hi there the answer to this is prolly very obvious & i missed it but well asking anyway

    i'd like to know why i can initialise an array like this:

    Code:
    int myarray[5][2]={0,0,0,0,0,0,0,0,0,0};
    but doing so doesn't work:

    Code:
    int myarray[5][2];
    myarray={0,0,0,0,0,0,0,0,0,0};
    (with that code compilers just complain that there is a parsing error and a ; is expected before the {

    is it only possible to initialise all members of the array when creating it or am i just making a syntax error? (initialising at start isn't possible in my case as the array is part of a struct & u compiler complains that i can't initialise variables there)

    thanks in advance for any help

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    you cant initialize an array in that manner except at creation.

    the most common way of initializing an array after creation is a for loop:

    Code:
    for ( int i = 0; i < SizeOfArray; i++ )
    {
       array[i] = value;
    }
    as a side note, you dont have to do this:

    Code:
    int myarray[5][2]={0,0,0,0,0,0,0,0,0,0};
    to initialize the entire array. all you have to do is:

    Code:
    int myarray[5][2] = {0,};
    and it will fill the whole array in with 0s for you.

    another option you have is the memset function. i will let you look that one up yourself, but it sets an entire block of memory to a certain value.
    I came up with a cool phrase to put down here, but i forgot it...

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    2
    oki thanks for the quick answer)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-12-2004, 12:15 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM
Website Security Test