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!

6
  • 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. Commented Nov 28, 2022 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? Commented Nov 28, 2022 at 23:00
  • 1
    Mostly correct, but it's not cout doing the thinking. There is an << operator overload specifically for const char * (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 *. Commented Nov 28, 2022 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! Commented Nov 28, 2022 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<void *>(p2) instead of (void *)p2 Commented Nov 29, 2022 at 2:13

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.