gopala krishna
gopala krishna

Reputation: 81

Char Pointers in C++

I was told that strdup returns the address where the string is stored. So in the program I have mentioned, to print the string, I thought that I have to use the dereferencing operator *, but if I do that it prints only the first character and without using the dereferencing operator, I am getting the whole string. I can not wrap my head around when to use the dereferencing operator.

Same is with directly assigning the const char.

When I checked it with int, it prints the address and not the value. Why the difference.

Program:

#include <iostream>
#include <string.h>
using namespace std ;

int main()
{
    char* num = strdup("Adam ");
    cout << num << endl ;
    char *n = "Eve ";
    cout << n << endl;

    int *m = (int*)670;
    cout << m << endl ;
    cout << *m << endl;
}

Upvotes: 0

Views: 109

Answers (2)

eerorika
eerorika

Reputation: 238351

to print the string, I thought that I have to use the dereferencing operator *

You thought wrong. char* is a pointer to a character. If you dereference a pointer to a character, the result is the character object that is pointed at.

but if I do that it prints only the first character and without using the dereferencing operator, I am getting the whole string

This is because of how the overload of the stream insertion operator of a character stream that accepts a pointer to a character is specified to behave. The operator will assume that it is a pointer to the first character of a null terminated string, and the specified behaviour is to print the entire string.

When I checked it with int, it prints the address

This is because the insert operator is not specified to print an entire null terminated character string when given a pointer to an integer. After all, a pointer to an integer won't be pointing to a character of a string.

There is no separate overload for pointers to integer. There is one overload that accepts all pointers (except character pointers, which have the separate overload): The overload for void pointers - all data pointers implicitly convert to void pointers. The specified behaviour of inserting a (other than character) pointer into a stream is to print the address that is being pointed at.

Why the difference.

Because pointers to characters are special. In string processing contexts, they're nearly always assumed to be pointers to a first character of a null terminated string. This is an idiom/convention dating back to the C language, which C++ was originally derived from.


PS. *m has undefined behaviour, since you never created an int object at the address 670. Furthermore, the program leaks the memory that strdup allocated.

Upvotes: 1

john
john

Reputation: 87959

In C there really are no strings. The address where the string is stored is the string.

It's confusing because in C++ there are strings, so you have C++ strings and C strings both of which are legal in a C++ program. If you have the address of a C++ string you really would have to use the dereference operator (as you do with an int).

Upvotes: 1

Related Questions