CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 1999
    Posts
    10

    initializing a array...

    i wanna just initialize a array of BOOL data as following,
    BOOL myArray[12][11];



    In the constructor i initialise the array like this:

    CMyDoc::CMyDoc()
    {
    ...........
    for (int y; y<11;++y)
    {
    for(int x; x<12; ++x)
    {
    myArray[x][y]=FALSE;
    }
    }
    ....................
    }



    It seems to have no problem. But when i started debugging, i have found that myArray[][]
    was comically allocated with such values like "-847851241" or "577". But some elements
    in the array was correctly allocated with "0".

    Why ???
    help me.....Thanx...
    ur toobe

    :-)

  2. #2
    Join Date
    May 1999
    Location
    L.A.
    Posts
    20

    Re: initializing a array...

    I might have a clue.
    Use the for {...} statements like this:

    for (int y=0; y<11; ++y)
    {
    ...



    I believe that the 'x' and 'y' variables are unassigned and can be of any value.
    It is sheer luck that some values in the array are '0'.

    /Johan


  3. #3
    Join Date
    May 1999
    Location
    L.A.
    Posts
    20

    Re: initializing a array...

    Sorry...
    I saw another mistake:
    Do like this:

    for (int y=0; y<12; y++)
    {



    Increase y afterwards. Otherwise, position [0] in the array won't be assigned.
    Remember the array is indexed 0, 1, 2, ..., 11 if you initialize it with MyArray[12];

    /Johan M


  4. #4
    Join Date
    Apr 1999
    Posts
    383

    Re: initializing a array...


    > BOOL myArray[12][11];

    This defines the array, reserving memory for it, but it doesn't initalise it. You have to initialise it yourself. The only time things get initialised to zero by default is when they are declared static.

    With an plain 'C'-style array like this, you can either use for/next to loop through, initialising each element individually, or you can treat it like a block of memory and use memcpy or FillMemory to write the required values all at once (this last is only safe with plain 'C'-style arrays, *not* with array classes).

    Dave



  5. #5
    Join Date
    May 1999
    Posts
    28

    Re: initializing a array...

    Try

    CMyDoc::CMyDoc()
    {
    ...........
    for (int y=0; y<11; y++)
    {
    for(int x=0; x<12; x++)
    {
    myArray[x][y]=FALSE;
    }
    }
    ....................
    }



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured