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
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 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.
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)
Closed 7 years ago.
If I want to change the pointed value without changing the pointer's address, what should I do?
In 3, *d=*b will return segmentation fault in running time. I think it is because the *d is not pointed to any memory space. This also explains why 2, works. However, for 1, *a and *b pointed to memory with same size, why it will return segmentation fault here?
void main(void){
char *a="abcd";
char *b="1234";
char *c=(char *)malloc(sizeof(char));
char *d;
*a=*b; //1.segmentation fault
*c=*b; //2.ok
*d=*b; //3.segmentation fault
}
Constant strings are usually stored in read only memory and you can't modify them.
To make them writable create a copy first:
char *a = strdup("abcd");
This is because strings, such as "abcd" are constant in C. It is undefined behaviour to try to modify one.
If you want the string to be modifiable, you have to declare it like this:
char a[] = "abcd";
This will declare a local array and initialise it with the content of the string (which is different from declaring a pointer which points to the string)
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 7 years ago.
Can someone explain why
char *s1 = "abcd";
char *s2 = s1;
s1[0] = "z";
s1[2] = "\0";
gives me a bus error 10 BUT
char s1[] = "abcd";
char *s2 = s1;
s1[0] = "z";
s1[2] = "\0";
doesn't?
are char *s1 and char s1[] not equivalent? Please explain, thanks.
Be free (of historical lazyness), be wise! Pointers are not arrays! Tutorials have lied you!
In the first example, you're modifying a pointer to a constant string literal, and that's undefined behaviour. Anything can happen then!
Meanwhile, in the second case, the string itself is stored inside the array, and the array itself is in the stack. Thus, the second example exposes more than a plain innocent array that's modifiable.
The s2 pointers make no difference in all this. IMHO, the fact that the first case is compilable is just historical lazyness, otherwise known as backwards compatibility.
BTW: Are you assigning string literals to chars? That's undefined behaviour too!
In the first case you set a s1 pointer to the address const string. Const string are stored in read -only area and you cannot modify it. This means that you cannot modify a character s[x]. It is UB
In the second case you declare a local array inited with a string. In this case only the init value is read-only and after init you use an allocated array that can be modified.
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.