This question already has answers here:
Modify a string with pointer [duplicate]
(4 answers)
Closed 6 years ago.
I am getting a seg fault for the below code, is there something wrong?
Here I am trying to shift the bits in a.
Also, I know char * is a read only memory.
So, we have to copy into char a[] and then modify it???
char *str = "abc";
*str = *str << 1;
char* string literal is pointing to read-only memory. You need to use a char array:
char str[] = "Hello";
*str = *str << 1;
See: What is the difference between char s[] and char *s?
What are you trying to do exactly ?
<< is a bitwise operator and *str will not refer to the whole string but only to the first character.
I also recommend you to put parenthesis around your pointer when doing any operations on it to be sure that the bit shift is not done on the address instead of on what is pointed at this address.
Related
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';
This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Closed 9 years ago.
In my GCC 32-bit compiler, the following code gives the output
char *str1="United";
printf("%s",str1);
output:
United
then should I consider char *str1="United"; the same as char str1[]="United"?
The two are not the same: char *str1="United" gives you a pointer to a literal string, which must not be modified (else it's undefined behavior). The type of a literal string should really be const but it's non-const for historical reasons. On the other hand, char str1[]="United" gives you a modifiable local string.
char* str = "United"; is read-only. You wouldn't be able to reach inside the string and change parts of it:
*str = 'A';
Will most likely give you a segmentation fault.
On the other hand char str1[] = "United"; is an array and so it can be modified as long as you don't exceed the space allocated for it (arrays cannot be resized). For example this is perfectly legal:
char str[] = "United";
str[0] = 'A';
printf("%s\n", str);
This will print Anited.
See the comp.lang.c.faq, question 1.32. It basically boils down to the fact that declaring the string in array form (char str[] = "foo") is identical to char str[] = {'f','o','o'}, which is identical to char str[] = {102, 111, 111}; that is, it is a normal array on the stack. But when you use a string literal in any other context it becomes "an unnamed, static array of characters, [which] may be stored in read-only memory, and which therefore cannot necessarily be modified." (And trying to modify it results in undefined behavior wherever it happens to be stored, so don't).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between char a[] = “string”; and char *p = “string”;
char *str = "Hello";
printf("%c",++*str);
This gives segmentation fault on linux with gcc.
The moment the first statement is changes to as
char str[10] = "Hello";
It works. What may be the reason?
It is undefined behaviour to attempt to modify a string literal.
The compiler is free to place it in read-only memory (as it probably does in your case). Attempting to modify read-only memory is what's probably triggering the segfault.
This statement char *str = "Hello"; stores the string "Hello" in RO-section and assigns the address of the area of RO-section(in which "Hello"is stored) to str. The data stored in RO-section cannot be modified thus you are getting a segfault.
char str[10] = "Hello";
is also wrong. You should instead write
char str[10];
strncpy(str,"Hello",sizeof(str));
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Difference between char *str="STRING" and char str[] = "STRING"?
Need some help with C programming
while this snip gets segmentation fault
int main(void) {
char* str ="abcde";
str[strlen(str)-1] ='\0';
printf("%s",str);
return 0;
}
If I put
char str [] ="abcde"; instead of the pointer that works perfectly, do you have an idea why so?
When you write char *str = "abcde"; you make a char pointer to a string literal, which you are not allowed to modify.
When you write char str[] = "abcde"; you make a char array and copy a string literal into it. This is just a normal char array so you are free to modify it.
It is undefined behaviour to modify a string literal. This is a deliberate design decision that allows string literals to be placed in special read only sections of the output program. In practice many compliers and platforms do this (marking it read only means you need only one copy of the strings in memory, even if there is more than one instance of the program running). This leads to the behaviour you observed on your platform.
char *str = "abcde";
str is a pointer to char that points to a string literal. String literals are stored in read only memory. You can't modify them.
char str[] = "abcde";
The string literal "abcde" is copied into the array str. So you can modify it.
char * is pointer to char (string literal) and char [] is an array of char. You can't modify string literal but you can modify char array.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Is it possible to modify a string of char in C?
What is the difference between char s[] and char *s in C?
There is something I don't understand about strings and pointers in C.
Suppose I have this declaration:
char str[] = "abc";
Then, if I attempt to modify it this way:
str[0] = 'b';
It will work.
But if I declare the string as a pointer to a char
char* str = "abc"
The attempt above will cause an access violation.
What I'm trying to understand is what exactly is the difference.
Thanks in advance
In The later example the compiler puts the string in the read only data section, so u can't modify it. But in earlier example you are declaring array of character which resides in stack hence you can modify content of this array.