Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Make a function that returns Char Arrays

  1. #1
    Join Date
    Nov 2006
    Location
    Kosovo
    Posts
    448
    Rep Power
    28

    Make a function that returns Char Arrays

    Hello CodeCall, I MISSED YOU ALL. My uncle got married a couple of days ago so I couldn't visit CC these days. Talked too much, back on thread.

    I was learning something about functions in C++ and I wanted to try something myself. I wanted to create a function that would return a char array but I got tons of errors. Could anyone please tell me how can I do that in an other way. Use CString?

    Looking forward for reply

    Wish you all the best,
    Dren

    PS: My Daddy was about to kill me when he heard that I wanted to fire an AK on my Uncles wedding lol

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: Make a function that returns Char Arrays

    How about a std::string?

  4. #3
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42

    Re: Make a function that returns Char Arrays

    I would also say that a C++ string (std::string) is the best choice, especially now when you're working with C++ anyway.

  5. #4
    Join Date
    May 2008
    Posts
    2,126
    Blog Entries
    1
    Rep Power
    33

    Re: Make a function that returns Char Arrays

    Check this out.

    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    char * Return_Phrase(char * array);
    
    int main() {
        char * return_value;
        char phrase[300];
    
        cout << "Enter a phrase: ";
        cin.getline(phrase, 299);
        
        return_value = Return_Phrase(phrase);
        cout << endl << return_value;
        getch();
    
        return 0;
    }
    
    char * Return_Phrase(char * array) {
         return array;
    }
    This should give you an idea of how to pass an array to a function, and then return it. You are going to have to use pointers in order to do the job.

    And yes I used <conio.h>, because although not standardized it is still a popular library.
    I will also take a direct quote from this site in one of the stickied threads for anyone who has a problem with me using it.

    From the C/C++ Resources thread...
    Is it okay to use conio.h? ...
    ... and questions alike regarding platform-dependent libraries are not really questions for us, but more for yourself.
    So yeah, apparently it is okay to use conio.h on this site, well if you would have read stickied threads you would have known that.

    Anyways hopefully this code will help you out.

  6. #5
    Join Date
    Nov 2006
    Location
    Kosovo
    Posts
    448
    Rep Power
    28

    Re: Make a function that returns Char Arrays

    Thank you all, especialy you MeTh0Dz|Reb0rn, your post helped me so much. I still dont know how to use std::string, that std:: looks so weird indeed.

    Thanks again,

    CodeCall #1,
    Dren

  7. #6
    LogicKills's Avatar
    LogicKills is offline Programmer
    Join Date
    May 2008
    Location
    US
    Posts
    138
    Rep Power
    16

    Re: Make a function that returns Char Arrays

    Quote Originally Posted by Dren View Post
    Thank you all, especialy you MeTh0Dz|Reb0rn, your post helped me so much. I still dont know how to use std::string, that std:: looks so weird indeed.

    Thanks again,

    CodeCall #1,
    Dren
    Well you don't have to use it as std::string, that is only if you don't declare 'namespace std' globally..
    Like:



    Code:
    #include <iostream>
    
    int main()
    {
    std::cout << "Hello World" << std::endl;
    return 0;
    }
    or
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    int main()
    {
    cout << "Hello World << endl;
    return 0;
    }
    or
    Code:
    #include <iostream>
    using namespace std; // globally declare the std or standard library
    
    int main()
    {
    cout << "Hello World" << endl;
    return 0;
    }
    http://logickills.org
    Science - Math - Hacking - Tech

  8. #7
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Make a function that returns Char Arrays

    Many dudes prefer NOT doing the "using namespace std" thing globally (that polluting namespaces etc), so it could be a nice habit to put std:: in front of those functions (cout etc) that need it, and not putting "using namespace..." on the top. To use strings, #include <cstring>, and you have a data type named "string" to use:
    Code:
    #include <cstring>
    
    std::string DoSomethingWithThatString(std::string str); // Function declaration
    
    std::string myDearString = ""; // Global, initialized string. Notice "std::" if you're not having "using namespace.."
    
    /* MAIN */
    int main()
    {
      using namespace std; // Now this is OK, but will only affect stuff within function
     
      string mySecondString; // Local, uninitialized string. Notice, no "std::", because of "using namespace.."
    
      mySecondString = DoSomethingWithThatString(myDearString); // Yup you probably get it :D
    
      return (EXIT_SUCCESS); // Has nothing to do with strings
    }
    
    /* FUNCTION RETURNING STRING */
    std::string DoSomethingWithThatString(std::string str)
    {
      // Do something with the string object (str) here if ya want...
      return str; // Returns input
    }
    The string variable is an object, having its own methodz (yeah...) and stuff, like myString.length etc...
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

  9. #8
    directoryxd is offline Newbie
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Re: Make a function that returns Char Arrays

    I am very weak in this issue

  10. #9
    Join Date
    May 2008
    Posts
    2,126
    Blog Entries
    1
    Rep Power
    33

    Re: Make a function that returns Char Arrays

    Quote Originally Posted by directoryxd View Post
    I am very weak in this issue
    Okay? Go read a tutorial online. Or study the code I posted above.

  11. #10
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Make a function that returns Char Arrays

    Don't worry, there are even free e-books on the subject!
    "Learn to program with C++" (John Smiley), for example... Or "Thinking in C++" 1 and 2...
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. strcmp() function returns a negative number if...
    By jackson6612 in forum C and C++
    Replies: 1
    Last Post: 10-21-2011, 05:07 AM
  2. Replies: 4
    Last Post: 04-18-2011, 07:13 PM
  3. function that takes a string and returns a percent value
    By mdbr in forum General Programming
    Replies: 7
    Last Post: 10-19-2010, 11:38 AM
  4. Replies: 3
    Last Post: 03-11-2010, 12:56 AM
  5. Function returns nothing.
    By shibbythestoner in forum PHP Development
    Replies: 14
    Last Post: 12-21-2007, 01:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

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