Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
4  
Please get a better book, there is a pretty exhaustive list here stackoverflow.com/questions/388242/… –  Benjamin Bannier Oct 9 '12 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. –  fg nu Oct 9 '12 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? –  kevin cline Oct 9 '12 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. –  fg nu Oct 10 '12 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 Oct 10 '12 at 14:00
show 1 more comment

3 Answers

up vote 8 down vote accepted

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];
share|improve this answer
    
Ah yes, the book did say that when introducing arrays. Thanks. –  fg nu Oct 9 '12 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). –  Keith Thompson Oct 10 '12 at 3:54
    
6.2 is exactly the followup question I had. Thanks! –  fg nu Oct 10 '12 at 11:27
add comment

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.

share|improve this answer
    
They don't. 1 and 3 is the same but 2 will return something different. –  Kris Van Bael Aug 5 '13 at 22:08
add comment

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;
}
share|improve this answer
    
@kevincline Jeez, give a guy a break –  James Oct 9 '12 at 19:04
    
@kevincline I've replaced strcpy() with std::string::copy(). –  Shuvo Oct 10 '12 at 3:19
2  
@kevincline: Don't use strncpy() unless you're sure it's what you want. –  Keith Thompson Oct 10 '12 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 Oct 10 '12 at 14:07
    
When setting the string, if the length is >= 256, you're just throwing it away without any notice? –  ott-- Dec 27 '12 at 11:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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