A simple string pointer in C++ [duplicate] - c++

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 7 years ago.
For the code given below, why is the output "This is the string" instead of the address of the first character in the string, 'T'?
int main()
{
char myString[] = "This is a string";
char *ptr = &myString[0];
cout << ptr << endl;
return 0;
}
Output is to be clicked above.

why is the output "This is the string" instead of the address of the first character in the string, 'T'?
There is an operato<< overload whose LHS is a std::ostream and the RHS is char const*. This function prints the string.
If you want to print the address of 'T', you can cast the pointer to a void*.
cout << static_cast<void*>(ptr) << endl;

char *ptr = &myString[0];
Means make ptr point to the first character of myString. Thencout has an overload of << that takes a char * and will print what it points to and and the preceding elements until it reaches an '\0'
If you want to print the address of the array then you need to convert the pointer to something else like a void* first and then print:
cout << reinterpret_cast<void*>(ptr) << endl;

Related

It prints DATA when displaying the value of variable with type array of char [duplicate]

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Why does cout print char arrays differently from other arrays?
(4 answers)
How does std::cout work with char pointers?
(1 answer)
Closed last month.
I can't understand why cout prints DATA for str variable. str does not contain the memory address of the first caracter ? What does it mean DATA ?
#include<bits/stdc++.h>
using namespace std;
int main()
{
char str[7]=”DATA”;
cout << str[2]<<” “<<str;
return 0;
}
It prints T DATA.
Thank you for your help,
operator<< has an overload for const char* that prints out characters until it hits a null terminating character. And a char[] array decays into a char* pointer to its 1st element.
So, when we print str[2] it just prints the single character T, but when we print str it prints all of the characters.
str[2] is a single char, T, which you see.
str, on the other hand, is a char* (read: pointer to char), and cout will print it as a c-string - i.e., print all the characters until it reaches the null character (\0), similar to what printf would have done. If you don't want that overload, you could cast it to a void*:
cout << str[2] << " " << (void* ) str;

How to you get the address of an element in an std::string in C++? [duplicate]

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 5 years ago.
For example, i have the following string :
std::string s = "Hello, World!"
I want the address of the last element of s which is '!'.
I tried the following code, but it does not seem to output what I want it to.
std::cout << &s[s.length() - 1];
That outputs '!' not it's address, the same happens with s.back()
Is it because of the formatting caused by std::cout or is the problem elsewhere?
Basically I want a function that outputs the address of the last (and if possible, the first) element in a string.
Currently, you're using the overload of operator<< that takes a const char* as input. It treats the input as a null-terminated C string.
If you cast to (const void*) the problem will go away:
std::cout << (const void*)(&s[s.length() - 1]);
auto address_back = &s.back();
auto address_front = &s.front();
cout << static_cast<void*>(address_back) << endl;
cout << static_cast<void*>(address_front) << endl;

Why does printing a char* give a string rather than an address? [duplicate]

This question already has answers here:
Why is address of char data not displayed?
(8 answers)
Closed 6 years ago.
I'm going through the basics of learning C++, but keep hitting a wall when trying to decipher the following about chars and pointers. Included are line comments giving my current understanding of what's going on. Given that I have code like below:
using namespace std;
int main()
{
//String literal is an array of chars
//Array address gets assigned to a ptr of char
char myletters[] = {'h','i'};
char* lp = myletters;
cout << *lp << endl;
//Logically equivalent to above statements
char* letters2 = "hi";
cout << *letters2 << endl;
//String literal turns into array of chars
//Array of chars gets assigned to a ptr of chars
//Each ptr of chars gets stored into letters array
char* letters[] = {"hi","hello"};
cout << *letters << endl;
}
My output will be:
h
h
hi
My question is: when I use the final cout to print the contents of *letters, why do I get the string "hi" rather than the address of "hi" or the address of the first character in "hi"? I get that the first uses of cout are printing a char, and that the last cout is printing a char*, but I'm still wondering why it prints the complete string rather than the address as I would generally expect from a pointer.
Thanks kindly.
the << operator has a special definition for char* that prints the C-string it refers.
In your case, *letters has char* type (being letters a char*[], same as char**) and not char as *lp have.

tried to print out the value the pointer, expecting address of the object to which the pointer points to, instead, compiler return the object's value [duplicate]

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 7 years ago.
I'm new to C++ and is trying to learn the concept of pointer. I have declared a pointer *pStart and initialised it to equal to 'text'. When I tried to print out the value of the pointer, I was expecting it to equal to the address to which 'text' was stored in, instead, the compiler printed out the string 'hello'. Could someone please explain it to me why this is happening?
char text[] = "hello";
char *pStart = text;
cout << pStart << " + " << *pStart << " + " << &pStart << endl;
This C++ feature is called operator overloading. Depending on the type of the input value certain type of streaming operator is invoked. For the character input look for "insert characters" at the link above or here.
Try this:
std::cout << std::hex << reinterpret_cast<int>(pStart);

Why is a char pointer dereferenced automatically in a dynamic array [duplicate]

This question already has answers here:
Why does cout print char arrays differently from other arrays?
(4 answers)
Closed 7 years ago.
Perhaps a stupid question. When I cout the pointer to the char array, I thought it would print an address; instead it dereferences the address and prints the actual values till null.
As opposed to an int array where it does what I expect it to. It prints the address of the first element.
Why does the char element gets dereferenced when you print the pointer.
char* as = new char[100];
as[0] = 'a';
as[1] = 'b';
as[2] = NULL;
cout << as << endl;
int* s = new int[100];
s[0] = 2;
cout << s << endl;
Asking this because when I try to get the address to the first char element a[0] = 'a';. I have to store it in a pointer to a pointer. Which seems weird to me but that's besides the point.
char ** d = &as;
cout << d << "this is d" << endl;
There is no overloaded output operator << that prints the address for any char pointer, it treats all char pointers as strings. If you want to print the address of a pointer, you need to cast it to void*
std::cout << "Address of string is " << static_cast<void*>(as) << '\n';
On a side-note, the code
char ** d = &as;
cout << d << "this is d" << endl;
will not print the address of the string, i.e. the pointer contained inside as, instead it will print where the variable as is stored in memory. Not quite the same thing.
char* character pointers are considered to be C-style null terminated strings by the std::ostream << operator. You are right that this is a different behavior from other pointer types.
&a is not a pointer to a[0] . It is a pointer to a which is itself a pointer. a is in fact the pointer to a[0] and is equivalent to &a[0].
IOStreams treat char* (and const char*) specially, so that you can print C-strings without further effort:
std::cout << "hello world\n";
(Bear in mind that the string literal expression decays immediately to a const char* when passed to operator<<.)
If you do not want this behaviour, you can cast to void*:
char* as = new char[100];
as[0] = 'a';
as[1] = 'b';
as[2] = NULL;
cout << (void*)as << endl;
Your "fix" is actually broken, because you are printing the address of the pointer as, not the address of the array elements that as points to. This is indicated by the char** type, which you already noticed.
It prints the string because that's what the definition of that particular operator<< overload does. Cast to void * if you want to print an address:
cout << static_cast<void *>(as) << endl;
The << operator is overloaded to take a char* and output its contents where as there is no such overload for a int*/int[].