看板Programming
标 题Re: 还是同一个问题 (c语言)
发信站HiNetNews (Wed Jun 11 02:19:15 2008)
转信站ptt!ctu-reader!ctu-gate!news.nctu!news.ntu!feeder.seed.net.tw!netnews!
如果把str3 配置空间,那麽即使 //<----- 这一行存在
执行也会正确
int main(void){
char *str1="abcd";
char *str2 = "1234"; //<-----
char *str3;
str3= (char*) malloc(10);
strcpy(str3,str1);
printf("str3:%s\n",str3);
exit(0);
}
"Alien" <
[email protected]> 撰写於邮件新闻:
[email protected]...
>※ 引述《"mynews" <
[email protected]>, 看板: Programming》之铭言:
> : #include <stdio.h>
> : #include <stdlib.h>
> : #include <string.h>
> : int main(void){
> : char *str1="123";
> : char *str4="456";
> : char *str3="789";
> : strcpy(str3,str4);
> : exit(0);
> : }
> : 这程式编译会过,但是执行会出现错误;string 在使用前需要配置空
> : 间,不过我常在很多opensource 看到这类的用法
> : char *__progname = "prog";
> : 这样用到底是不是正确的用法?安全的使用字串应该使用阵列?
> : char str[20]="prog";
> : 那 chr *string[50] ; (字串阵列) 这种用法安全吗?
> : 另外也有人这样使用 chr str[]="prog";
> : 到底那些用法是安全且正确?
>
> 问题不在於配置与否, 而是
> 你的 strcpy 的目标位置是不
> 能改变的 (指向 string literal)
>
> alien
>
> --
>
※ 发信站: 批踢踢实业坊(ptt.cc)
> ◆ From: 202.155.236.82
>