-
May 1st, 2007, 06:45 PM
#1
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!
-
May 1st, 2007, 07:11 PM
#2
Re: Pointers and "explode" function
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++.
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.
-
May 1st, 2007, 08:42 PM
#3
Re: Pointers and "explode" function
You could use the strtok() function to split up your line into tokens.
Regards
Alan
-
May 2nd, 2007, 05:45 AM
#4
Re: Pointers and "explode" function
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.
-
May 2nd, 2007, 08:55 AM
#5
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.
}
-
May 2nd, 2007, 09:12 AM
#6
Re: Pointers and "explode" function
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.
-
May 2nd, 2007, 10:10 AM
#7
Re: Pointers and "explode" function
Originally Posted by Clash
Hmm, I think I got it, I send what I want to be returned, right?
Yes, you got it !
-
May 3rd, 2007, 09:55 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|