| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 691 人关注过本帖
标题:复制字符串 老是程序报错 求指点
取消只看楼主 加入收藏
nixk
Rank: 2
等 级:论坛游民
帖 子:32
专家分:12
注 册:2014-9-10
结帖率:83.33%
收藏
已结贴  问题点数:20 回复次数:1 
复制字符串 老是程序报错 求指点
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void)
{
    char *s1 = (char *)malloc(sizeof(char)*20);
    int n;
    int i;
    printf("give me a string: ");
    //gets(s1);    //用这个输入可不可行?
    scanf("%s",s1);
    if(s1 == NULL)
        return 1;
    n = strlen(s1);
    char *s2 = (char *)malloc((n+1)*sizeof(char));
    if(s2 == NULL)
        return 1;
    for(i = 0;i < n; i++)
        s2[i] = s1[i];
    s2[n] = '\0';
    printf("%s",s2);
    free(s1);
    free(s2);
    return 0;
}

[ 本帖最后由 nixk 于 2014-9-10 23:04 编辑 ]
2014-09-10 22:17
nixk
Rank: 2
等 级:论坛游民
帖 子:32
专家分:12
注 册:2014-9-10
收藏
得分:0 
我用vc6.0编译的 报错了 是怎么回事呢
用gcc就可以
--------------------Configuration: copyStr - Win32 Debug--------------------
Compiling...
copyStr.c
d:\vc_proj\copystr.c(16) : error C2143: syntax error : missing ';' before 'type'
d:\vc_proj\copystr.c(17) : error C2065: 's2' : undeclared identifier
d:\vc_proj\copystr.c(17) : warning C4047: '==' : 'int ' differs in levels of indirection from 'void *'
d:\vc_proj\copystr.c(20) : error C2109: subscript requires array or pointer type
d:\vc_proj\copystr.c(20) : error C2106: '=' : left operand must be l-value
d:\vc_proj\copystr.c(21) : error C2109: subscript requires array or pointer type
d:\vc_proj\copystr.c(21) : error C2106: '=' : left operand must be l-value
d:\vc_proj\copystr.c(24) : warning C4022: 'free' : pointer mismatch for actual parameter 1

copyStr.obj - 1 error(s), 0 warning(s)

[ 本帖最后由 nixk 于 2014-9-11 13:45 编辑 ]
2014-09-11 12:49
快速回复:复制字符串 老是程序报错 求指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026774 second(s), 10 queries.
Copyright©2004-2025, BC-CN.NET, All Rights Reserved