Segmentation fault (core dumped) [duplicate] - c

This question already has answers here:
Why does scanf require &?
(7 answers)
Closed 5 years ago.
Below is a block of code from my program. I'm getting error of Segmentation fault (core dumped) after entering name and age in first loop.
#include<stdio.h>
#include <string.h>
struct Cricketer
{
char name[25];
int age;
float avg_run;
};
int main(){
struct Cricketer c[3];
int i,j;
for (i=0 ; i<3;i++){
printf("Enter name: \n");
scanf("%s",c[i].name);
printf("Enter age: \n");
scanf("%d",c[i].age);
printf("Enter average run: \n");
scanf("%f",c[i].avg_run);
}
return 0;
}
And I couldn't find what is causing this program.

Your error is here:
scanf("%d",c[i].age);
Change it for:
scanf("%d",&(c[i].age));
When using %d you have to pass the address of the int variable. And the same for floats:
scanf("%f",&(c[i].avg_run));
When using scanf, the second argument has to be the address of a variable. With your variable name there is no problem because it already refers to the address of the buffer in which you want to store the string.

When using scanf, the second argument should be the address of a variable. In your program c[i].age and c[i].avg_run are the variables themselves and not the addresses. Use the & operator to get the address of a variable. For Example, &(c[i].age) or just &c[i].age.
What you are passing to scanf as mentioned above are some numbers which might or might not be valid memory addresses. Thus invoking undefined behavior.
c[i].name happens to be fine because referring to an array (name in this case) by just the name evaluates to the base address of the array.
It is a good idea to have compiler warnings enabled. More important is one reads and understands the warnings. Read your compiler manual for more info.

Related

memory address of * and & in C [duplicate]

