3

C++ noob here. I have a very basic question about a construct I found in the C++ book I am reading.

// class declaration
class CStr {
  char sData[256];
 public:
  char* get(void);
};

// implementation of the function
char* CStr::get(void) {
  return sData;
}

So the Cstr::get function is obviously meant to return a character pointer, but the function is passing what looks like the value (return sData). Does C++ know to return the address of the returned object? My guess would have been that the function definition would be return &sData.

6
  • 4
    Please get a better book, there is a pretty exhaustive list here stackoverflow.com/questions/388242/… Commented Oct 9, 2012 at 8:36
  • Hmm, this one is called C++ in Plain English by Brian Overland, and on the whole is very readable... And in his defense, he did make it clear that array names return the address of the first element earlier in the book, I just didn't put 2 and 2 together. Commented Oct 9, 2012 at 9:48
  • 2
    This is still really ugly C++ code. Why isn't the author using std::string ? Or is this a book primarily about C++ on Windows? Commented Oct 9, 2012 at 16:20
  • I also omitted the call to the string library #include<string> since it wasn't necessary for the function I included in my query. That said, it /is/ a book about C++ on Windows. Commented Oct 10, 2012 at 1:19
  • 1
    This code is ugly mainly because pointer to internal, private data is exposed outside the class. And well, it is ugly since sData isn't explicitly declared as private either.
    – user29079
    Commented Oct 10, 2012 at 14:00

3 Answers 3

10

For C and C++, array's degrade to pointers. An array cannot be returned as the value of a function, only a pointer can be returned. Returning the array is equivalent to returning the address of the first element of the array:

return &sData[0];
3
  • 1
    Ah yes, the book did say that when introducing arrays. Thanks. Commented Oct 9, 2012 at 4:27
  • 1
    For more information, read section 6 of the comp.lang.c FAQ. It applies to C++ as well (except that in C++ you're more likely to use a container class such as a vector). Commented Oct 10, 2012 at 3:54
  • 6.2 is exactly the followup question I had. Thanks! Commented Oct 10, 2012 at 11:27
2

Does C++ know to return the address of the returned object?

In this case, yes. An array's name is a constant pointer to the first element in the array. So, when

char* CStr::get(void) {
    return sData;
}

is executed, pointer to the first element of the array is returned. So, the caller can reference the array and do a traversal. Run the following code. You'll see the output "hello, world".

#include <iostream>
#include <string>

class CStr {
    char sData[256];
public:
    CStr(std::string str);
    char* get(void);
};

CStr::CStr(std::string str) {
    if (str.length() >= 256) {
        sData[0] = '\n';
        return;
    }

    str.append("\n");
    str.copy(sData, str.length());
}

char* CStr::get(void) {
    return sData;
}

int main() {
    CStr msg("hello, world");
    char* str = msg.get();

    while (*str != '\n')
        std::cout << *str++;

    return 0;
}
5
  • @kevincline Jeez, give a guy a break
    – James
    Commented Oct 9, 2012 at 19:04
  • @kevincline I've replaced strcpy() with std::string::copy().
    – theD
    Commented Oct 10, 2012 at 3:19
  • 2
    @kevincline: Don't use strncpy() unless you're sure it's what you want. Commented Oct 10, 2012 at 3:55
  • 1
    @KeithThompson Excellent article. Would be perfect as a FAQ on SO :) Since there seems to be quite a lot of brainwashed people mindlessly preaching for strncpy there.
    – user29079
    Commented Oct 10, 2012 at 14:07
  • When setting the string, if the length is >= 256, you're just throwing it away without any notice?
    – ott--
    Commented Dec 27, 2012 at 11:31
-1

In C and C++ returning the name of an array is same as returning the Address of the first element in an array hence you can return the same address in three different ways :

  1. return sData;
  2. return &sData;
  3. return &sData[0];

all the three statements return the same address.

1
  • They don't. 1 and 3 is the same but 2 will return something different. Commented Aug 5, 2013 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.