#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int i = 0, num = 0;
double j = 0;
char *str = NULL;
printf("Enter the number: ");
scanf("%d", &num);
for (i = 0; i < num; i ) {
j = i / 3 i;
printf("j = %lf \n", j);
}
strcpy(str, "Success!");
printf("%s\n", str);
return 0;
}
{in this code, I input 3 and i got error like this
j= 0.000000
j= 1.000000
j= 2.000000
signal: segmentation fault (core dumped)
and I want to print j= 0.000000
j= 1.000000
j= 2.000000
Success!
when i try debug, i got error maybe strcpy(str, "Success!"); what can i do?}
CodePudding user response:
You forgot to allocate memory to the str
variable. As soon as you try to access it, for instance in the strcpy
function, you get a segmentation fault, because you are accessing address NULL, which is 0, which is illegal. The OS catches it and terminates the program with the error you are seeing.
You need to allocate memory for your string. You can do it dinamically, as the code below:
Security risk
Please note that I am using a 100 characters array, if you write more than 100 characters you will encounter more errors, similar to the segfault but sometimes trickier to debug, as you may overwrite legal parts of the memory and corrupt your data without knowing it).
There are ways to mitigate this, for instance using strncpy(str, "Success!",100);
which will write exactly 100 bytes, and stop even if there is no string terminator.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFLEN 100
int main(void)
{
int i = 0, num = 0;
double j = 0;
char *str = NULL;
str = malloc(BUFLEN); // Allocate for 100chars
memset(str,0,BUFLEN); // Init with zeros, for NULL terminated string
printf("Enter the number: ");
scanf("%d", &num);
for (i = 0; i < num; i ) {
j = i / 3 i;
printf("j = %lf \n", j);
}
strncpy(str, "Success!",BUFLEN);
printf("%s\n", str);
return 0;
}
Or statically
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFLEN 100
int main(void)
{
int i = 0, num = 0;
double j = 0;
char str[BUFLEN] = {0};
printf("Enter the number: ");
scanf("%d", &num);
for (i = 0; i < num; i ) {
j = i / 3 i;
printf("j = %lf \n", j);
}
strncpy(str, "Success!",BUFLEN);
printf("%s\n", str);
return 0;
}
The malloc
approach gives more flexibility and it is something you will need to learn at some point, but it is very risky if you don't know what is happening. So I can suggest first to understand deeply why your code was not working, and secondly to learn the difference between the stack and the heap.
CodePudding user response:
Here's your problem:
char *str = NULL;
...
strcpy(str, "Success!");
You're telling strcpy
to copy the string literal to an invalid location (NULL
), hence the segfault.
You have two choices here:
- You can assign the string literal directly to
str
:
This doesn't copystr = "Success!";
"Success!
tostr
; instead, it stores the address of the string literal tostr
. However, if you do this, you should declarestr
asconst char *
so the compiler will yell at you if you try to modify the contents of the string (behavior of writing to string literals is undefined). - Declare
str
as an array ofchar
large enough to hold the string plus the terminator, not a pointer:char str[10]; ... strcpy(str, "Success!");