Using & with string class objects in c++ [duplicate] - c++

This question already has answers here:
displaying address of char variable in c++ using pointers?
(1 answer)
Why does streaming a char pointer to cout not print an address?
(4 answers)
Closed 9 months ago.
As far as my knowledge in c++, the & character can act as an address of operator(finding the address of a variable in memory) or as a bitwise AND operator or declaring references.
However if I run this code:
#include<iostream>
#include<string>
using namespace std;
int main() {
string s = "Stackoverflow";
cout<<&s[0]<<endl<<&s[1]<<endl;
return 0;
}
Output is
Stackoverflow
tackoverflow
I expected it to print the addresses of the first 2 characters of the string, however, I got the string itself starting from a different index. How does it work?

Related

When I run this code, result is always 24 regardless of what string is. Why? [duplicate]

This question already has answers here:
c++ sizeof( string )
(9 answers)
Closed 1 year ago.
When I run this code, result is always 24 regardless of what string is. Why?
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s = "asdccccc";
cout << sizeof(s);
return 0;
}
A string is an object. By using sizeof you are getting the size of the members of that object. One of those members is probably a pointer to the actual string contents, but the size of a pointer is constant no matter what it points to.
Consider this simple example
class string
{
const char* _ptr;
....
....
public:
}
When you write sizeof(string), you will get the size of the class, not the size of string literal _ptr points to.

using scanf_s to write into a const char* memory [duplicate]

This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
What is the difference between char s[] and char *s?
(14 answers)
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 1 year ago.
I have a program as shown below :
This one works fine why? isn't it supposed to be constant for each of the characters? why am I able to overwrite it?
input: "Test" (Any String that is shorter dan 8 last byte reserved for null-terminated)
#include <iostream>
using namespace std;
int main()
{
const char itemss[8] = "1234567";
const char* item = items;
scanf_s("%s", item, 8);
printf(item);
return 0;
}
but when I try to change the code to this :
this one doesn't work, the code is the same as the above code but this one cause segmentation fault, string literal is const char[] (in this case const char[8]) so it should be no different than the code above right? my guess is this code below allocated in read-only memory but then again the code above also use const.
#include <iostream>
using namespace std;
int main()
{
const char* item = "1234567";
scanf_s("%s", item, 8);
printf(item);
return 0;
}

Why output of 'p' and '&p' is not same for 'int* p' in c++;? [duplicate]

This question already has answers here:
Address of pointer
(5 answers)
C: Why do pointer and &pointer have different values?
(4 answers)
Closed 5 years ago.
Here p is int type of pointer variable.In my code as per I understand output will be same for two line.But it doesn't.So I want to konw why it doesn.t?
#include<iostream>
using namespace std;
int main(){
int* p;
cout<<p<<endl; //0x41c2de
cout<<&p<<endl; //0x28ff0c
}
Question: Why output of two line is not same??

Please explain how this c++ program is working? [duplicate]

This question already has answers here:
"" + something in C++
(3 answers)
Closed 5 years ago.
I am having trouble , in understanding this program please help:
#include <iostream>
using namespace std;
int main(){
const char* s = 5+"hellow world";
cout<<s;
return 0;
}
It is correct and gives following output
In third line of your code, an anonymous character array is created by the compiler. When you add 5 to the c-string, it performs pointer arithmetic and moves the pointer 5 ahead to the string. Hence, it skips the 5 character from the c-string and only stores other characters from the array into the s.

Using & operator with char data type [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is address of char data not displayed?
I was experimenting with ampersand operator and got stuck at this program :
#include<iostream>
using namespace std;
int main() {
char i='a';
cout<<&i;
return 1;
}
I was expecting the address of variable i as the output but instead the output came as the value of variable i itself.
Can anybody explain what just happened? Thanx in advance.
That's because cout::operator<< has an overload for const char*. You'll need an explicit cast to print the address:
cout<<static_cast<void*>(&i);
This will call the overload with void* as parameter, which is the one used to print addresses.
Also note that your code runs into undefined behavior. You only have a single char there, and the overload expects a null-terminated C-string.