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

This is a discussion on newbee question:can you initialise an array after it's declaration & if so how? within the C++ Programming forums, part of the General Programming Boards category; hi there the answer to this is prolly very obvious & i missed it but well asking anyway i'd like ...

  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
    ...
    ... is offline
    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, 07:25 PM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21