returning a char* array

This is a discussion on returning a char* array within the C++ Programming forums, part of the General Programming Boards category; Hi all, here is one of my classes, the problem is with the getLog() function. Code: class Class1 { public: ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    returning a char* array

    Hi all,

    here is one of my classes, the problem is with the getLog() function.

    Code:
    class Class1 
    {
    public:
    	Class1(HWND sw);
    	virtual ~Class1();
    	char* getLog();
    	int getAmount();
    private:
    	int kFound;
    	char* log[10];
    };
    The function char* getLog() is supposed to returh the log variable declared in the private section.

    Here is the getLog() body

    Code:
    char* Class1::getLog()
    {
    	return log;
    }
    The error im getting is:

    Code:
    'return' : cannot convert from 'char *[10]' to 'char *'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    I understand the error but dont know how to fix it. I tried changing the signature from char* getLog() to char* [10] getLog() but that complained even more. I know it is something very simple but i havent got a clue because ive never had to do anything like this before.

    Thanks

  2. #2
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    your variable is of a different type from your return value.
    The variable is an array of pointers, the return value is a single pointer.

    returning char** returns a pointer to a pointer to char, which is equivalent to an array of char*.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    C++ has its own string type, e.g.:

    string log;

    If you really want log to be an array, then you can do this:

    string log[10];

    How did you plan to assign something to the log member variable the way you have it now? Array names are constant, so you can't change them, which means you can't do this:

    log = someArray;
    Last edited by 7stud; 01-31-2006 at 04:27 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    you can't do this:

    log = someArray;
    If you want to do that, use strcpy() in <cstring>.

    C++ has its own string type, e.g.:

    string log;
    . . . which is in <string>. The string class overloads several operators and allows things like = (to assign one string to another) and + (to concatenate two strings together).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing and returning array pointers?
    By thealmightyone in forum C Programming
    Replies: 26
    Last Post: 03-26-2009, 03:38 PM
  2. Returning an Array
    By mmarab in forum C Programming
    Replies: 10
    Last Post: 07-19-2007, 07:05 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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