Char array getter

This is a discussion on Char array getter within the C++ Programming forums, part of the General Programming Boards category; Well, given the prototype "void GetLastName( Name lastName )", it seems reasonable to expect that the name will be copied ...

  1. #16
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well, given the prototype "void GetLastName( Name lastName )", it seems reasonable to expect that the name will be copied into lastName. So, yes. Try it and see what happens.
    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.

  2. #17
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Well this seems like it works.
    Code:
    void GetLastName(Name lastName)
       {
          strcpy(last,lastName);
       }
    But I don't see the reason for a parameter in a getter.

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    This isn't really a getter function per se. It's more like a retrieval function or something. It's a getter but because of language syntax you have to pass in an argument and have that filled in, rather than returning the value. Same thing if you'd wanted to return two values: functions are only allowed to return one value so you might have to do
    Code:
    void getTwoValues(int &returnValue1, int &returnValue2) {
        returnValue1 = 12;
        returnValue2 = 55;
    }
    since "(int, int) getTwoValues()" isn't valid.

    Like I said, you could write your particular getter without a parameter by returning a pointer instead, but if you're just learning about arrays now then the instructor might not have wanted to go into details about having to preserve the scope of the variable you're returning, etc.
    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. #19
    Registered User XNOViiCE's Avatar
    Join Date
    Nov 2012
    Location
    'merica
    Posts
    38
    Okay I think I understand it all now! Thanks for all the help I think I would have been stuck for a long time if you didn't help! We have learned arrays just he likes making things difficult for us that's all!

Page 2 of 2 FirstFirst 12
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, 04: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, 11:36 AM
  5. signed char array to unsign char array.
    By beon in forum C Programming
    Replies: 5
    Last Post: 12-14-2006, 06:19 PM

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