Unusual behaviour with character pointers
Last Updated :
03 Nov, 2020
In C++, cout shows different printing behaviour with character pointers/arrays unlike pointers/arrays of other data types. So this article will firstly explain how cout behaves differently with character pointers, and then the reason and the working mechanism behind it will be discussed.
Example 1:
C++
// C++ program to illustrate difference
// between behaviour of integer pointer
// and character pointer
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Integer array
int a[] = { 1, 2, 3 };
// Character array
char ch[] = "abc";
// Print the value of a and b
cout << a << endl;
cout << ch << endl;
return 0;
}
Output:
0x7ffc623e56c0
abc
Explanation:
From the above code, it is clear that:
- 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.
Example 2:
C++
// C++ program to illustrate behaviour
// of character pointer pointing to
// character array
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Character array b
char b[] = "abc";
// Pointer to character array
char* c = &b[0];
// Print the value of c
cout << c << endl;
}
Explanation:
In this example as well, the character type pointer c is storing the base address of the char array b[] and hence when used with cout, it starts printing each and every character from that base address till it encounters a NULL character.
Example 3:
C++
// C++ program to illustrate difference
// between behaviour of character and
// character pointer
#include <iostream>
using namespace std;
// Drive Code
int main()
{
char c = '$';
char* p = &c;
cout << c << endl;
cout << p << endl;
}
Output:
Explanation:
In the above example, c is a simple character variable and it prints the value stored in it as expected. p being a character pointer when used with cout, results in the printing of each and every character till a null character is encountered. Thus, some garbage value is being printed after '$'. This simply means that in the memory the null character was placed after the 'a' character (since in this case, there is no automatic storing of a null character after the useful piece of data is finished unlike character arrays) and so it stops printing and gives the obtained output.
Reason behind unusual behaviour with character pointers:
The reason for this lies in the concept of Operator Overloading. '<<' operator is overloaded for different types of inputs. In the case of const void* overload, it prints just the address. But for const char* overload, it starts printing each and every character till it encounters a null character (treating the input as C — style string).
Example 4:
Â
C++
// C++ program to illustrate
// printing of character array
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Character array
char c[] = "abc";
// print value of c, c[0] and *c
cout << c << endl;
cout << c[0] << endl;
cout << *c << endl;
}
Explanation:In the above example:
- Only 'c' with cout is treated as const char * and << operator overload for such input is called and thus every character is printed until null character.
- When using c[0], i.e., *(c + 0), it simply dereferences the particular memory location and prints the value stored at that location only.
- Similarly, for *c which is same as *(c + 0).
NOTE: The unusual behaviour can be rectified if the output is type casted into something which will not be treated as C-style string.
Example 5:
C++
// C++ program to illustrate behaviour
// of typecasted character pointer
#include <iostream>
using namespace std;
// Driver Code
int main()
{
char c[] = { "abc" };
char* b = c;
cout << (void*)b;
}
Explanation:
In the above example, the base address of the character array is obtained in the output.
Utilization of unusual behaviour with character pointers:
Given a string, print the pattern as shown in the following example:
C++
// C++ program to illustrate the
// utilization of unusual behaviour
// of character pointer
#include <iostream>
using namespace std;
// Function that prints the pattern
void printPattern(char* ch)
{
// Base Condition
if (*ch == '\0')
return;
// Recursion function call after
// excluding the current character
printPattern(ch + 1);
// Print the whole string starting
// from base address stored in 'ch'
cout << ch << endl;
}
// Driver Code
int main()
{
char ch[] = { "abcd" };
// Function Call
printPattern(ch);
return 0;
}
Similar Reads
Print a character n times without using loop, recursion or goto in C++ Given a character c and a number n, print the character c, n times. We are not allowed to use loop, recursion, and goto. Examples : Input : n = 10, c = 'a'Output : aaaaaaaaaa Input : n = 6, character = '@'Output : @@@@@@ In C++, there is a way to initialize a string with a value. It can be used to p
2 min read
iscntrl() in C++ and its application to find control characters In C++, iscntrl() is a predefined function used for string and character handling. cstring is the header file required for string functions and cctype is the header file required for character functions. A control character is one which is not a printable character i.e, it does not occupy a printing
3 min read
A C/C++ Pointer Puzzle Prerequisite: Pointers Assuming the size of int = 4 bytes, size of a pointer variable = 8 byte, what will be the output of following program. Few hints on how to solve it: Size of int = 4 bytes, size of a pointer variable = 8 bytes (on my machine), adding 1 to a pointer makes the pointer point to it
2 min read
How to Compare Characters in C++? char in c is a keyword used for representing character data type. The memory size of char is 1 byte containing numbers, alphabets, and alphanumeric characters. We can compare the characters in C using 2 different ways: Comparison using ASCII values.Using the built-in function.1. Using ASCII Values A
3 min read
C - Pointer to Pointer (Double Pointer) In C, double pointers are those pointers which stores the address of another pointer. The first pointer is used to store the address of the variable, and the second pointer is used to store the address of the first pointer. That is why they are also known as a pointer to pointer.Let's take a look at
5 min read