A
a2x
Hi, I've fixed this error, but I don't know why it occurs. Do you?
Code:
#include <stdlib.h>
#include <string.h>
void another();
int main()
{
char* temp;
strcpy(temp, "Hi.\n");
printf(temp);
another();
return(0);
}
void another()
{
char* temp1;
strcpy(temp, "Hi again.\n");
}
Output:
Hi.
Segmentation fault
The segmentation fault refers to the second call of strcpy(). If I
don't call strcpy() the second time the program runs fine. Everything
works fine if I replace 'char* temp1' with 'char temp1 = (int*) malloc
(200)'.
Do I have to use malloc before every call to strcpy()? Is my compiler
(gcc) just randomly letting me off doing it the first time?
Thanks.
Code:
#include <stdlib.h>
#include <string.h>
void another();
int main()
{
char* temp;
strcpy(temp, "Hi.\n");
printf(temp);
another();
return(0);
}
void another()
{
char* temp1;
strcpy(temp, "Hi again.\n");
}
Output:
Hi.
Segmentation fault
The segmentation fault refers to the second call of strcpy(). If I
don't call strcpy() the second time the program runs fine. Everything
works fine if I replace 'char* temp1' with 'char temp1 = (int*) malloc
(200)'.
Do I have to use malloc before every call to strcpy()? Is my compiler
(gcc) just randomly letting me off doing it the first time?
Thanks.