Thread: Wrong return type when returning char array as pointer

  1. #1
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294

    Wrong return type when returning char array as pointer

    I am writing a class Player which has several char arrays as private fields. I am trying to write a method which returns an array as a pointer, but doesn't alter the array in any way, thus the const.

    Here is a snippet:

    Code:
    class Player
    {
    private:
       char state[MAX_STATE_CHAR + ONE_VALUE];
       int rating;
       char last[MAX_NAME_CHAR + ONE_VALUE];
       char first[MAX_NAME_CHAR + ONE_VALUE];
       int groupNumber = NEG_ONE;
    
    public:
       char * GetFirst() const
       {
          return first;
       }
    Visual studio is saying that the return type doesn't match. Any advice?
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try declaring the return type as const char*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Thank you laserlight. That makes sense. Otherwise the contents of the array would be alterable, right?
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are promising that the function shall not alter any member variables. By returning a char*, the caller could modify the member variables and you would break your promise.
    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. Confusing array of char pointer type mismatch
    By tkks in forum C Programming
    Replies: 4
    Last Post: 04-21-2013, 11:24 AM
  2. Replies: 1
    Last Post: 11-27-2012, 08:04 AM
  3. Replies: 24
    Last Post: 04-19-2012, 04:51 AM
  4. Replies: 17
    Last Post: 03-06-2008, 02:32 PM
  5. return-type char
    By Ash1981 in forum C Programming
    Replies: 2
    Last Post: 01-15-2006, 07:57 AM