This question already has answers here:
What is the meaning of "wild pointer" in C?
(11 answers)
What is a segmentation fault?
(18 answers)
Closed 7 months ago.
Instead of pointer char *s if I use array char s[100] (in line 4) the code gets executed. Why is that?
#include<stdio.h>
int main(){
int l=0;
char *s;
printf("enter string : \n");
fgets(s,100,stdin);
puts(s);
while(s[l]!='\0'){
l++;
}
printf("length : %d",l-1);
return 0;
}
Because you haven't allocated any memory for s, nor even initialized it to a known value.
That means fgets() is scribbling onto some random address, and your program crashes.
With a local char s[100]; you allocate 100 bytes on the stack, and the value of s is a pointer to the zeroth byte of that allocation.
Related
This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed 5 months ago.
#include <stdio.h>
void swapchars(int Q,char* L){
char *ptr;
for(int i=0;i<Q-1;i+=2){
*ptr= *(L+i);
*(L+i)= *(L+i+1);
*(L+i+1)= *ptr;
}
}
int main() {
// Write C code here
int N;
scanf("%d",&N);
char str[N+1];
scanf("%s",str);
swapchars(N,str);
printf("%s",str);
return 0;
}
I don’t know what is wrong with this code it keeps generating segfault although when I used the same loop directly in the main function without an external function the code was working properly
You haven't initialized ptr to anything, so dereferencing it is undefined - you are writing to a random place in memory.
Think about whether you really need a pointer to store the intermediate value.
This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed 4 years ago.
Here is a simple program that is a function which checks for the character 'a' within a string, then returns the character if found, and NULL if it is not found. I am not really sure if it is the function or the call of the function itself, here is the code.
#include <stdio.h>
char *find_char(char *str, char character);
int main(){
char *str;
printf("str\n");
scanf("%s", str);
printf("%c",*find_char(str,'a'));
return 0;
}
char *find_char(char *str, char character){
char *pstr = str;
while(*pstr!='\0' && *pstr!=character){
pstr++;}
if (*pstr!=character)
return NULL;
else
return pstr;
}
Your problem basically lies in these two lines, the first and third code line of your main function:
char *str; // Create pointer, pointing to ***arbitrary*** memory.
scanf("%s", str); // Write to that memory, undefined behaviour.
You need to create backing storage for the pointer so you have somewhere valid to write your input to.
A better idea would be to use a rock-solid input routine rather than relying on often-dodgy practices like writing to invalid memory, or allowing uncontrolled input into limited-size buffers. One such beast can be found here.
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)
Why is this string reversal C code causing a segmentation fault? [duplicate]
(8 answers)
Closed 5 years ago.
#include<stdio.h>
#define ASIZE 50
void Reverse(char *str){
int Asize,i=0;
char temp;
// Find the length of the string
while(*(str+i)!='\0'){
i++;
}
Asize=i;
// string reverse
for(i=0;i<(Asize/2);i++){
temp = *(str+i);
//may be below is some error with first input method 1
//but for input method 2 it works perfectly
*(str+i) = *(str+(Asize-(i+1)));
*(str+(Asize-(i+1))) = temp;
}
}
int main()
{
//input method 1. (error aries while i pass the pointer as argument)
char *s = "abcxyz";
//input method 2 (works perfectly while as function parameter)
char s[ASIZE];
scanf("%s",s);
Reverse(s);
printf("%s",s);
}
In the main the input method 1 not working perfectly for the reverse of the string, but the method 2 works.
The concept of mine is not clear with the memory representation of char pointer. Maybe I am not good to make the question correctly but Would someone please make me clear why the method 1 is not working. Thanks in advance for the help.
"abcxyz" is actually a const char[7] type that can decay to a const char* in certain situations.
It is not a char* type; the behaviour on attempting to modify the string is undefined.
char s[ASIZE]; on the other hand is an array of char with automatic storage duration. You are free to modify any element of that as you please.
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 5 years ago.
I am trying to reverse a string literal with the use of pointers, though my code received SEGSIGV signal on *head=*tail line
char* reverse(char* input, int n) {
char temp;
char* head= input;
char* tail= &input[n-1];
while(head<tail){
temp=*head;
*head=*tail;
*tail=temp;
head++;
tail--;
}
return input;
}
int main(void){
char* sentence= "All work and no play makes jack a dull boy";
reverse(sentence, strlen(sentence));
printf("%s", sentence);
return 0;
}
I don't know if I am trying to access restricted memory segment.
Yes, you are trying to modify read-only memory.
You cannot rearrange a constant string.
With a simple change you can fix it:
char sentence[] = "All work and no play makes jack a dull boy";
(use an array instead of a 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[]"?
(19 answers)
Closed 6 years ago.
Here is the code I tried:
void check(char*, int);
int main(){
int len;
char* str="stack over flow";
len=strlen(str);
check(str,len);
getch();
return 0;
}
void check(char* str,int len)
{
char* arr;int i=0;
arr=(char*)malloc(sizeof(char)*len);
arr="over flow stack";
while(arr[i]!='')
{
str[i]=arr[i];//here the error is thrown
i++;
}
puts(str);
}
when I tried to print back elements of arr into str I'm getting error.
You're first allocating memory with malloc, then you're changing that pointer to point to "over flow stack", then you're trying to copy characters from "over flow stack" over the constant string "stack over flow", which has undefined behaviour, thus your program crashes.
Using a literal string doesn't allocate any memory - it's being put into the read only space of your program. You can't update this piece of memory.
To get memory you can overwrite you could do something like char* str = strdup("stack over flow");
You're still going to run into issues if the arr in the check function is longer than str