0

Here is a Minimum Reproducible Example:

#include <iostream>
#include <string>


int main()
{
    int givenInt;
    float givenFloat;
    double givenDouble ;
    std::string givenString;
    char givenChar;

    std::cin >> givenInt;
    std::cin >> givenFloat;
    std::cin >> givenDouble;
    std::cin >> givenString;
    std::cin >> givenChar;

    std::cout << givenInt << std::endl;
    std::cout << givenFloat << std::endl;
    std::cout << givenDouble << std::endl;
    std::cout << givenString << std::endl;
    std::cout << givenChar << std::endl;

    std::cout << &givenInt << std::endl;
    std::cout << &givenFloat << std::endl;
    std::cout << &givenDouble << std::endl;
    std::cout << &givenString << std::endl;
    std::cout << &givenChar << std::endl;

    std::cout << "End" << std::endl;

    return 0;
}

My input is:

32
64.212
4.76545
Hey
Hey

The first five prints are:

32
64.212
4.76545
Hey
H

The last one is only a H because givenChar is a char.

However, when I print the memory adresses, I get:

0x75337ffcdc
0x75337ffcd8
0x75337ffcd0
0x75337ffcb0
H└³⌂3u
End

What is this weird H└³⌂3u?

I'm using CLion: enter image description here

  • 2
    Output streams have an overload of the `<<` operator that takes any variant of `char*`, and treats it as a null-terminated string. If you want to print the pointer itself, you need to do e.g. `static_cast(&givenChar)`. – Some programmer dude Aug 09 '23 at 10:09
  • `&givenChar` is of type `char*`. The ostream's `operator<<` overload that accepts `char*` treats its argument as a pointer to the first character of a null-terminated C-style string. Since `givenChar` is not at the beginning of a C-style string, the behaviour is undefined. In practice, `└³⌂3u` is probably a representation of bytes that happen to be located in memory right after `H`. The output terminates because the next byte happens to have the value of zero, and `operator<<` interprets it as a null-terminator. – n. m. could be an AI Aug 09 '23 at 10:21
  • What's the difference between `char*` and `void*`. Is `void*` a type for "any pointer", and `char*` a pointer for a char? – FluidMechanics Potential Flows Aug 09 '23 at 14:03
  • `char*` is assumed to be c-string. The rest are not. – drescherjm Aug 09 '23 at 14:13
  • oh `char*` is never a pointer for a char? Fairs – FluidMechanics Potential Flows Aug 09 '23 at 14:37
  • 1
    It can be also a pointer to a char but that gets you into problems when the code assumes a null terminated c-string. – drescherjm Aug 09 '23 at 16:40
  • 2
    `char*` is always a pointer to a `char`. Sometimes that `char` is the first character of a null-terminated character array (a.k.a. C-style string), at other times it is not. It is no different from other pointers. `int*` may point to an individual integer, or it may point to an element of an array of integers. If you have a function that accepts a pointer, it might expect a pointer to an individual object or a pointer to an element of an array. You need to look at the documentation to know exactly. – n. m. could be an AI Aug 10 '23 at 09:18
  • I understand! It all depends on what the function does with the pointer – FluidMechanics Potential Flows Aug 10 '23 at 09:54

0 Answers0