Thread: Character Array functions.

  1. #1
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079

    Character Array functions.

    Can I make a funtion that is a character array? What is the proper syntax?

    I tried this but it didn't work.

    Code:
    char Foo[50]  ();
    
    //Int Main was here with all sorts of stuff.
    
    char Foo ()
    {
    RETURN "HELLO WORLD";
    }

  2. #2
    High Flyer stumpster123's Avatar
    Join Date
    Mar 2005
    Posts
    26
    I would say you can't do this because it is a function not a data type and is not accessed like a data type.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you'd have to use character pointers:
    Code:
    #include<iostream>
    
    char*foo();
    
    int main()
    {
        char*out=foo();
        std::cout<<out;
        std::cin.get();
        return 0;
    }
    char*foo()
    {
              return "Hello World";
    }
    just be sure to look into pointers so you don't introduce memory leaks into your program.
    Last edited by major_small; 03-29-2005 at 11:55 PM. Reason: warning: memory leak possible
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Can I make a funtion that is a character array?
    A comment on terminology:
    A function performs some task. A function can accept data and peform some operation on that data, and a function can return data. But a function is not a character array, it is a function. It can accept a character array as data, or it can return a character array, but the function itself cannot be a character array.

    It seems like a function should be able to return a character array because you can do this:
    Code:
    #include<iostream>
    using namespace std;
    
    void display(char str[])
    {
    	cout<<str<<endl;
    }
    
    int main()
    {
    	
    	char my_str[] = "hello world";
    	display(my_str);
    
    	return 0;
    }
    The function is able to accept a character array as a parameter.

    But, you cannot do this:
    Code:
    #include<iostream>
    using namespace std;
    
    char[] display(void)
    {
    	return "hello world";
    }
    
    int main()
    {
    	
    	cout<<display()<<endl;
    
    	return 0;
    }
    The function is unable to return a character array.

    However, what you need to realize is: an array name is really a pointer, and for whatever reason, the return type will not let you use array notation as a substitute for pointer notation.
    Last edited by 7stud; 03-30-2005 at 02:07 PM.

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by SlyMaelstrom
    Can I make a funtion that is a character array? What is the proper syntax?

    I tried this but it didn't work.

    Code:
    char Foo[50]  ();
    
    //Int Main was here with all sorts of stuff.
    
    char Foo ()
    {
    RETURN "HELLO WORLD";
    }
    That is wrong because if you returned "HELLO WORLD" you would be returning the address of that string. Plus even if you changed the function header to say char* Foo(), you would still be returning the address to a local variable (I think this is correct) which is a big no-no.

    Just to make sure I got this right, returning a string literal would be the same as returning an address to a local char array, right? The address wouldn't contain anything useful if used in the calling function, right?

    EDIT: Shoot, I tested the returning of the address of the string literal and then displaying the string and it worked. Could someone please explain why this happens? However, it doesn't work when I try to return the address of a regular array. Even setting it up like this worked:

    Code:
    #include <iostream>
    
    using namespace std;
    
    char* charArrayFunc()
    {
        
        char* dookey = "Hello World!";
        return dookey;
    }
    
    int main()
    {
        char* fooFoo = charArrayFunc();
        cout<<fooFoo<<endl;
        cin.get();
        return 0;
    }
    Why is this?

    Alright this is ridiculous! I even did this and it still works! What is up with it?! This is going against everything I've ever learned about local variables!

    Code:
    #include <iostream>
    
    using namespace std;
    
    char* charArrayFunc()
    {
        char dookey = 'K';
        return &dookey;
    }
    
    int main()
    {
        char* fooFoo = charArrayFunc();
        cout<<*fooFoo<<endl;
        cin.get();
        return 0;
    }
    Last edited by homeyg; 03-30-2005 at 04:30 PM.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Shoot, I tested the returning of the address of the string literal and then displaying the string and it worked. Could someone please explain why this happens?
    RULE #1: Everything is passed by value.

    What that means is that a copy is always made when you send an argument to a function AND when you return a value from a function. So, when you return a string literal, a copy of that address is sent back to main(), and a copy of an address is just as good as the original address. Then, the << operator is programmed to ouput a string instead of the address for the type "pointer to char", which is why you can do this:

    char str[] = "hello world";
    cout<<str<<endl;

    or this:

    char* p = "goodbye";
    cout<<p<<endl;
    Last edited by 7stud; 03-30-2005 at 06:35 PM.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Just to make sure I got this right, returning a string literal would be the same as returning an address to a local char array, right?
    I think the explanation for that goes like this, the original function:
    Code:
    char Foo ()
    {
        return "hello world";
    }
    never declared a named variable, so nothing goes on the stack(i.e. the thinga-ma-jig that keeps track of temporary local variables)--similar to when you use the new operator. Instead, string literals get stored elsewhere in memory, so they are not targets for destruction when the funtion ends. Another characteristic of a string literal is that if another variable in the program is assigned the same string literal, it gets the same address. In that regard, the string literal is a constant, so you can't change it. If you could change the string literal, that would mess up the other variables that were assigned the address of that same string literal.

    Applying those musings to this function:
    Code:
    char* charArrayFunc()
    {
        
        char* dookey = "Hello World!";
        return dookey;
    }
    The string literal is placed in that special area of memory I surmised about earlier, and then its address is assigned to dookey. In the return statement, a copy of the address in dookey is returned to main(). Then, dookey is destroyed when the function terminates, but the unnamed string literal "Hello World!" is safely hidden away somewhere's!

    As for this one:
    Code:
    char* charArrayFunc()
    {
        char dookey = 'K';
        return &dookey;
    }
    ummm...erhhh...I guess char literals are treated the same as string literals.
    Last edited by 7stud; 03-30-2005 at 07:29 PM.

  8. #8
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Yeah but the function with the char literals, I also tried it with a local type int variable, and cout in main was still able to display it.

    Code:
    int* charArrayFunc()
    {
        int dookey = 3444;
        return &dookey;
    }
    
    int main()
    {
        int* whatever = charArrayFunc();
        cout<<*whatever<<endl;
        return 0;
    }
    It still worked!

    Now, what I'm thinking (after I flipped through a few of my C++ books) is that the variable dookey goes out of scope, its former address hasn't been reused yet, so it retains the value it originally contained. That's what I concluded after an hour of reading. The same goes for char variables because I don't think char literals represent their address and aren't treated the same way as string literals.
    Last edited by homeyg; 03-30-2005 at 10:31 PM.

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Homeyg, what compiler are you using? Returning a pointer (or reference) to a local normally generates a warning. As to why that "works" it's one of the charming things about C/C++, there are very few errors when it comes right down to it, what there is a lot of is UNDEFINED BEHAVIOR This can range from a Blue Screen of Death to, far more commonly, doing what you expected. In this case the address is still "there" because addresses are just numbers. Even after your fuction returns whatever was written in memory is probably still there. It's just that it will likely be used for something else the next time you call a function. Even pointers to things that you have deleteed might still have whatever was in there before, and the operating system may still consider that address to be within your program. Then at some point later, when the operating system is looking for more space it may decide that the same address is part of a different program. This is when the ever-popular General Protection Fault/ Segmentation Fault/ Bus Error happens.

  10. #10
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by grib
    Homeyg, what compiler are you using? Returning a pointer (or reference) to a local normally generates a warning. As to why that "works" it's one of the charming things about C/C++, there are very few errors when it comes right down to it, what there is a lot of is UNDEFINED BEHAVIOR This can range from a Blue Screen of Death to, far more commonly, doing what you expected. In this case the address is still "there" because addresses are just numbers. Even after your fuction returns whatever was written in memory is probably still there. It's just that it will likely be used for something else the next time you call a function. Even pointers to things that you have deleteed might still have whatever was in there before, and the operating system may still consider that address to be within your program. Then at some point later, when the operating system is looking for more space it may decide that the same address is part of a different program. This is when the ever-popular General Protection Fault/ Segmentation Fault/ Bus Error happens.
    I use Dev-C++. Thanks for the explaination. That's what I figured the deal was all along.
    Last edited by homeyg; 03-31-2005 at 03:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Character Array - almost? works
    By voltson4 in forum C Programming
    Replies: 3
    Last Post: 03-04-2003, 06:03 PM