Thread: Return to a char[]

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Cool Return to a char[]

    If I have a variable initialised as so...

    Code:
    char name[50];
    I want to use a function which takes in say a full name and returns it to this variable.

    I appreciate that it is not possible to pass or return an array.

    However, is there anyway I can return a string from a function into that exact variable 'name' (which is defined as above)


    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can pass the array (it decays to a pointer) to the function and let the function fill it with the full name. You would also need to pass the size of the array unless you intend to fix it at 50.

    However, have you considered using std::string instead?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Thumbs up

    I can do it with string and it is all very smart and nice.

    Its just I seen this question which wants it to be returned into the char array. So I'm interested to see how/if this would work.

    Thanks for your reply.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Quote Originally Posted by laserlight View Post
    You can pass the array (it decays to a pointer) to the function and let the function fill it with the full name. You would also need to pass the size of the array unless you intend to fix it at 50.

    However, have you considered using std::string instead?
    Also,

    What wouldI return from the function?

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    void? bool to indicate success? char* to the beginning of the same array (for convenience)?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Its just I seen this question which wants it to be returned into the char array. So I'm interested to see how/if this would work.
    If you are looking for this kind of syntax:
    Code:
    name = foo();
    then no, it will not work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Code:
    void GetName(char * Name)
    {
    cin >> Name;
    }
    Now your Name from wherever you passed it will be changed to whatever is input. Keep in mind there is no size checking here.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Okay,

    Please clarify...

    I cannot return a string into the char[]???

    ie) name = func(...)

    Thanks

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by strokebow
    Please clarify...

    I cannot return a string into the char[]???

    ie) name = func(...)
    Yes. In fact, I thought you already knew that you could not assign to an array since you noted that "it is not possible to pass or return an array".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM