问题描述
我很困惑。为什么以下会导致seg故障?尽管
我已经为malloc()提供了一个Hello的空间。
我明白使用固定长度的数组会有效非常好。
但我希望了解如何使用指针实现这一目标。
谢谢。
Stan
#include< stdio.h>
#include< stdlib.h>
int main(int argc,char * argv [])
{
char * Hello = NULL;
char * World = NULL;
Hello = malloc(100);
Hello =" hello";
World =" world";
strcat(Hello,World);
printf("%s \ n",Hello);
返回0 ;
}
Hi,
I''m puzzled. Why does the following cause a seg fault? Notwithstanding
that I''ve already malloc() a certain space for "Hello".
I do understand that using a fixed length array will work very well.
But I wish to find out how can this be achieve using pointers.
Thank you.
Stan
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *Hello = NULL;
char *World = NULL;
Hello = malloc(100);
Hello = "hello";
World = "world";
strcat(Hello, World);
printf("%s\n",Hello);
return 0;
}
推荐答案
HTH,
- g
-
Artie Gold - 德克萨斯州奥斯汀
如果你没有什么可隐瞒的,你就不会尝试!
HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you''re not trying!"
这篇关于strcat()和分段错误。但是malloc()已经完成了。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!