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

Thread: Pointers and "explode" function

  1. #1
    Join Date
    May 2007
    Posts
    90

    Pointers and "explode" function

    1. I'm making a level editor for my game, so I have a .txt like this
    Layer 1
    Line 1 0,0,47,43,1,0,0,0,38,... (64 tiles)
    Line 2... (64 lines)
    Layer 2
    ... (64x64)
    Layer 3
    ... (64x64)

    On PHP I could use commands like explode(), is there a similar command for C++? I need so I can get the separate values for each comma, thanks again!

    2. Returning an array, am I doing something wrong?
    Code:
    //inside class CLevel;
    int *getCamera()
        {
            int camera[2];
            camera[0] = 40;
            camera[1] = 40;
    
            return camera;
        }
    
    //on int main
    CLevel *levelCommands;
    levelCommands = new CLevel;
    int *camera;
    camera = levelCommands->getCamera();
    camera[0] returns me like a huge int and camera[1] returns me -1... doesn't make any sense. :P

    Thanks guys!

  2. #2
    Join Date
    May 2007
    Posts
    1

    Re: Pointers and "explode" function

    Quote Originally Posted by Clash
    On PHP I could use commands like explode(), is there a similar command for C++? I need so I can get the separate values for each comma, thanks again!
    No, there's not such a thing in standard c++.

    Quote Originally Posted by Clash
    2. Returning an array, am I doing something wrong?
    [code]
    //inside class CLevel;
    int *getCamera()
    {
    int camera[2];
    camera[0] = 40;
    camera[1] = 40;

    return camera;
    }

    camera[0] returns me like a huge int and camera[1] returns me -1... doesn't make any sense. :P
    Technically, you are returning a pointer, not an array. And in this case, you're returning a pointer to a local variable, which leads to this undefined behaviour.

  3. #3
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: Pointers and "explode" function

    You could use the strtok() function to split up your line into tokens.

    Regards
    Alan

  4. #4
    Join Date
    May 2007
    Posts
    90

    Re: Pointers and "explode" function

    Quote Originally Posted by aquartz
    Technically, you are returning a pointer, not an array. And in this case, you're returning a pointer to a local variable, which leads to this undefined behaviour.
    And what would be the correct way to return an array?
    Last edited by Clash; May 2nd, 2007 at 06:26 AM.

  5. #5
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: Pointers and "explode" function

    Hi.

    In C++, you can't really pass or return an array. A common practice is pass the address of the array (along its size) or to pass a reference to the array (in this case, its size must be defined in compile time). The following code ilustrates both cases:

    Code:
    void getCamera(int camera[], size_t size)
    {
      //Your code goes here.
    }
    
    void getCamera(int (&camera)[2]) //Must specify the size here.
    {
      //Your code goes here.
    }

  6. #6
    Join Date
    May 2007
    Posts
    90

    Re: Pointers and "explode" function

    Quote Originally Posted by ltcmelo
    Hi.

    In C++, you can't really pass or return an array. A common practice is pass the address of the array (along its size) or to pass a reference to the array (in this case, its size must be defined in compile time). The following code ilustrates both cases:

    Code:
    void getCamera(int camera[], size_t size)
    {
      //Your code goes here.
    }
    
    void getCamera(int (&camera)[2]) //Must specify the size here.
    {
      //Your code goes here.
    }
    Thanks for your reply,
    Sorry for the dumb question, I just can't understand why should I declare I'm sending info to the getCamera fuction if what I want is to it give me info, not send info to it. Thanks!

    Edit: Hmm, I think I got it, I send what I want to be returned, right?
    Code:
    int getCamera(int (&camera)[2])
        {
            camera[0] = cameraX;
            camera[1] = cameraY;
    
            return camera[2];
        }
    Last edited by Clash; May 2nd, 2007 at 09:21 AM.

  7. #7
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: Pointers and "explode" function

    Quote Originally Posted by Clash
    Hmm, I think I got it, I send what I want to be returned, right?
    Yes, you got it !

  8. #8
    Join Date
    Aug 2002
    Posts
    879

    Re: Pointers and "explode" function

    1. Well you can use strtok() or
    write your own explode() function
    http://www.codeguru.com/forum/showth...ight=tokenize#

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)