1

I am a student in a CompSci intro class and I have a very basic understanding of pointers in C++. I had noticed in attempting to complete an assignment that a character array / c-string uses pointers differently than other data types.

For example, please consider the following code I created:

#include <iostream>

using std::cout, std::endl;

int main()
{
    int inta[] = {1,2,3};
    int* p1 = inta;

    cout << "p1 = " << p1 << endl;
    cout << "*p1 = " << *p1 << endl;
    cout << "sizeof(p1) = " << sizeof(p1) <<
        ", sizeof(*p1) = " << sizeof(*p1) << endl;

    char stra[] = "Dog";
    char* p2 = stra;

    cout << "p2 = " << p2 << endl;
    cout << "*p2 = " << *p2 << endl;
    cout << "sizeof(p2) = " << sizeof(p2) <<
        ", sizeof(*p2) = " << sizeof(*p2) << endl;
    return 0;
}

The output of *p1 and *p2 are both the first value of the array. However, while the output of p1 is the pointer to the first element of inta (which tracks from online research), the output of p2 is the entire word "Dog". The sizes of p1 and p2 are the same, the size of *p1 and *p2 are 4 and 1 respectively. Is there something I am missing?

I am using Visual Studio Community 2022 and created a normal project.

Thank you, and I appreciate your help!

Nathan Pierson
  • 5,461
  • 1
  • 12
  • 30
  • It's specifically that `operator<<(std::isteam&, char*)` (and other character pointer types) has a special overload. It's so that if you write `std::cout << "Hello world";` it prints `Hello world` and not the address of the first `'H'`. See e.g. [this question](https://stackoverflow.com/questions/17813423/cout-with-char-argument-prints-string-not-pointer-value) – Nathan Pierson Nov 28 '22 at 22:36
  • 1
    Note that pointers are really dumb. They know the location of an object and that is all. They don't know how big the object is, `sizeof(p1)` is the size of the pointer, not the pointed-at object. – user4581301 Nov 28 '22 at 22:38
  • 1
    Thank you Nathan, I have a conclusion question based on the example you posted: so is it true that p1 in my code does actually store the address of the first value of the c-string into it? And that cout simply outputs addresses to c-strings as the c-string itself? – Michael Davis Nov 28 '22 at 23:00
  • 1
    Mostly correct, but it's not `cout` doing the thinking. There is an [`<<` operator overload specifically for `const char *`](https://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt2) (`const` because it's not going to change the values while printing. That would be dumb) that prints strings because this is what the programmer wants the overwhelmingly vast majority of the time when the have a `char *`. – user4581301 Nov 28 '22 at 23:05
  • 2
    Oh yes of course I had mixed that up; `cout` is the output stream, the `<<` operator modifies the output. I understand now, thank you so much! – Michael Davis Nov 28 '22 at 23:19
  • 1
    If you really want to print the address of the `'D'` in `stra`, do `cout << "p2 = " << (void *)p2 << endl` instead of `cout << "p2 = " << p2 << endl`. The effect of the `(void *)` is producing a `void *` pointer from `p2` - and that results in a different overload of the stream's `operator<<()` (which accepts a `void *`) being called - which prints the address passed rather than the contents of the string. To be even more specific (and use a coding style often advocated) use `static_cast(p2)` instead of `(void *)p2` – Peter Nov 29 '22 at 02:13

1 Answers1

1
  • p1 is a pointer to an int. *p1 is thus an int -> size = 4
  • p2 is a pointer to a char, *p2 is thus a char -> size = 1

passing a char pointer to an output operator (cout<< or printf etc) will keep reading chars until it reaches the null char at the end of the string

pm100
  • 48,078
  • 23
  • 82
  • 145