address of characters in C++ [duplicate] - c++

This question already has answers here:
The address of character type variable
(3 answers)
Closed 4 years ago.
I have this program
#include <iostream>
using namespace std;
int main ()
{
char c;
c = 'A';
cout<<"c = "<<c<<" &c = "<<&c<<endl;
return 0;
}
I expect the conosol to print out the following statement:
c = A &c = address in hex format
but instead I get this:
(Output: &c = A0 ")
why does the compiler treats the address of a character as an ASCII code, not a hex number

The problem is the "<<" operator is defined for char and char* to write a "character sequence" so if you want to see the address You have to cast to the number (int)&c

Related

why does compiler print char as smilie [duplicate]

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.

The code below outputs 1 and 0 how ever i do not know why [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 2 years ago.
I understand that the code below is outputing the result of a boolean question but i do not understand why it outputs 1 and 0 instead of 1 and 1.
#include <iostream>
using namespace std;
int main()
{
string d = "abc", e = "abc";
char a[] = "abc", b[] = "abc";
cout << (d == e);
cout << (a == b);
return 0;
}
//outputs 10
i also tried outputing the value stored in variables a and b and got the same value for both
#include <iostream>
using namespace std;
int main()
{
string d = "abc", e = "abc";
char a[] = "abc", b[] = "abc";
cout << (d == e);
cout << (a == b);
cout << "\n" << a << "\n";
cout << b;
return 0;
}
// outputs 10
//abc
//abc
In C, std::string has a special operator== that actually compares the strings. Contrary to what you might read, std::strings are not equivalent to char arrays. Char arrays are essentially treated as pointers, so == compares the memory addresses, which are different, so the program will output 0.
For comparing 2 arrays of char you should use strcmp, the == operators compares the pointer addresses instead of the value itself. You can also iterate the arrays and compare element by element.

c++: How do you print to stdout a casted int value? [duplicate]

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.

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.

how to show the character that come several time in string? [duplicate]

This question already has an answer here:
Counting characters in words in a C++ console application
(1 answer)
Closed 9 years ago.
Is there anyway to show that character s appears 5 times in the string is there any function to sort out the character and than show us that the character appears 5 or 6 times in the string.
#include<iostream>
#include<string>
int main(){
using namespace std;
string a="hello how are you"
//now i want to show the l character appears several time.
//and i need help here.
system("pause");
}
You can use std::count
int lcount = std::count(a.begin(), a.end(), 'l');
Just keep counting using a pointer until nul character is reached and keep increasing an integer on successful comparison.
#include<iostream>
#include<string>
int main(){
using namespace std;
string a="hello how are you";
char *p = &a[0];
int c = 0;
do
{
if(*p == 'l')
c++;
}while(*p++);
cout << c;
}
You can walk over any container, including a string and do what you like with it.
You could count how many instances of each character. You may want to consider ignoring whitespace.
std::string a("hello how are you");
std::map<char,int> count;
for(auto c : a)
{
++count[c];
}