intialising array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: intialising array

  1. #1
    Join Date
    Feb 2006
    Posts
    157

    intialising array

    Dear all,
    I am trying to create and initialize a two dimensional and dynamic boolean array with all elements set to zero.
    I tride
    Code:
    bool** myarray;
    myarray= new bool[dim1][dim2]
    But that doe snot work.
    Do you have any suggestions?
    Thanks

  2. #2
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: intialising array

    Code:

    Code:
     bool myarray[11][12];
     memcpy(myarray,0,sizeof(myarray));
    With regards
    Programartist

  3. #3
    Join Date
    Feb 2006
    Posts
    157

    Re: intialising array

    dim1 and dim2 are not constants

  4. #4
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: intialising array

    Hi,

    There's not a one-line way of doing it, since C++ requires the first dimension to be constant.

    If you don't need the memory to be continguous you could create an array of length dim1, then loop dim1 times and create arrays of size dim2, and store pointers to those in the first array.

    If you need the memory to be contiguous, you could create a 1D array of size dim1*dim2, then calculate the indexes yourself when you need to access it.

    You can use memcpy as suggested earlier to initialize each array.

    However, it's probably best to use vector from the std template library anyway, unless you need to use an array for some specific reason.

    Alan

  5. #5
    Join Date
    Apr 1999
    Posts
    27,418

    Re: intialising array

    Quote Originally Posted by ProgramArtist View Post
    Code:

    Code:
     bool myarray[11][12];
     memcpy(myarray,0,sizeof(myarray));
    With regards
    Programartist
    You shouldn't be using memcpy() on bool arrays. A bool can only be assigned true or false. Using memcpy() bypasses this
    check and just blindly writes 0, which may or may not be correct (depending on the implementation).

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: intialising array

    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center