Return to a char[]

This is a discussion on Return to a char[] within the C++ Programming forums, part of the General Programming Boards category; If I have a variable initialised as so... Code: char name[50]; I want to use a function which takes in ...

  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
    22,306
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,306
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,306
    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".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07:57 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  4. Replies: 7
    Last Post: 06-16-2006, 10:23 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21