Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

character pointers in C++

Tags:

c++

pointers

I have a doubt regarding character pointers in c++. Whenever we create a character pointer in c++: char *p="How are you doing", p should contain the address of memory location which holds the value "how are you doing".

However, I am perplexed at a sample code and the output. Why cout<<p gives back the entire string? It should give value of a memory address. Secondly, why does cout<<*p gives only first character of the string?

Code:

#include <iostream>
using namespace std;

int main () {

const char *str = "how are you\n";
int i[]={1,2,3};


 cout << str << endl;   // << is defined on char *.
 cout << i << endl;
 cout << *str << endl;

}

OUTPUT:

how are you

0xbfac1eb0

h
like image 843
Frustrated Coder Avatar asked Mar 11 '11 07:03

Frustrated Coder


People also ask

What is character pointer in C language?

A character pointer is again a pointer like the pointers to other types in C. But there is catch here. when you do: char a = 'A'; char *ptr = &a; // ptr points to character 'A' Here ptr is pointer to a character. But when you do: char *str = "Hello"; char *ptr = str; // ptr points to first character of string str.

How do character pointers work?

When using the integer pointer to an array, cout prints the base address of that integer array. But when the character pointer is used, cout prints the complete array of characters (till it encounters a null character) instead of printing the base address of the character array.

What is the syntax for character pointer?

An example of a pointer declaration can be : char *chptr; In the above declaration, 'char' signifies the pointer type, chptr is the name of the pointer while the asterisk '*' signifies that 'chptr' is a pointer variable.

Is char * a pointer?

Char* is a pointer to a character, not to a string.


2 Answers

If you want to print the address, then you've to cast the char* to void*, as

const char *str = "how are you\n"; 
cout << (void*) str << endl; 

In the absense of the cast, cout sees str as const char* (which in fact it is) and so cout thinks you intend to print the null-terminated char string!

Think : if you want coud << str to print the address, how would you print the string itself?

--

Anyway here is more detail explanation:

operator<< is overloaded for char* as well as void* :

//first overload : free standing function
basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& _Ostr, const char *_Val);

//second overload : a member of basic_ostream<>
_Myt& operator<<(const void *_Val);

In the absence of the cast, first overload gets called, but when you cast to void*, second overload gets called!

like image 50
Nawaz Avatar answered Oct 22 '22 15:10

Nawaz


This is due to Operator overloading.

the << operator is overloaded to output the string pointed to by the character pointer.

Similarly, with *p, you will get the first character, hence you get the first character as output.

like image 40
Shamim Hafiz - MSFT Avatar answered Oct 22 '22 15:10

Shamim Hafiz - MSFT