3

I have learned that a pointer points to a memory address so i can use it to alter the value set at that address. So like this:

int *pPointer = &iTuna;

pPointer here has the memory address of iTuna. So we can use pPointer to alter the value at iTuna. If I print pPointer the memory address gets printed and if I print *pPointer then the value at iTuna gets printed

Now see this program

    char* pStr= "Hello !";
cout<< pStr << endl;
cout<< *pStr << endl;
system("PAUSE");
return 0;

There are a lot of stuff I don't understand here:

  1. In "Hello !" Each letter is stored separately, and a pointer holds one memory address. So how does pStr point to all the letters.

  2. Also When I print out pStr it prints Hello !, not a memory address.

  3. And when I print out *pStr it prints out H only not all what pStr is pointing too.

I really can't understand and these are my concerns. I hope someone can explain to me how this works ad help me understand

4
  • 2
    Please get rid of the system("pause"). Commented Aug 3, 2012 at 15:07
  • I don't quite like that article, because a beginner (the most likely audience) will find himself very confused if cin.get() or getchar() won't pause after an input operation. Commented Aug 3, 2012 at 15:14
  • 2
    char* pStr= "Hello !"; is deprecated, use char const*. Commented Aug 3, 2012 at 15:19
  • 1
    @Cubic, Yeah, you have to work around it by using either one or two of them, or you can rely on the non-guaranteed behaviour of cin.sync before calling cin.get. Or you can just use it like a normal console application instead of trying to make it a standalone app. Commented Aug 3, 2012 at 15:30

7 Answers 7

10
  1. "Hello !" is an array of type char const[8] and value { 'H', 'e', 'l', 'l', 'o', ' ', '!', 0 }. pStr is a pointer to its first element; its last element has the value 0.

  2. There is an overload in the iostreams library for a char const * argument, which treats the ar­gu­ment as a pointer to the first element of an array and prints every element until it encounters a zero. ("Null-ter­mi­nat­ed string" in colloquial parlance.)

  3. Dereferencing the pointer to the first element of an array gives you the first element of the array, i.e. 'H'. This identical to pStr[0].

Sign up to request clarification or add additional context in comments.

8 Comments

Great answer. One thing, What do you mean there is an overload in the iostreams library for a char2 argument
@MohamedAhmedNabil: There's simply an definition of an overload namespace std { ostream & operator<<(ostream & o, char const * s); } which is implemented as something like for ( ; *s; ++s) { o << *s} return o;. When you say std::cout << pStr;, that overload is invoked, i.e. the actual call is operator<<(std::cout, pStr).
char const, char const!
@Xeo: Naturally. What did I say?
You missed the string literal type.
|
2

1-) Since pStr points to a char, it actually points to the beginning of an array of a null terminated string

2-) cout is overloaded with a char * argument. It will print out ever character in the string until it reaches the null character

3-) You are dereferencing the pointer to the first element of the character array.

Comments

1

1-) In "Hello !" Each letter is stored seperatly, and a pointer holds one memory adress. So how does pStr point to all the letters.

The letters are stored in that order in each memory cell with an extra final cell holding 0 to mark the end.

2-)Also When i print out pStr it prints Hello ! not a memory adress.

The cout << understands that you are pointing at a string and so prints the string.

3-)And when i print out *pStr it prints out H only not all what pStr is pointng too.

The * means you are askign for the value at that address. cout << knows that the address holds a char and so prints the char.

Comments

1

Your understanding of pointers is correct in all respects.

Your problem is that the << operator has been overridden for various datatypes on a stream. So the standard library writers have MADE the operator << do something specific on any variable of the type char * (in this case the something specific means output the characters at that address until you get to the end of string marker) as opposed to what you expect it to do (print an address in decimal or hex)

Similarly the << operator has been overridden for char to just output a single character, if you think about it for a bit you will realise that *pStr is a dereferenced pointer to a character - that is it is a char - thus it just prints a single char.

2 Comments

What does "overridden" means?
in c++it is possible to overload operator (like + or >>) for specific types: en.wikipedia.org/wiki/…
1
  1. You need to understand concept of strings as well. In C and C++, string is a few characters (char's) located one after another in a memory, basically, 'H','e','l','l','o','\0'. Your pointer holds memory address of the first symbol, and your C++ library knows that a string is everything starting this address and ending with '\0'.

  2. When you pass char* to cout, it knows you output a string, and prints it as a string.

  3. Construction *pStr means "give me whatever is located at address stored in pStr". That would be char - a single character - 'H', which is then passed to cout, and you get only one character printed.

Comments

1
  1. A pointer, *pStr points to a specific memory address, but that memory address can be used not only as a single element, i.e. a char, but also as the beginning of an array of such elements, or a block of memory.

  2. char arrays are a special type of array in C in that some operations handle them in a specific way: as a string. Hence, printf("%s", ... and cout know that when given a char * they should look for a string, and print all the characters until the terminating null character. Furthermore, C provides a string library with functions designed to manipulate such char * as strings.

  3. This behavior is just as you'd expect from your own analysis: de-referencing pStr simply gives you the value at the memory address, in this case the first element of an array of chars in memory.

Comments

0
  1. A pointer to an array (a C style string is an array of char) is just a pointer to the first element of the array.

  2. The << operator is overloaded for the type char* to treat it as a C-style string, so when you pass it a char* it starts at the pointer you give it and keeps adding 1 to it to find the next character until it finds the null character which signals the end of the string.

  3. When you dereference the pointer you the type you get is char because the pointer only actually points to the first item in the array. The overload of << for char doesn't treat it as a string, just as a single character.

Using strings like this is C-style code. When using C++ you should instead use std::string. It is much easier to use.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.