1

I initialized a char and a pointer, and then point it to the char. But I found that only the first character in the char was passed into the pointer. How could this be? Appreciate any advice!

    char p[]="This is a long char.";
    std::cout<<sizeof(p)<<"\n";
    char *ptr=p;
    std::cout<<*ptr<<"\n";

Got:

21
T
Program ended with exit code: 0

When checking the value of variables I noticed that:

ptr=(char *) "This is a long char."
*ptr=(char) 'T'

Why is the ptr holding the whole char but the content in it is only the first character? Isn't this the opposite of what we call a pointer? Got really confused...

Muwei He
  • 45
  • 1
  • 5
  • 1
    `ptr` is the pointer (that holds the address to where something else is stored in memory), the `'*'` functions as the dereference operator providing access what is stored at that address. So `*ptr` provides access to the object stored at the memory location pointed to by `ptr` (e.g. the address held by `ptr` as its value). Since a pointer to anything points to the beginning address for a block of memory, when the type is `char *`, the pointer points (holds the address of) the first character. Dereferencing the `pointer` provides access to the first character... – David C. Rankin Feb 10 '20 at 00:19
  • Thank you! I think I understand why first character now; but in this case how can I access the full object using the char* pointer? – Muwei He Feb 10 '20 at 00:23
  • `std::cout << ptr;` will output the string pointed to by `ptr`. (also, don't confuse the `'*'` in `char *ptr=p;` with the dereference operator -- it isn't, it is just the `type` for the declaration, e.g. `char* ptr = p;`) So you can do `char *ptr;` then `ptr = p;` to make that clear. Attaching the `'*'` to variable during declaration makes clear you are declaring a pointer to type, as `char* a, b, c;` certainly does NOT declare 3-pointers, but instead 1-pointer and 2 plain `char`. Using `char *a, b, c;` makes that clear. Though in C++ the & for `reference` should go with the type. – David C. Rankin Feb 10 '20 at 00:26

3 Answers3

2

When you said:

std::cout<<*ptr...

you dereferenced ptr, which gives you the (first) character that ptr is pointing to.

flyingCode's answer shows you how to print the entire string.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
2

In C++, an array is just like multiple variables grouped together, so they each have a memory address, but they are one after another.

So, the pointer is pointing at the address of the first element, and the other elements are in the adresses right after that in order.

When you use the * operator on a pointer, it references the contents of the memory address the pointer is pointing to.

If you wanted to access another element of the array using this, you would use *(ptr + 5) to offset the memory address.

When you use the variable name alone, the language just does the offset for you and gets all the contents of the array. The pointer doesn't know the length of the array, but it can find the end of it, because char arrays always end with a null character (\0).

matbout
  • 46
  • 5
1

ran your code in repl and changing it to the code below worked :D

  char p[]="This is a long char.";
  std::cout<<sizeof(p)<<"\n";
  char *ptr=p;
  std::cout<<ptr<<"\n"; //notice no *

I recommend you use std::string rather than c-strings unless you have a specific need for them :)

flyingCode
  • 132
  • 2
  • 10
  • Thank you for advice! I wanted to but using char is an assignment requirement:( And that is what confuses me: why the ptr itself has the whole char but the content of it is only the first character? – Muwei He Feb 10 '20 at 00:12
  • to put it simply, `ptr` is actually just pointing to the first character of the array, so when you use *ptr, it's going to return what it's pointing to. Using ptr will return the array. For a more in-depth explanation here is a link :D https://stackoverflow.com/questions/43822029/why-pointer-variable-with-asterisk-and-without-asterisk-behave-differently-in-pr – flyingCode Feb 10 '20 at 00:19