Thread: returning arrays?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What's the problem? Other than missing the #include <cstring> to access strcpy, your code is fine and works. You cannot return an array, you can only return a pointer like you did. Functions that use the char* pointer as a C style string wil work as long as it is null-terminated, which it is by strcpy in this case.

    IMO you should just use C++ strings instead. They are easier to work with and less prone to errors.

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    17
    Are you trying to return a single character, or the whole string? That is to say:
    Quote:
    char * is a pointer and returning char* will return the address of the fname instead of the value in the array
    Do you mean you just want one character, or the whole thing? If you just want the whole thing, returning a pointer to a character is what you want. Otherwise just return a single character and specify what character in the array you want to return.
    Yes I am trying to return the whole array. At first when I had teh code above complie and run it return me an address (229...). I save close poject and reopen it and recompile and run and it return me the array' s value.

    What's the problem? Other than missing the #include <cstring> to access strcpy, your code is fine and works. You cannot return an array, you can only return a pointer like you did. Functions that use the char* pointer as a C style string wil work as long as it is null-terminated, which it is by strcpy in this case.
    Is there anyway to make a class hold string values?
    example:
    Code:
    #include <cstring>
    class a
    {
         string b;
    }
    i get error doing the above for some reason

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning Arrays as pointers
    By chinook86 in forum C Programming
    Replies: 7
    Last Post: 04-03-2007, 04:51 PM
  2. Passing & Returning Arrays
    By jeffdavis_99 in forum C++ Programming
    Replies: 10
    Last Post: 04-02-2005, 06:44 PM
  3. Functions returning char arrays
    By turmoil in forum C Programming
    Replies: 3
    Last Post: 05-27-2003, 01:43 AM
  4. Returning Arrays
    By mr_spanky202 in forum C Programming
    Replies: 2
    Last Post: 04-06-2003, 02:57 PM
  5. returning arrays from functions
    By Leeman_s in forum C++ Programming
    Replies: 11
    Last Post: 06-05-2002, 10:00 PM