function returning char array help

This is a discussion on function returning char array help within the C++ Programming forums, part of the General Programming Boards category; the problem here is that i have an array of chars Code: char exampleArray[50]; and i want to create a ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    34

    function returning char array help

    the problem here is that i have an array of chars

    Code:
    char exampleArray[50];
    and i want to create a function that returns this array so something like

    Code:
    char getCharArray() { return exampleArray; }
    but this wont work as it cannot convert from char[30] to char any ideas?

    Many Thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You cannot return an array from a function by value. You can return it by reference though. But then you would be using a local variable that had already gone out of scope. Either malloc it in the function and free it outside the function, or pass an array to the function by reference.
    Last edited by robwhit; 09-18-2008 at 08:28 AM.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    ...or just use an std::vector.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    Sorry im not that experience with c++ here is my code that im trying to get working.

    Code:
    class charArray
    {
    	public:
    		charArray()
    		{
                                          memset( sampleArray, " ", 30);
    		}
    
    		void setSampleArray(char x[]){ strcpy( sampleArray,  x); } 
                                    
                                    char getSampleArray() {  return sampleArray; } // << problem
    
    
    	protected:
    		char sampleArray[30];
    
    };

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Wouldn't it be possible to do:

    Code:
    const char* getSampleArray() const { return sampleArray; }
    A char and a pointer-to-a-bunch-of-chars is a different thing.
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As stated, C does not allow assignment of arrays, and returning an array that you can't assign to something is pretty pointless [what I'm trying to say, is that arrays can not be returned, because you can not "give" the returned array to anything].

    You can do other things, such as return the address of it, or you can pass in an array to copy it into, or even create a copy dynamically using new and free it with delete. Which is best for you depends on what you want to do with the array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Isn't this:
    Code:
    const char* getSampleArray() const { return sampleArray; }
    what you want?

    Won't these work?
    Code:
    charArray ca;
    char *arr;
    arr = ca.getSampleArray();
    cout << arr[0] << arr[1]; //outputs two spaces
    or

    Code:
    charArray ca;
    char array[30];
    strcpy (array, ca.getSampleArray());
    cout << arr[0] << arr[1]; //outputs two spaces
    Of course you could use string instead of char[]

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    Code:
    const char* getSampleArray() const { return sampleArray; }
    worked for me thank you very much for your replies.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why not use an std::string instead of a char array?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
     memset( sampleArray, " ", 30);
    that is wrong, " " is the address of a string that holds a space, terminated with a zero. That is almost certainly not matching what you actually want to fill your string with [and you should get a warning or error for it].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    Quote Originally Posted by matsp View Post
    As stated, C does not allow assignment of arrays...
    C? Since when is this C?
    Anyway, to point something else out.
    You have an array of chars, char[30], and then try to return char. See the problem? The types do not match. You are telling the compiler the function is returning something else than you are actually returning.
    That mentioned, a solution was already given, so you don't need to change anything. Just giving you some information on why it failed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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