Author |
Message |
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
|
Back to top |
|
|
tcsoccerman Server Help Squatter
Age:32 Gender: Joined: Jan 15 2007 Posts: 694 Location: Atlantis Offline
|
Posted: Mon Dec 01, 2008 3:06 pm Post subject: |
|
|
|
|
google search="c++ function array return" first link.
int *foo(int *array)
{
int *start = array;
return start;
}
int main()
{
int myarray[] = {1,2,3,4,5,0};
foo(myarray);
return 0;
}
output="1,2,3,4,5" |
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Mon Dec 01, 2008 3:17 pm Post subject: |
|
|
|
|
tcsoccerman wrote: | google search="c++ function array return" first link.
int *foo(int *array)
{
int *start = array;
return start;
}
int main()
{
int myarray[] = {1,2,3,4,5,0};
foo(myarray);
return 0;
}
output="1,2,3,4,5" |
I know that but how would you do that with char* mychar[10][80] ? |
|
Back to top |
|
|
Cheese Wow Cheese is so helpful!
Joined: Mar 18 2007 Posts: 1017 Offline
|
Posted: Mon Dec 01, 2008 6:15 pm Post subject: |
|
|
|
|
mychar[3][5] = mychar[15] _________________ SSC Distension Owner
SSCU Trench Wars Developer |
|
Back to top |
|
|
Bak ?ls -s 0 in
Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Mon Dec 01, 2008 6:42 pm Post subject: |
|
|
|
|
You don't want to return an array by reference since the memory for an array is allocated on the stack which gets reused once the function returns. Your two options are to declare the array on the heap, or return by value (which you can't really do either). Alternatively, you could pass in a stack array like in tsoccerman's sample code (although you won't be able to resize it based on the number of words).
This is probably the easiest way of doing what you want, although it's definitely not the most efficient
Quote: | vector <string> GetWords(char* input)
{
std::stringstream ss(std::stringstream::in|std::stringstream::out);
ss<<input;
int words = GetNumberOfWords(input);
vector <string> wordlist;
for(int i=0;i<words;++i){ string s; ss>> s; wordlist.push_back(s);
return wordlist;
} |
You could be more efficient by using the heap directly, although I'm not sure how good you are with dynamic memory . _________________ SubSpace Discretion: A Third Generation SubSpace Client |
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
|
Back to top |
|
|
k0zy Server Help Squatter
Gender: Joined: Jan 11 2003 Posts: 571 Location: Germany Offline
|
Posted: Tue Dec 02, 2008 2:00 pm Post subject: |
|
|
|
|
You missed one * in the function.
The return type has to be char**.
Otherwise it's an array of char, not of strings. _________________ It's a shark! Oh my god! Unbelievable! |
|
Back to top |
|
|
Bak ?ls -s 0 in
Age:25 Gender: Joined: Jun 11 2004 Posts: 1826 Location: USA Offline
|
Posted: Tue Dec 02, 2008 3:51 pm Post subject: |
|
|
|
|
make sure you do "#include <vector>", "#include <string> and "using namespace std;" at the top of your code
With dynamic memory, it's tricky because you need to declare the array dynamically, as well as each string dynamically. At the end you have to remember to free all the data (each string, and then the array) in order to not have a memory leak.
So your function would look like this (using c-style dynamic memory instead of c++):
char** GetWords(char* input)
{
std::stringstream ss(std::stringstream::in|std::stringstream::out);
ss<<input;
int words = GetNumberOfWords(input);
char** wordlist = malloc((words + 1) * sizeof(char*));
wordlist[words] = 0; // so we know when to stop
for(int i=0;i<words;++i){ string s; ss >> s; wordlist[i] = malloc(s.length() + 1); strcpy(wordlist[i],s.c_str()); }
return wordlist;
} |
And then remember to free the data after you use it:
char ** words = GetWords(input);
for (int x = 0; words[x] != 0; ++x)
{
cout << "word " << x << " is " << words[x] << endl;
free(words[x]);
}
free(words);
|
|
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Tue Dec 02, 2008 9:49 pm Post subject: |
|
|
|
|
Thanks I get it actually, just one thing confuses me, when you allocated memory for char** wordlist, why did you do words + 1 ? wouldn't it be enough with just char** wordlist = (char**)malloc((words)*sizeof(char*)) |
|
Back to top |
|
|
k0zy Server Help Squatter
Gender: Joined: Jan 11 2003 Posts: 571 Location: Germany Offline
|
Posted: Wed Dec 03, 2008 11:10 am Post subject: |
|
|
|
|
Bak did that, so you don't have to keep track of how many items you got inside the arrray.
Otherwise, you'd always have to know the exact amount of words when you want to iterate over it.
With bak's way, you can just stop if you reach 0 (the last word). Take a look at his way to free the array.
Edit: While we are at it.
I think, when you declare a pointer, you should keep the * to the variable name.
I know it's about personal preference here.
What do you think you are declaring when you do:
Hint: It's not two pointers. |
|
Back to top |
|
|
Cheese Wow Cheese is so helpful!
Joined: Mar 18 2007 Posts: 1017 Offline
|
Posted: Wed Dec 03, 2008 5:34 pm Post subject: |
|
|
|
|
it isnt? =( |
|
Back to top |
|
|
tcsoccerman Server Help Squatter
Age:32 Gender: Joined: Jan 15 2007 Posts: 694 Location: Atlantis Offline
|
Posted: Wed Dec 03, 2008 6:39 pm Post subject: |
|
|
|
|
x is y isn't. if i remember correctly. |
|
Back to top |
|
|
Samapico No, these DO NOT look like penises, ok?
Joined: May 08 2003 Posts: 1252 Offline
|
Posted: Thu Dec 04, 2008 12:05 am Post subject: |
|
|
|
|
tcsoccerman wrote: | x is y isn't. if i remember correctly. | exactly
you'd need to repeat the * for each if you want 2 pointers
int *x, *y; _________________ (Insert a bunch of dead links here) |
|
Back to top |
|
|
Dr Brain Flip-flopping like a wind surfer
Age:38 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Thu Dec 04, 2008 6:59 am Post subject: |
|
|
|
|
That's why, style wise, you should always put a space between the type and the *. So int *x; or char *string;. So that you're not confused when declaring multiple pointers on the same line. _________________ Hyperspace Owner
Smong> so long as 99% deaths feel lame it will always be hyperspace to me |
|
Back to top |
|
|
k0zy Server Help Squatter
Gender: Joined: Jan 11 2003 Posts: 571 Location: Germany Offline
|
Posted: Thu Dec 04, 2008 12:46 pm Post subject: |
|
|
|
|
Dr Brain wrote: | That's why, style wise, you should always put a space between the type and the *. So int *x; or char *string;. So that you're not confused when declaring multiple pointers on the same line. |
That's what I was trying to say. |
|
Back to top |
|
|
Dr Brain Flip-flopping like a wind surfer
Age:38 Gender: Joined: Dec 01 2002 Posts: 3502 Location: Hyperspace Offline
|
Posted: Thu Dec 04, 2008 5:42 pm Post subject: |
|
|
|
|
Yeah, sorry. I tend to only read the last 4-5 posts in a thread. Seemed like everyone was confused on the subject. |
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
Posted: Thu Dec 04, 2008 7:20 pm Post subject: |
|
|
|
|
Bob Dole.. Bob Dole... Bob Dole...... bob dole.... bob... dole.... wrote: | Bak did that, so you don't have to keep track of how many items you got inside the arrray.
Otherwise, you'd always have to know the exact amount of words when you want to iterate over it.
With bak's way, you can just stop if you reach 0 (the last word). Take a look at his way to free the array.
Edit: While we are at it.
I think, when you declare a pointer, you should keep the * to the variable name.
I know it's about personal preference here.
What do you think you are declaring when you do:
Hint: It's not two pointers. |
Ah I see, as for the pointer i knew that, while ago i tried doing that but I usually use char* just because it looks better and with char* I don't declare more then one on the same line because the code looks nicer. |
|
Back to top |
|
|
hellzlaker Registered Cap Buster Popping men in the ass since Oct 2005
Gender: NEVER ENOUGH! Joined: Oct 27 2005 Posts: 34 Offline
|
|
Back to top |
|
|
|