Point to char confusion

I am very much confused by the following code.


1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main()
{
    char c = 'b';
    char *ptrChar = &c;
    std::cout << ptrChar << '\n'
              << ptrChar + 1 << '\n'
              << ptrChar + 2 << '\n'
              << ptrChar << std::endl;
    return 0;
}


The output is

bGa
Ga

when and why were memory at ptrChar+1 AND ptrChar+2 initialized?
why does not ptrChar+2 print out 'a'?
why it prints nothing when I want to print ptrChar for the 2nd time?
Last edited on
The program (the expression ptrChar + 2) engenders undefined behaviour.
(ptrChar is a not a pointer to an element of an array)

See: https://eel.is/c++draft/expr.add#4

More info:
If any of the operands is a pointer, the following rules apply:

A pointer to non-array object is treated as a pointer to the first element of an array with size 1.

If the pointer P points to the ith element of an array, then the expressions P+n, n+P, and P-n are pointers of the same type that point to the i+nth, i+nth, and i-nth element of the same array, respectively. The result of pointer addition may also be a one-past-the-end pointer (that is, pointer P such that the expression P-1 points to the last element of the array).

Any other situations (that is, attempts to generate a pointer that isn't pointing at an element of the same array or one past the end) invoke undefined behavior.
https://en.cppreference.com/w/cpp/language/operator_arithmetic#Additive_operators


In addtion, note that the stream insertion operator expects a pointer to the first element of a null-terminated array of char

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
    const char arr[] = "abcd" ; // null terminated array of 5 char

    const char* ptrChar = &( arr[0] ) ; // pointer to the first char in the array

    std::cout << ptrChar << '\n' // abcd
              << ptrChar+1 << '\n' // bcd
              << ptrChar+2 << '\n' // cd
              << ptrChar+3 << '\n' ; // d
}
Last edited on
Hello.

when and why were memory at ptrChar+1 AND ptrChar+2 initialized?

Because that these expresions show you something doesn't mean that they were initializated. When I run your code in my machine, lines 8 and 9 shows garbage (different every time I run the program) and line 10 shows the same that line 7.

why does not ptrChar+2 print out 'a'?

Could you explain me why it should be in that way?

why it prints nothing when I want to print ptrChar for the 2nd time?

As I said, in my system both times you call ptrChar it shows the char b.

I think that you are confusing some concepts, and trying to use pointer's arithmetic in a char variable and expecting the same behaviour as if you use it on an array or a char sequence.

Read these:
http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/ntcs/
http://www.cplusplus.com/doc/tutorial/pointers/
The output is

And the output from one run of your code compiled with Visual Studio 2019 (32-bit):
b

_?ù<═]ò
b

Each time program is run I get different output, since the code is reading uninitialized memory that the program doesn't "own."

Using pointer math with a variable that isn't an array or container will get you in trouble.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
    char c = 'b';
    char* ptrChar = &c;
    std::cout
    << "        ptrChar: " << ptrChar << '\n'  // AS EXPECTED
    << "       *ptrChar: " << *ptrChar << '\n' // DEREFERENCE
    
    << "   *ptrChar + 1: " << *ptrChar + 1 << '\n'
    << "   *ptrChar + 2: " << *ptrChar + 2 << '\n'
    
    << " *(ptrChar + 1): " << *(ptrChar + 1) << " ... JUNK\n" // JUNK, AS EXPECTED
    << " *(ptrChar + 2): " << *(ptrChar + 2) << " ... JUNK\n" // JUNK, AS EXPECTED
    
    << "              c: " << c << '\n' // 'b' AS EXPECTED
    << "         (int)c: " << (int)c << '\n'; // ASCII  VALUE OF 'b' AS EXPECTED
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.