Thread: Char array getter

  1. #1
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38

    Char array getter

    Hey all,

    I am writing a program using cstrings only and I was wondering if this would be the correct way to make a getter for a char array.

    Code:
    typedef char Name[MAX_LENGTH + 1];
    
     Name last;
    
    // Get the last name of the passenger
       Name GetLastName()
       {
          return last;
       }
    Thanks in advanced!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No, you can't return an array, even if you typedef it.

    Since you posted on C++, the best thing to do is use std::string.

    But in C, the only useful way is to pass a pointer to a char array, where you want the answer to be stored, along with a length parameter.
    fgets() is a really good model for this style of interface.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Unfortunately, no. You can't return arrays (of chars or of anything) directly as a return value. You can return a pointer, which works as long as you can guarantee that the object will remain in scope. For example, this will work
    Code:
    typedef char Name[MAX_LENGTH + 1];
    
    Name names[10];
    
    char *getName(int i) {
        return names[i];
    }
    But this will not, because the array will have gone out of scope after the function returns.
    Code:
    char *getName(int i) {
        Name name;
        return name;
    }
    It is syntactically valid but you'd probably get segmentation faults when running it. Your array-returning syntax will be a syntax error, though. Have you tried it?
    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.

  4. #4
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    So is there a way to do the GetLastName function with cstrings or is that just impossible?

  5. #5
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    I have not tried it because it is causing other problems later in my program that is why I am wondering if it would work.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, like I said. You can return a pointer -- which is like an array in many respects, but you need to ensure the lifetime/existence of the variable you are returning. There are many ways to do that, including just creating the data as an ordinary array outside the function as a global/static/class member variable, or simply passing it into the function; or you can use dynamic memory allocation. But if you're going to all that effort, std::string would really be easier. By the way, you can convert an std::string to a C-string at any time with the .c_str() function. There's very little need to use C-strings directly in C++.
    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.

  7. #7
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Well the truth is it is a programming assignment teaching us how to use cstrings and cctypes. That is the reason I cannot use strings

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well perhaps you can tell us more about what the assignment requires, and why you think you need a "char array getter".

    [edit] Post #8000! [/edit]
    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.

  9. #9
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Well it is maintenance on an older program that we used and I need different things like enumeration new data types and other different things. The reason I think I need this char array getter is because the last time I wrote this program I made this last name getter so last could be received by other classes. The reason for this is because last is private.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Perhaps you should see where the getter is being called to see if the functionality belongs in the class that knows the name. In any case, if the Name is declared within a class then a member function of that class can safely return a pointer to the char array, since the array will be in scope as long as the class is in scope.
    Code:
    typedef char Name[BUFSIZ];
    class X {
    private:
        Name name;
    public:
        char *getName() const { return name; }
    };
    If you have an array, just stating the name of the array is enough to get "&array[0]", i.e. a pointer to the first element.
    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.

  11. #11
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    But if I do char getName() it will not be able to fit in all the characters of the name. Correct? Or I am missing something?

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There's a difference between returning a single char as you've suggested, and returning a char* (which is a pointer to chars). A char* pointer just points somewhere in memory and says "there are chars here, go wild". It doesn't tell you how many chars there are, you're supposed to know through other means. The standard for C-strings is to have a NULL termination character, so that you can tell where the string ends.

    Most standard functions that deal with strings don't actually take char arrays, they take pointers. (This is the only way to accept as an argument a string of any length.) For example, strlen is declared as
    Code:
    size_t strlen(const char *s);
    which basically means it takes a char* or char pointer as an argument and returns a size_t (an integer type) as a return value.

    Similarly, you can return a char* which points to anywhere within an array of characters (including the beginning), and a calling function can take the char* and start walking through memory treating the data there as a string.

    Try it. You might find that it works.
    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.

  13. #13
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Well I was re-reading the assignment and it has to be void GetLastName( Name lastName ) would that change using the size_t strlen(const char *s); you suggested?

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It looks like that function is passing in an array, expecting the last name to be copied into the variable, e.g.
    Code:
    Name name;
    GetLastName(name);
    So you have one char array inside the class, and one char array passed in as a parameter, and you want to put the data from one into the other. In a word, strcpy.
    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.

  15. #15
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Oh so I will be putting the char array into this getter then I will just have that array copy to another array are you saying?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char array doesn't print the char in the array
    By KiRa11Love in forum C Programming
    Replies: 4
    Last Post: 09-07-2012, 05:52 AM
  2. uincode char array to array<unsigned char,1>^
    By ripspinner in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2009, 05:41 PM
  3. Class getter and setter functions help
    By MasterM in forum C++ Programming
    Replies: 12
    Last Post: 06-16-2009, 08:37 AM
  4. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  5. signed char array to unsign char array.
    By beon in forum C Programming
    Replies: 5
    Last Post: 12-14-2006, 07:19 PM