-1

Say that in a class student, I had written the following public member function:

char *p() 
{ return (name);}

Now, name is actually a datamember(private section) and is a character string. I assume that this means char* can return a string. If I were writing something similar like:

void main()

{ char *s = "GOODLUCK";

    cout<<*s;
}

What does *s give me here? I think it's the whole string 's'. Am I correct?

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
Karthik
  • 113
  • 1
  • 8
  • please edit Your code to be readable – Jacek Cz Oct 27 '17 at 16:20
  • 2
    No, `*s` is not the whole string. – ForceBru Oct 27 '17 at 16:21
  • At my school, I was told that it was a full string. But I was not sure. Could you please explain? – Karthik Oct 27 '17 at 16:24
  • Did you try compiling that? A good compiler would warn you about assigning a string constant to a character string pointer. – Dave S Oct 27 '17 at 17:00
  • This question indicates something more disastrous occurring to you, a common trend where one would prefer to ask others to perform research on their behalf. It indicates that you are the kind who will ask thousands of questions, all frequently asked by your kind, and all which would be answered by a decent textbook. If only you'd pick up the textbook rather than wasting all of those thousands of minutes typing up questions which already have printed answers, you'd be thousands of minutes ahead in your education. It's not really worth answering a question which the OP won't read, is it? – autistic Oct 30 '17 at 16:37
  • @ Sebivor: Pretty less on time, that's why I can't prefer a textbook.You are pretty rude by the way. – Karthik Oct 31 '17 at 11:06

1 Answers1

2

Description below is far from pedantic, but should help you to start digging into pointers:

  • char *s means, that s is pointer to char. In other words, it is variable, which can store address of area, capable of storing char. BTW I prefer writing char* s (space moved) as more descriptive: s is variable name, char* is variable type.
  • "GOODLUCK" is an array of 9 chars (the last one is '\0'). All array memebers are always kept one after another in memory, so if you know address of the first member, you can find address of the second, the third and so on. That's why arrays in C++ are handled as address of the first member.
  • s = "GOODLUCK"; means "store array address (aka address of the first element) into s". So s now points to the first char of "GOODLUCK".
  • *s means "get data, which variable points at". s points to the first char of string, so *s is simply a G
  • cout << *s; means "calculate *s and pass it to cout", so it's identical to cout <<'G';
  • writing cout << s; (note missing asterisk) doesn't pass one char to cout, but passes pointer to char. cout expects pointer to char to be passed only when it represents address of array, not address of single char. cout will print one by one all members of array, so the whole string will be displayed.

Bottom line: char *p() { return (name);} actually returns address of the first char of the name, but address of the first char represents the whole name. Possible further reading

nnovich-OK
  • 2,900
  • 2
  • 13
  • 16
  • Please note that `"GOODLUCK"` is a [string literal](http://en.cppreference.com/w/cpp/language/string_literal), so that OP should have declared either `const char* p1 = "GOODLUCK";` or `char p2[] = "GOODLUCK";` with different meanings and capabilities. – Bob__ Oct 30 '17 at 16:40
  • I know you mentioned the null termination on the end of the literal, but it's probably also worth making it clear that this is required for things to know when they have reached the end of the string, and it is how cout knows how many characters to print. If you don't have the null at the end, anything trying to read the string will just run off the end and read uninitialised memory until it segfaults or encounters a null by chance. – Sean Burton Oct 31 '17 at 14:17