This question already has answers here:
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 6 years ago.
Can anyone explain why this code not work, pls!! Thanks you so much!
#include <stdio.h>
void changer(char * tp)
{
int i=0;
tp[0]='b';
}
int main(void)
{
char *st="aaab";
changer(st);
printf("%s",st);
}
This statement
tp[0]='b';
results in undefined behaviour because tp points to a string literal. You are not allowed to modify a string literal in C.
Instead you could use an array:
char st[] = "aaab";
which you'd be able to modify.
char *st="aaab";
This statement indicates that st is a pointer variable but "aaab" is string constant.
Instead try
char st[]="aaab";
This statement indicates that it declares st as array [5] of char and copies the contents of the string literal. Here st is a constant pointer.
Related
This question already has answers here:
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 10 months ago.
#include <stdio.h>
#include <stdlib.h>
int main() {
char *str="hello";
str[0]='H';
return 0;
}
If I use an array I can do use subscript to assign the first character. What's different about using a char pointer here that causes a segmentation fault?
In this code snippet
char *str="hello";
str[0]='H';
you are trying to change a string literal pointed to by the pointer str. Any attempt to change a string literal results in undefined behavior.
From the C Standard (6.4.5 String literals)
7 It is unspecified whether these arrays are distinct provided their
elements have the appropriate values. If the program attempts to
modify such an array, the behavior is undefined.
So though in C opposite to C++ string literals have types of non-constant arrays it is always better to declare pointers to string literals with the qualifier const.
const char *str="hello";
You could declare a character array initialized by the string literal and change the array itself like
char str[] ="hello";
str[0]='H';
okay, I got the answer from this wiki - https://en.wikipedia.org/wiki/Segmentation_fault#Writing_to_read-only_memory
The char pointer declared like that writes to a read-only segment of the process, editing that gives a segmentation fault (specifically SEGV_ACCERR which is defined as invalid permissions for mapped object (see here)
An array on the other hand similarly allocated will be done so on the stack and so can be edited.
This question already has answers here:
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 6 years ago.
This works and produces bbcd as I'd expect.
#include <stdio.h>
int main(void) {
char string[] = "abcd";
string[0] = string[1];
printf("%s\n", string);
}
This is a bus error.
#include <stdio.h>
int main(void) {
char *string = "abcd";
string[0] = string[1];
printf("%s\n", string);
}
Why?
Valgrind says:
==9909== Process terminating with default action of signal 10 (SIGBUS)
==9909== Non-existent physical address at address 0x100000FA2
==9909== at 0x100000F65: main (test.c:6)
Because in the second case, you're trying to modify a string literal which invokes undefined behavior.
To elaborate, in the second case, string[0] is the first element of the string literal, and any assignment to that is an attempt to modify the value held by that element.
Quoting C11, chapter ยง6.4.5, "String literals"
[...] If the program attempts to modify such an array, the behavior is
undefined.
This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(20 answers)
Closed 6 years ago.
I'm trying to implement my own strcpy function , here is the code :
#include<stdio.h>
#include <stdlib.h>
void Strcat(char *p1,char*p2)
{
while(*p1!='\0'){p1++;}; //this increments the pointer till the end of the array (it then points to the last element which is '\0')
while(*p2!='\0'){*p1++=*p2++;};
}
int main()
{
char txt1[]="hell";
char txt2[]="o!";
printf("before :%s\n",txt1);
Strcat(txt1,txt2);
printf("after :%s\n",txt1);
}
Everything works fine .
However , when I change
char txt1[]="hell";
char txt2[]="o!";
to
char *txt1="hell";
char *txt2="o!";
I face a Segmentation fault ,
Why is that ?
Aren't they ( both initialization methods) equivalent ?
Aren't "txt1" and "txt2" both act like a pointer to the first element of the char array ?
String literals are not modifiable and trying to modify them invokes undefined behavior.
char txt1[]="hell"; is a character array, so modyfying its contents is allowed.
char *txt1="hell"; is a pointer to a string literal, so you must not modify what is pointed.
This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Is modifying a string pointed to by a pointer valid?
(9 answers)
Closed 9 years ago.
I am trying to modify string "hello" to "Hello" but it's not working,neither it gives any error .Can someone please explain why it's not working.
#include <stdio.h>
int main() {
char *arr[] = {"hello" , "world"};
char **p = arr;
printf("%s\n",arr[0]);
*(*(p+0)+1) = 'H';
printf("%s\n",arr[0]);
return 0;
}
string literals are of type char[] and are stored in read only memory. You cannot alter them.
If you want to alter them you need to create a char array. What you have a is an array of pointers.
You can do this:
char foo[] = "Hello";
foo[0] = 'G';
printf("%s", foo);
Because you can't modify string literals (despite them being of an array type of non-const char). Your program invokes undefined behavior as-is.
char *arr[] = {"hello" , "world"};
arr is array of pointers to char, so you cannot modify the string literals, the pointers are pointing to.
char arr[][6] = {"hello" , "world"};
arr is 2 dimensional array of chars - or array of two strings. You can modify them, but you need to specify the length of the second dimension of the array. It needs to be big enough to hold the longest strings.
This question already has answers here:
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 10 years ago.
int main()
{
char *ch="girl";
int x=strlen(ch);
*ch=ch[x];
printf("%c",*ch);
getch();
return 0;
}
Why there is a runtime error during the assignment of a NULL value to the pointer to character?
Replace
char *ch = "girl"
with
char ch[] = "girl"
Where the former creates a pointer to immutable memory, the latter creates a char[] array of the right size and initialises it with the letters of "girl" (including the terminating zero-byte).
UPDATE: thanks to #dreamlax
"girl" is implicitly declared as a char *. But most likely your compiler is putting the string-literals into a section (rostrings) which will later be placed in a protected memory-area. When you try to assign something to *ch it will access this protected (or not depending on your platform) memory.
The compiler should warn you about the char *ch = "girl";.
And this
int x=strlen(ch); //x=4
*ch=ch[x]; //you are out of bounds of array, because first element is 0, so last is 3