Results 1 to 16 of 16

Thread: returning arrays

  1. #1

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919

    returning arrays

    can i do that in C++?

    non of my books say anything about it.

    thanks

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  3. #3

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    well, i want to write a function similar to the "Split()" function in VB.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    well i found this
    http://forums.vb-world.net/showthrea...ht=vector+copy
    but it's not templatized
    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.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  10. #10

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    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

  11. #11
    jim mcnamara
    Guest
    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 charusing a pointer

    long 
    *myfunc(long *stuff );
    {
          
          return &
    long[0];
    }
    This returns an array of longs.,  And so on

  12. #12

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    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?

  13. #13

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    but in jims example thats what it shows, i think


    thanks for the help btw

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  16. #16

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    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
  •  



Click Here to Expand Forum to Full Width