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;
}
Related
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?
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.
This question already has answers here:
Why does cout print char arrays differently from other arrays?
(4 answers)
Closed 5 years ago.
I am novice in programming. I have written a program and confused in concepts of pointers.
#include <bits/stdc++.h>
using namespace std;
int main()
{
char c[]="hello";
char *a=c;
cout<<a<<endl;
int arr[]={1,2,3,5};
int *p=arr;
cout<<p<<endl;
return 0;
}
When I print a, it prints hello but when I print p it print the address. Why?
std::ostream has overload for const char* to display C-string.
int* would use the void* one which print the address.
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.
This question already has answers here:
Why can't I write to a string literal while I *can* write to a string object?
(4 answers)
Closed 7 years ago.
I tried running the following program:
#include <iostream>
using namespace std;
int main(){
char *x = "Linux";
*x = 'T';
cout<<"value: "<<*x<<endl;
}
According to me, it should have stored 'T' in the location pointed to by x. But instead it gave segmentation fault. But when I did:
char *x;
*x = 'T';
The output was as expected. Can somebody explain this behavior?
Using a non-const char pointer to a string literal is deprecated and should not be used in new code. Modifying a string literal is undefined behavior. Your second example dereferences an uninitialized pointer which is also undefined behavior. That means it can sometimes appear to work.
String literals are const char*s, not char*s.
Both of your examples are undefined behavior; the second one appearing to work only occurs by accident.