C-Segmentation Fault error [duplicate] - c

This question already has answers here:
What is a segmentation fault?
(17 answers)
Closed 4 years ago.
Recently i have started working on built-in functions but came up with an error and that is:
Why am i getting segmentation fault for this program
#include<stdio.h>
#include<ctype.h>
int main()
{
char str[50];
int n;
printf("Who is your best friend? ");
scanf("%s",str);
n=isalpha(str);
if(n!=0)
{
printf("Is Alpha");
}
else
{
printf("Invalid Input");
}
return 0;
}
Please help me out...

isalpha()'s prototype is
int isalpha( int ch );
The argument is of type int. But the one that you are passing is of type char * since str is a character array.
Perhaps you meant
unsigned char str;
scanf("%c",&str);
isalpha() returns 0 if its argument is not alphabetic.
And to avoid overflow, you could modify your scanf() to
scanf("%49s",str);
with one character to store the \0 character.
Have a look at this post.
Edit: The argument of isalpha() shouldn't be char. It must be at least unsigned char as explained here. Thanks to melpomene for pointing this out.

Related

Getting a segmentation fault but cannot see error? [duplicate]

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.

Character type pointer memory allocation? [duplicate]

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.

String comparison in if statement not working [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 6 years ago.
I am new in C programming. Can you please advise, what's wrong with my code? It looks like the if statement is not working, and instead it's jumping and printing the else statement.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char profession;
printf("what is your profession? \n");
scanf(" %s", profession);
if(profession==“QA”)
{
printf(“Go and Test\n“);
}
else
{
printf("Do whatever you want");
}
return 0;
}
First of all, you can't compare strings like that in C. use strcmp or strncmp instead.
Secondly, in your code, profession is a char, and you want to put a string (several chars) in it. It won't work. You can create a char * (pointer on a char) (without forgetting to malloc() it) or a char [] (a char array).
Firstly, strings in C are array of characters, so you have to declare profession as a pointer, pointing to an array of characters. So that statement will look like this char* profession, secondly, you have to use a method called strcmp(char* a, char* b) that accepts two char pointers. This will return 0 if they are equal. I will include the answer, but I assume there are better ways of writing this code.
int main() {
char* profession;
char* compare = "QA";
printf("What is your profession?\n");
scanf(" %s", profession);
if(strcmp(profession, compare) == 0) {
printf("Go and Test\n");
} else {
printf("Do whatever you want");
}
return 0;
}

assigning a char to a string [duplicate]

This question already has answers here:
why this program doesn't give the expected output? [duplicate]
(2 answers)
Closed 7 years ago.
I'm stuck here trying to understand why this assignement can't work in this way in C. What I'm trying to do is substitute all space occurrences with underscore char. (output: Hi_from_Synchronyze)
I saw that the problem comes when I try to do this..
s[n]='_';
the complete code is this one
#include <stdio.h>
#include <stdlib.h>
char *underscore(char *s, int n);
int main()
{
printf("%s", underscore("Hi from Synchronyze", 0));
return 0;
}
char *underscore(char *s, int n)
{
if(s[n]=='\0')
return s;
else {
if(s[n]==' ') {
s[n]='_';
return underscore(s, n+1);
}
else return underscore(s, n+1);
}
}
I'd like to know what's going on behinde and why this happens, not the solution.
Thank you very much in advance
String literals are read-only, so you can't assign to them.
Make a mutable copy of the string first, something like:
char text[] = "Hi from Synchronyze";
printf("%s", underscore(text, 0));

segmentation fault run time error [duplicate]

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 9 years ago.
i have written a code . this looks as follows .
#include<stdio.h>
#include<string.h>
int do_print2(char *q[]);
int main()
{
char *p[]={"pointervaule","NAM", "JAM", "CALM"};
do_print2(p);
return 1;
}
int do_print2(char *p[])
{
printf("this is print1 char *p \n");
strcat(p[0],"added");
printf("%s\n", (p[0]));
return 1;
}
after compilation, i am trying to run it, i am getting segmentation fault. help me in learning what is the reason for that error. thank in advance.
In your code: strcat(p[0],"added"); try to write on read only memory that is illegal in C. Because p[0] points to a constant string.
Not p is pointer to char array, but not 2-dimensional char array.
Read: Difference between char* str[] and char str[][] and how both stores in memory? an answer with diagrams and code examples, to understand it better.
The OS says that the C strings are in the read section of the object (i.e. protected from writing).
Due to historical reasons "bla bla" is really a const char * const data type, but is allowed to get away in the C compilers eyes some teenage interdependencies. But the headmaster (OS) is less forgiving and expels the running of such code in the corridors. (how many metaphors in that statement).
You can't write to read only memory, better way to do this:
#include<stdio.h>
#include<string.h>
int do_print2(char q[][20]);
int main()
{
char p[4][20] = {{0}, {0}, {0}, {0}};
strcat(p[0],"pointervaule");
strcat(p[1],"NAM");
strcat(p[2],"JAM");
strcat(p[3],"CALM");
do_print2(p);
return 1;
}
int do_print2(char p[][20])
{
printf("this is print1 char *p \n");
strcat(p[0],"added");
printf("%s\n", (p[0]));
return 1;
}

Resources