M
Min-Koo Seo
I'm using Linux gcc.
Follwing program raise segmentation fault.
#include<stdio.h>
#include<string.h>
void string_or(char *str1, char *str2)
{
int i;
int len = strlen(str1);
for (i = 0 ; i < len ; i++)
{
if (str2 == '1') {
str1 = str2;
}
}
}
int main() {
char *str1="00000001000";
char *str2="00000000001";
string_or(str1, str2);
printf("result=%s\n", str1);
}
However, if I use
int main() {
char str1[]="00000001000"; // Note that I've used [], instead of *
char str2[]="00000000001";
string_or(str1, str2);
printf("result=%s\n", str1);
}
Then, the function works perfectly.
What's the problem with '*'?
Follwing program raise segmentation fault.
#include<stdio.h>
#include<string.h>
void string_or(char *str1, char *str2)
{
int i;
int len = strlen(str1);
for (i = 0 ; i < len ; i++)
{
if (str2 == '1') {
str1 = str2;
}
}
}
int main() {
char *str1="00000001000";
char *str2="00000000001";
string_or(str1, str2);
printf("result=%s\n", str1);
}
However, if I use
int main() {
char str1[]="00000001000"; // Note that I've used [], instead of *
char str2[]="00000000001";
string_or(str1, str2);
printf("result=%s\n", str1);
}
Then, the function works perfectly.
What's the problem with '*'?