Thread: Return an array of structs.... What am I missng.

  1. #1
    Registered User
    Join Date
    Mar 2014
    Location
    Ruckersville Va.
    Posts
    4

    Return an array of structs.... What am I missng.

    cannot convert from 'Map *' to 'Map (*__w64 )[12][16]' I am thinking I overcomplicated this one or I just don't see it.



    Code:
    int map1[12][16]={
       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
       {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3},
       {3,3,3,4,3,3,3,3,3,3,3,3,3,3,2,3},
       {4,4,4,4,4,4,4,4,4,4,4,4,4,1,2,3},
       {3,3,3,3,3,3,3,3,3,3,3,3,4,1,2,3},
       {3,3,3,3,3,3,3,3,3,3,3,3,4,1,2,3},
       {1,3,3,3,3,3,3,3,3,3,3,3,4,1,2,3},
       {4,4,4,4,4,4,4,4,4,4,4,4,4,1,2,3},
       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3},
       {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3},
       {3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,3},
       {4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4}};
    
    
    struct    Map
    {
      public:
        int    Xpos; // X position in Pixels
        int    Ypos; // Y position in Pixels
        int    ID;   // The ID number that corresponds to the Bitmap to display
        };
    
     
    Map* LoadMap(int lvl[12][16])
        {
          Map tm[12][16];
     
     
          for (int y=0; y<12; y++)
           {
            for (int x=0; x<16; x++)
              {
               tm[y][x].Xpos = x*50;
     
                if(lvl[y][x] == 1)
                     {
                       cout<<"1 ";
     
                      tm[y][x].ID = 1;
                     }
                    if(lvl[y][x] == 2;
                       {
                          cout<<"2 ";
                          tm[y][x].ID = 2;
                       }
                     If(lvl[y][x] == 3)
                        {
                       cout<<"3 ";
                      tm[y][x].ID = 3;
                       }
    
                     if(lvl[y][x] == 4)
                      {
                      cout<<"4 ";
                      tm[y][x].ID = 4;
                      }
     
                  }
     
                    tm[y][x].Ypos= y*50;
     
    cout<<"\n";
     
     
            }
    return   *tm;                 // error is here or
     
        }
    
    
    
    void main()
    {
    
    Map lvl1[12][16];               
     
    &lvl1=LoadMap(map1);   // error is here
    
    return;
    }

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    It looks like you're making a static allocation of data and then returning a pointer to it which I think will cause severe lifetime problems.

    Also, the compiler is telling you that a pointer to a static allocation like what you're doing is not the same as a simple pointer to anywhere in the memory. So no more static, use dynamic.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You cannot return a C array. Period. C arrays behave differently from all other data types.
    Furthermore, returning a pointer to local stack data is undefined behaviour. The data will go away once the function ends which makes the pointer point to invalid data.

    However, you can return an array if you use std::array or std::vector. The real C++ types that do not act special in any way.
    Also, main shall return int and nothing else.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    A function can return a pointer to a static or an allocated (malloc or new) array or matrix, but not a local array or matrix since it's memory on the stack effectively gets freed when the function returns. Syntax:

    Code:
    int (*returnptrtomatrix())[12][16]
    Last edited by rcgldr; 03-29-2014 at 02:24 PM.

  5. #5
    Registered User
    Join Date
    Mar 2014
    Location
    Ruckersville Va.
    Posts
    4
    Thanks for the info guys. I have also figured out that my compiler (VS.net 2003) isn't doing me any favors as it is so out of date. I downloaded the new dev-C but if you know of a better one that's free I like to hear about it.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You could just upgrade to Visual Studio 2013 Express. Dev-C++ has long since been abandoned (not maintained), to the best of my knowledge.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    You could just upgrade to Visual Studio 2013 Express. Dev-C++ has long since been abandoned (not maintained), to the best of my knowledge.
    Visual Studio is a great IDE, and in fact, it's the best one available for Windows, but its C++11 support is still pretty poor, even 2+ years and two major version releases after the standard's adoption. MinGW with G++ 4.8, and Code::Blocks, Eclipse or Netbeans would be good alternatives, if you want to make use of all that C++11 has to offer.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    ...but its C++11 support is still pretty poor, even 2+ years and two major version releases after the standard's adoption...
    No, it's not. The latest version is pretty compliant.
    Yes, it is missing a few bits here and there, but it's definitely not as poor as you make it out to be.
    For reference, here is a list: C++11/14 Core Language Features in VS 2013 and the Nov 2013 CTP - Visual C++ Team Blog - Site Home - MSDN Blogs

    Oh and btw, MinGW is not fully C++11/14 compliant, either.
    If you want a compiler that support C++11 and complete Draft C++14, then Clang is a good choice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    No, it's not. The latest version is pretty compliant.
    Yes, it is missing a few bits here and there, but it's definitely not as poor as you make it out to be.
    For reference, here is a list: C++11/14 Core Language Features in VS 2013 and the Nov 2013 CTP - Visual C++ Team Blog - Site Home - MSDN Blogs
    There is still a lot of "No" in that list, and many of them are things I use often. Obviously, individual preferences may vary, of course.

    Quote Originally Posted by Elysia View Post
    Oh and btw, MinGW is not fully C++11/14 compliant, either.
    If you want a compiler that support C++11 and complete Draft C++14, then Clang is a good choice.
    G++ 4.8 may not be 100 percent complete, but it's very close, and much closer, in fact, than VC++. 4.9 is going to be complete, but since it hasn't been released yet, it's not available with MinGW yet, nor (as far as I know) as a repository package for any Linux distro.

    Good call on Clang. I haven't used it, but I've heard nothing but good things about it, except from people who have tried to build it from source on windows. I've been meaning to give it a try, but haven't found the time.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Elkvis View Post
    There is still a lot of "No" in that list, and many of them are things I use often. Obviously, individual preferences may vary, of course.
    True, but that does not equal poor support. That's only the core features, too. It does not include the standard library, which to my knowledge, is fully compliant unless it depends on core features not yet supported in the compiler.
    So while "many of them are things I use often" is a big negative to you, it may or may not be to others, and it does absolutely not imply that overall support is poor.

    Good call on Clang. I haven't used it, but I've heard nothing but good things about it, except from people who have tried to build it from source on windows. I've been meaning to give it a try, but haven't found the time.
    Clang has absolutely wonderful tools aside from being a wonderful compiler. The only problem is that it does not work with Visual Studio, which is kind of a dealbreaker for me.
    As for building it on Windows... let's not go there. Been there once. Not going back.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Elysia View Post
    As for building it on Windows... let's not go there. Been there once. Not going back.
    That's exactly what I was referring to. I remember your thread on the tech board, and wouldn't wish your experiences on my worst enemies
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-20-2011, 09:43 PM
  2. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  3. return an array of structs
    By MK27 in forum C Programming
    Replies: 14
    Last Post: 02-14-2010, 08:31 PM
  4. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  5. Replies: 12
    Last Post: 12-06-2005, 08:30 PM

Tags for this Thread