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

    2d dynamic arrat

    The below memory allocation for a and a1 are correct?.
    I am getting incorrect values while printing array contents
    I ran it in Linux with g++ compiler
    Please post your comments


    Pg1 and Pg2 are same exceptin pg2 - array dimensions are stored in variable

    Pg1
    int (*a)[2];
    a = (int (*)[2])malloc(3 * sizeof(int));
    a[0][0]=3;
    a[0][1]=4;
    a[0][2]=5;
    a[1][0]=6;
    a[1][1]=7;
    a[1][2]=8;


    Pg2

    int n=2;
    int m=3;
    int (*a1)[n];
    a1 = (int (*)[n]) malloc (m*sizeof(int));
    a1[0][0]=3;
    a1[0][1]=4;
    a1[0][2]=5;
    a1[1][0]=6;
    a1[1][1]=7;
    a1[1][2]=8;

    cout<<"\n"<<a1[0][0]<<a1[0][1]<<a1[0][2]<<a1[1][0]<<a1[1][1]<<a1[1][2];
    cout<<"\n"<<a[0][0]<<a[0][1]<<a[0][2]<<a[1][0]<<a[1][1]<<a[1][2];


    Output

    346678
    8811-111075563152

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: 2d dynamic arrat


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