This question already has answers here:
Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer
(5 answers)
Closed last year.
#include<stdio.h>
int main(){
int *i;
*i=1;
int a[5]={10,20,30,40,50};
printf("%d\n",&a[4]);
printf("%d\n",&a[3]);
printf("%d\n",&a[2]);
printf("%d\n",&a[1]);
printf("%d\n",&a[0]);
printf("%d\n",i);
return 0;
}
printf("%d\n",i); This print statement prints different values every time it is ran, which is understandable as the program is loading on RAM(am I right?).
But,
Then why does printf("%d\n",&a[0]);(or any other a[i]) prints the same value each time the program is run, does that mean the program is saving the array on harddisk, if so why?
Please answer
Please do share this question if you want the answer
printf("%d\n",i); This print statement prints different values every time it is ran, which is understandable as the program is loading on RAM(am I right?).
No, you're not actually initialising i so its value is random garbage: if you compile using address sanitizer (-fsanitize=address), the program will immediately explode when reaching *i = 1, and compiling with -Wuninitialized will also point to this issue.
But there's no requirement that i is stable either, when properly initialised: it depends how the allocator in use works, and where it decides to put the value in the heap.
Then why does printf("%d\n",&a[0]); (or any other a[i]) prints the same value each time the program is run, does that mean the program is saving the array on harddisk, if so why?
That's just a peculiarity of your machine, it doesn't occur on my system (that is the addres of a moves around): it depends where the runtime decides to put the stack and what it decides to do (or not) before running main (even more so if you enable stack ASLR), since a lives on the stack.
FWIW on my system, using clang, i always has the same value (when properly initialised using int *i = malloc(sizeof *i);, but the value of a moves around.
*i points to a random address anywhere
The array a[] is on stack whose (start) address may be the same each time
#include<stdio.h>
int main(){
int *i;
int a[5]={10,20,30,40,50};
//%p denotes we are printing address that a pointer points to
printf("%p\n",&a[4]);
printf("%p\n",&a[3]);
printf("%p\n",&a[2]);
printf("%p\n",&a[1]);
printf("%p\n",&a[0]);
*i=1;
printf("%p\n",i);
return 0;
}
$gcc memory_address.c && ./a.out
0x7ffe282734d0
0x7ffe282734cc
0x7ffe282734c8
0x7ffe282734c4
0x7ffe282734c0
Segmentation fault (core dumped)
$ gcc memory_address.c && ./a.out
0x7fffb1b8b670
0x7fffb1b8b66c
0x7fffb1b8b668
0x7fffb1b8b664
0x7fffb1b8b660
Segmentation fault (core dumped)
We are getting Segmentation fault because i pointer is not assigned a valid reference and array a is assigned new memory on ram every time you run your program.

Different output when printing a null char pointer with and without a newline appended [duplicate]

This question already has answers here:
What is the behavior of printing NULL with printf's %s specifier?
(4 answers)
Closed 5 years ago.
I tried the following code on Linux
#include<stdio.h>
int main()
{
char *p=NULL;
printf("%s",p);
return 0;
}
#include<stdio.h>
int main()
{
char *p=NULL;
printf("%s\n",p);
return 0;
}
The first one outputs: (null)
While the second one causes a segmentation fault.
Why does \n make such a difference?
Both of your examples are undefined behavior per standard. Calling printf with %s and passing a NULL pointer is UB.
Therefore it makes no sense to discuss the outcome. On one system you might get one result and on another system you get another result.
Also see https://stackoverflow.com/a/11589479/4386427

Why segmentation fault occur while using pointer variable?

I'm trying to read the paragraph using scanf() function in C.
So, First I tried the below code,
#include <stdio.h>
int main()
{
char Input[100];
printf("Please give the paragraph\n");
scanf("%[^EOF]\n",Input );
printf("\n\n%s\n\n",Input);
return 1;
}
It's working fine, it get the paragraph of input from stdin and print it.
Then I tried the same thing with pointer like given below,
#include <stdio.h>
int main()
{
char *Input;
printf("Please give the paragraph\n");
scanf("%[^EOF]\n",Input );
printf("\n\n%s\n\n",Input);
return 1;
}
This code also get the input and print the output properly.
But it throw the segmentation fault error while the program terminating.
So, I need to know why the error occur while terminating?
If the pointer doesn't point any memory means, How the printf() is printing the given input, and from where it is printing?
Thanks.
Since Input is a pointer to nothing in particular and has no value in particular, passing its value to scanf makes no sense. Assign it a value first -- make it point to something -- and then you can use it to tell scanf where to store things.

what is Segmentation fault (core dumped)? [duplicate]

This question already has answers here:
What is a segmentation fault?
(17 answers)
Closed 9 years ago.
I am trying to write a C program in linux that having sqrt of the argument, Here's the code:
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
int main(char *argv[]){
float k;
printf("this is consumer\n");
k=(float)sqrt(atoi(argv[1]));
printf("%s\n",k);
return 0;
}
After I type in my input at the "shell> " prompt, gcc gives me the following error:
Segmentation fault (core dumped)
"Segmentation fault" means that you tried to access memory that you do not have access to.
The first problem is with your arguments of main. The main function should be int main(int argc, char *argv[]), and you should check that argc is at least 2 before accessing argv[1].
Also, since you're passing in a float to printf (which, by the way, gets converted to a double when passing to printf), you should use the %f format specifier. The %s format specifier is for strings ('\0'-terminated character arrays).

Increment operator gives segmentation fault? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Change string literal in C through pointer?
Here is a code sample
void main()
{
char *i="prady"; printf("%c ",++*i);
}
Can anyone tell me why this code is giving a segmentation fault in gcc when I guess it should give 'q'. When I am using only *i++ it giving me the result but incase of pre-increment only it's giving me a segmentation fault.
Also tell me why void main is not a proper way to write main() function.
++*i means ++(*i). You're trying to modify the first character of a string literal, which is not permitted. As far as the C standard is concerned behavior is undefined, but this implementation has helpfully segfaulted to alert you to the problem.
*i++ means *(i++). You're modifying your pointer i, which is fine.
void main() is not a proper way to write a main function because the standard says that main returns int. The return value is used to indicate the success or failure of the program. Implementations can support other forms of main, but there are two that are required: int main(void) and int main(int argc, char *argv[]).
++*i
means that you pre-increment your pointer. for example
int *i
*i = 1;
imagine that i is a pointer to the address 0x8FF43FF0
if you compile ++*i before dereferencing i points to 0x8FF43F4
++(*i)
means, first dereference i, than increment
Steve Jessop already told you why ++*i returns an error so I'm not going to tell you that again.
*i++ will return p, the first letter of the word, because the "++" operator returns the value first, and only after had it returned the value does it increment it.
So if you want your program to print 'q' you would have to say printf("%c ",*i+1).
Also, if you want your program to print the second character, try: printf("%c ", *(i+1)). The next letter in the alphabet after the third letter of your word will be printf("%c ", *(i+2)+1) and so on.
Why you should use int main instead of void? The value returned by the main function informs the operating system about how the program ended. o (as in return 0) tells the operating system that the program had executed correctly. you usually use non 0 codes when the program must end because of an error.

Resources