What is the difference between these two definitions? [duplicate] - c

This question already has answers here:
Memory Allocation char* and char[]
(3 answers)
Closed 6 years ago.
What is the difference between these two definitions?
char *string = "MyString";
char string[] = "MyString";
As much as I know, the first one is a pointer to a string.

The first is a pointer to a string literal, the second is an array initialized with the contents of the string literal (which BTW when optimized points exactly to where string points).
The first one lives in the read only segment of the program's memory and thus cannot be modified.
The second one is an array of 9 elements and you can modify any of the 9 elements including the termnating null byte that is not explicitly set in the code in your question.

Related

Why can't I change a string by index when it is declared by pointer notation? [duplicate]

This question already has an answer here:
Why can I not modify a string literal in c?
(1 answer)
Closed 4 months ago.
for example:
char stringer[]="hello";
stringer[2]='A';
The above works to change 'l' to 'A'. But if I do the following:
char *stringer="hello";
stringer[2]='A';
This doesn't work, is there a reason for this?
As answered by Some programmer dude in comments:
Literal strings are really non-modifiable arrays of characters. With char *stringer="hello"; you make stringer point to the first character of such an array. Attempting to modify its contents leads to undefined behavior.
That's why you should always use const char * when pointing to literal strings.
Strings can be modified when using pointer, if the pointer is pointing to something you're allowed to modify. For example
char stringer[] = "hello";
char *pointer = stringer;
pointer[2] = 'A';

Why cant i replace the character at 0th index with character at 1th index through the assignment statement at line 11? [duplicate]

This question already has answers here:
Segmentation fault when modifying a string [duplicate]
(2 answers)
Closed 4 years ago.
#include<stdio.h>
void main()
{
char d;
char *r="Helloo";
printf("%s\n",r);
d=*(r+1);
printf("%c",d);
*(r+0)=d;
printf("%s\n",r);
}
this was working fine when i stored the string in a character array but why doesn't it work now
char *r="Helloo";
You assigned pointer r to a string literal. String literals should be treated as immutable. Any attempt to modify one leads to undefined behavior (N1570, Section 6.4.5/7).
With
char r[]="Helloo";
You have the string stored in an array that you can modify so it works as expected.

Can two equal/unequal string literals be stored in the same memory location? [duplicate]

This question already has answers here:
String Literal address across translation units [duplicate]
(2 answers)
Understanding Char Array equality in C
(5 answers)
Closed 8 years ago.
I have some doubts about string literals.
Firstly, I know that C-strings must be compared using the strcmp(),strncmp() or some other function and if one uses ==, then they compare the pointers as the array name "decays" into a pointer to its first element.
Is there a chance that two same strings can have the same address,i.e, Can these conditions be true?
if("string"=="string")
/*OR*/
char* str="string";
if(str=="string")
/*OR*/
char* str="string";
char* str2="string";
if(str==str2)
Secondly, Can two unequal strings contain the same address?
Lastly, Do these strings stay in read only memory segment until the program ends?

once defined ,string cannot be initialized to other characters? [duplicate]

This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
C - why is strcpy() necessary
(4 answers)
Closed 8 years ago.
Using pointers string can be initialized to other characters but when once string has been defined it cannot be initialized to other characters.What is the reason behind it??
int main()
{
char str1[]="hello";
char *p="hello";
str1="bye";/*error*/
p="bye";/*works*/
}
You've defined str1 as an array, and arrays aren't assignable.
You can, however, copy other data into the array, for example:
char str1[] = "hello";
strcpy(str1, "bye");
Arrays are arrays and pointers are pointers. Defining arrays gives the pointer to the allocated array, that is a constant pointer to the location where the array space hass been reserved. That is a concrete address in the lifo stack. So, str1 is a constant pointer value and you cannot change it. You cannot set the value of the address of a different constant string.
Defining pointers, as char*p, gives you a variable value of an address. And so, you can change de value of the variable p.
To change the characters in an array such as what you are doing you have to use a function such as strcpy or do it index by index.
str1[0] = 'p';
will print out pello
What you are trying to do is not supported by the C language.

Are string literals constant or not? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is it possible to modify a string of char in C?
char *s = "anusha";
Is this like a constant pointer? When i tried to change the character in location 3 by writing s[3]='k', it gave me a segmentation fault. So i am assuming it is like pointing to a constant array or s is a constant pointer? Which among the two? Please clarify.
That is correct, you are not allowed to modify string literals.
However, it's legal to do this:
char s[] = "anusha";
s[3] = 'k'
The difference here being that it is stored as a local array that can be modified.
It looks like your compiler treats "anusha" as a pointer to char, but places the string itself into write-protected memory. I remember reading that this is a convenience policy in order to comply with existing code.
As Joe pointed out, this is detailed in Is it possible to modify a string of char in C?.

Resources