This question already has answers here:
cout not printing unsigned char
(5 answers)
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 1 year ago.
#include<iostream>
using namespace std;
int main(){
int a=1025;
int *p=&a;
cout<<"Size of integer is: "<<sizeof(int)<<" bytes"<<endl;
cout<<"Address= "<<p<<" Value= "<<*p<<endl;
char *p0;
p0=(char*)p;
cout<<"Size of char is: "<<sizeof(char)<<" bytes"<<endl;
cout<<"Address= "<<p0<<"Value= "<<*p0<<endl;
cout<<"It printed smilie above";
return 0;
}
Why is the address and value of char printed as a smilie?
The output I got is:
Size of integer is: 4 bytes
Address= 0x61ff04 Value= 1025
Size of char is: 1 bytes
Address= ☺♦Value= ☺
The overload for operator<< which prints char* (like char *p0;) does not print the address the pointer is pointing at like the overload for void*. Instead, it actually reads the content at the pointed-out address - and keeps on reading, until a \0 is encountered, which is what makes std::cout << "Hello world"; work.
"Hello world" (a null terminated C string) in itself decays into a const char*, pointing at the H, and the operator<< overload starts reading there and outputs each character until it sees a \0 and then it stops.
The smiley is just a happy coincidence. The values stored at the pointed-out address happened to form a unicode sequence - which forms a smiley.
Related
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;
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.
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;
This question already has answers here:
Why does streaming a char pointer to cout not print an address?
(4 answers)
Closed 7 years ago.
Why in my code I see value, which stored in the address, but not adress?
char *fortunes[] =
{
"1",
"2",
"3",
"4"
};
cout << *fortunes[2]; // result 3
cout << fortunes[2]; // result 3, but I expected to see adress
There is an overload for std::ostream& operator<< which takes a const char* and interprets it as a null-terminated string, printing out characters until it finds the null terminator. If you want to print the value of the pointer (the address it holds), you can cast it to void*.
For example
cout << reinterpret_cast<const void*>(fortunes[2]) << endl;
This question already has answers here:
How to get the number of characters in a std::string?
(12 answers)
C sizeof char pointer
(5 answers)
Closed 9 years ago.
char* string = "hello there";
cout << sizeof( string ); // prints 4, which is the size of pointer
cout << sizeof( *string ); // prints 1, which is the size of char
How do I get the number of characters contained in the string (11)?
It's strlen you want for that, not sizeof. The first counts the number of characters up to the terminating NUL while the second gives you the size of the type which, in this case, is a pointer rather than the underlying array of characters.
By that last point, I mean:
char *x = "hello there";
char y[] = "hello there";
std::cout << sizeof(x) << ' ' << sizeof(y) << '\n';
will most likely output something like:
4 12
on a system with 32-bit pointers (and 8-bit char). In that case, the 4 is the size of the pointer, the 12 is the number of bytes in the array (including the NUL at the end).
In any case, that's moot, since strlen() is the right way to get the length of a C string (yes, even in C++, though you may want to consider using the C++ strings since they may save you a lot of trouble).
The function sizeof() returns the size of a data type in Byte
For example because you define:
char* string = "hello there";
then type of string is char * and size of mostly all pointer is 4 Bytes ( this function returns 4) But type of *string is char and size of every character is 1 Byte ( This function returns 1)
Solution for you :
Alternative 1:
Use function strlen() in library 'string.h'
Alternative 2:(from scratch)
int length = 0;
int index = 0;
while ( string[index] != '\0')
{
length++;
index++;
}