This question already has answers here:
Why in the code "456"+1, output is "56" [duplicate]
(3 answers)
Closed 6 years ago.
I am doing some exercises in C++ when I came upon something not so clear for me:
cout << "String" + 1 << endl;
outputs : tring
I suggest it is something with pointer arithmetic, but does that mean that everytime I print something in quotes that is not part of previous defined array,I actually create a char array ?
A quoted string (formally a string literal) is an array of const char, regardless of whether your printing it or doing anything else with it.
Code:
cout << "String" + 1 << endl;
has the same effect as this:
const char *ptr = "String";
cout << ptr + 1 << endl;
so no you do not create a new array, you just change pointer and pass it to std::cout
Related
This question already has answers here:
How to print the address of char array
(1 answer)
cout << with char* argument prints string, not pointer value
(6 answers)
Why does the index of a `char **` type give the whole string?
(2 answers)
Closed 2 years ago.
In C++ could someone clarify
1- In he following code, printing char* is displayed as bunch of random characters, but printing string* is just an integer address? Why so?
int * intptr;
char * charptr;
std::string * stringptr;
float * floatptr;
//printing
std::cout << intptr << " , " << charptr << " , " << stringptr << " , " << floatptr << "\n";
output:
0x7ffeea2f1a60 , ���� , 0x7ffeea2f1a48 , 0x1092d13d4
2- charptr is a pointer variable painting to part of memory that contains a character. Just for curiosity, how do you print the address of charptr?
3- While reading different sources, I came across to this: "The different printing is because of the functions (i.e. a const char *) that treat that memory as a sequence of characters". Could someone expand this answer w.r.t the above code?
char* is special with regard to printing. The reason is the historical legacy of C, where strings are represented as arrays of characters. So most of the time given a char* what you want to do is print the characters it points at.
I don't think you mean print the address of charptr (that would just be cout << &charptr), I think you mean print the address which is the value of charptr. The way to do that is to cast the address to void* before printing it.
cout << static_cast<void*>(charptr);
Your third question is essentially what I explained in the first paragraph above.
This question already has answers here:
How to output a character as an integer through cout?
(6 answers)
Convert an int to ASCII character
(11 answers)
Closed 3 years ago.
I have a program that takes a string. Using the check() function that calculate the sum of all the value in the string of integers, I make additional computations, aka ss. The issue comes when I try to convert ss, which is an int, into a char, c.
When I try to print out the newly converted value, nothing prints out on the console, not even an error message.
I have tried using static_cast<char>(ss), and it won't work. Yet when I try to print out the ss value, I get it to print it out.
Source Code
void sum(string input)
{
int s = check(input);
int ss = (s * 9) % 10;
char c = ss;
cout << "val is: " << c << endl;
}
int main()
{
string x = "7992739871";
sum(x);
return 0;
}
Can someone explain what I might be doing wrong and how it can be fixed?
You can use std::to_string() (C++11) but making sure that the value of c is something that can be printable is a better practice.
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;
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.
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);