Thread: returning arrays?

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    17

    returning arrays?

    Hi all, I am wondering if anyone can help me as I havent been programming in c++ for a long time and just went back. I did some basic c++ before but kind of lost the touch.

    The problem I am trying to do is return the values of an array of characters. See examples:

    main.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "PPlayer.h"
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	system("cls");
    	PPlayer p;
    	p.addFN("hoang");
    	cout<<p.retFN();
        system("PAUSE");  //<- console pause to see result before exiting
        return EXIT_SUCCESS;
    };

    PPlayer.h
    Code:
    class PPlayer
    {
    	char fname[30];
    	char lname[50];
    	int age;
    	public:
    		char* retFN();
    		void addFN(char[]);
    };

    PPlayer.cpp
    Code:
    #include <iostream>
    #include "PPlayer.h"
    
    char* PPlayer::retFN()
    {
    	return fname;
    }
    
    void PPlayer::addFN(char fn[])
    {
    	strcpy(fname,fn);
    	age = 1;
    	return;
    }

    It's pretty basic, as I am trying to do some test.
    char * is a pointer and returning char* will return the address of the fname instead of the value in the array (I think that's what it does, cant remember my c++ much).

    What i m wondering is, are there any other way of returning say the name enter : hoang
    ?

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    17
    ok now this is wierd.

    I m using Dev C++
    and now the function retFN() works and return teh value instead of the address ..

    anyone help with how that happens?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you trying to return a single character, or the whole string? That is to say:
    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.

    This doesn't make a copy of anything though. It just returns the address of the string. (Unless you just want a single character, then that's by value.)
    Code:
    char foo( char *bar )
    {
        return bar[ 5 ]; // returns one character by value (makes a copy of it)
    }
    
    char *foo( char *bar )
    {
        return bar; // doesn't copy anything, just returns the address
    }
    In the latter case, you're just getting the location of the character. It's up to you to know that it's a string or a single character though. Example:
    Code:
    char *foo( char *bar )
    {
        return bar;
    }
    ...
    
    char baz = 'a';
    char *ptr;
    
    ptr = foo( &baz ); // pass the address to a function, it's returned and stuck in 'ptr'
    This is the difference between strings and simply having a pointer to a character. Now that you're really confused, need anything else?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    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.

  5. #5
    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

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To use the string class you include "string":
    Code:
    #include<string>
    The "cstring" library is for use with all of the C character array "string" functions. (strcpy, strstr, etc etc).
    i get error doing the above for some reason
    It helps to include said error in the future.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In addition to changing your #include to <string>, you must specify the std namespace for strings, either with a using directive or declaration, or by just typing std::string.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    17
    Quote Originally Posted by Daved
    In addition to changing your #include to <string>, you must specify the std namespace for strings, either with a using directive or declaration, or by just typing std::string.

    thank you,

    this is what i was missing as well.

    the error was telling me that string is undeclare.

    but after i included
    Code:
    using namespace std;
    the compiler stop complaining about it.

    and yes, string is easier than char[]

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