|
-
Sep 1st, 2001, 01:57 PM
#1
Thread Starter
Fanatic Member
returning arrays
can i do that in C++?
non of my books say anything about it.
thanks
-
Sep 1st, 2001, 02:23 PM
#2
transcendental analytic
What for? Usually it's better to pass a pointer
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 1st, 2001, 08:54 PM
#3
Thread Starter
Fanatic Member
well, i want to write a function similar to the "Split()" function in VB.
-
Sep 1st, 2001, 09:15 PM
#4
Monday Morning Lunatic
I made one pretty quickly using a vector (from the STL), although I could probably rewrite it a bit better.
Look up vector, copy, and iterators in the reference. You should find something. Also I posted code for my original non-optimised Split function which I'm sure you can speed up.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 2nd, 2001, 05:01 AM
#5
transcendental analytic
well parksie, can you pick any iterator_type as well as value_type? 2 input and 4 output iterators and match value would do
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 2nd, 2001, 06:15 AM
#6
Monday Morning Lunatic
I think you can mix&match most things between them as long as they have the same conceptual type (for example you just need to pass an iterator, so any kind of iterator will work).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 2nd, 2001, 06:27 AM
#7
transcendental analytic
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 2nd, 2001, 07:23 AM
#8
Monday Morning Lunatic
Aha you noticed It was before I particularly cared about things like that 
I think the STL docs have an example of this, and once my poor brain gets its act together I can knock something useful up I think.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 2nd, 2001, 08:23 AM
#9
transcendental analytic
Of course, I notice everything 
to be as generalized as possible I'd have two functions, one in style of instr, returning a outputiterator of 2 inputiterators, then have one that copies over the intial part to a container/array in conjunction with instr, then use that iteratively to fill a container/array
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 2nd, 2001, 09:49 AM
#10
Thread Starter
Fanatic Member
uhh im lost.
im putting them into a header file. so far i have left, right, replace(i dont think it works too well though), instr. i havent done vectors yet, but i guess ill have to now..
thanks
-
Sep 2nd, 2001, 05:37 PM
#11
Before you completely lose it, VC++ does support simpler things.
STL vector templates are fun.
But, vanilla C does support returning arrays. You do it everyday.
PHP Code:
char * myfunc(char *whatever){
char *tmp;
..........
return *tmp ;
}
This returns an array of char, using a pointer
long *myfunc(long *stuff );
{
return &long[0];
}
This returns an array of longs., And so on.
-
Sep 2nd, 2001, 07:35 PM
#12
Thread Starter
Fanatic Member
oh
soo if i do
Code:
//main:
char returnstuff[];
returnstuff=stuff("hello");
char *stuff(char *blah){
char otherstuff[]=blah;
return *otherstuff;
}
//crappy example but that would return an array?
-
Sep 3rd, 2001, 09:59 AM
#13
Thread Starter
Fanatic Member
but in jims example thats what it shows, i think
thanks for the help btw
-
Sep 3rd, 2001, 10:08 AM
#14
transcendental analytic
if you allocate the array on the heap it won't run out of scope, but on the other hand it leaves the possibility the client could forget to delete it, so from a OOP perspective returning an array on the heap is bad programming practice. You should pass an array instead, and then if you want, return a reference to it
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 4th, 2001, 07:07 AM
#15
jims example has a typo:
Code:
char * myfunc(char *whatever){
char *tmp;
..........
return *tmp ;
}
it should read
return tmp;
and one can only hope the string in tmp does not run out of scope
in nabeels code is the problem that
char returnstuff[];
has a range of 0, therefore is not able to save anything.
My recommentdation is to pass a pointer to a pointer that will receive the starting address of the array. Or pass a pointer that will be filled (has to have memory associated before).
You could also implement the split function using a callback function, but that is complicated.
I'd use this prototype:
Code:
void Split(char* str, char** arStrings, int iMaxStrings, char* delimiter = " ");
I think count and compare are not really needed.
What you have to do is allocate an array of strings and call the function. iMaxStrings is the size of the array. In the function, use strcpy to fill the array.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 4th, 2001, 09:44 AM
#16
Thread Starter
Fanatic Member
alright, im trying that.
thanks
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
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
|