| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 753 人关注过本帖
标题:ladies and gentlemen,please give a hand,a powerful hand!
只看楼主 加入收藏
cheenbee
Rank: 2
等 级:论坛游民
帖 子:53
专家分:32
注 册:2011-11-3
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:3 
ladies and gentlemen,please give a hand,a powerful hand!
#include<stdio.h>
int main(){
    void copy_string(char from[],char to[]);
    char a[]="I am a teacher.";
    char b[]="You are a student.";
    int i;
    printf("string a is:%s\nstring b is:%s\n",a,b);
    printf("copy string b to string a!\n");
    copy_string(b,a);
    printf("string a is:%s\nstring b is:%s\n",a,b);
    printf("sizeof(a)=%d\nstrlen(a)=%d\n",sizeof(a),strlen(a));
    putchar('\n');
    for(i=0;i<=18;i++)
     printf("%c",a[i]);
    putchar('\n');
    return 0;
}
void copy_string(char from[],char to[]){
    int i=0;
    while(from[i]!='\0')
    {
        to[i]=from[i];
        i++;
    }
    to[i]='\0';  
}
结果输出如下:
string a is:I am a teacher.
string b is:You are a student.
copy string b to string a!
string a is:You are a student.
string b is:You are a student.
sizeof(a)=16
strlen(a)=18

You are a student.

只有一个疑问:字符数组a在调用copy_string函数复制b数组后输出的字符串长度为18,但是所占字节数却还是16,各位可以看一下代码,便可知口齿不清的小弟所要表达的意思,问一句,这是为何,数组a 主函数调用copy_string函数前后的存储状况内容!

搜索更多相关主题的帖子: teacher include please return 
2011-11-19 11:02
cheenbee
Rank: 2
等 级:论坛游民
帖 子:53
专家分:32
注 册:2011-11-3
收藏
得分:0 
回复 楼主 cheenbee
补充一下 我是在Visual C++里面编译的····

而在winTc中#include<stdio.h>
void main(){
    void copy_string(char from[],char to[]);
    char a[]="I am a teacher.";
    char b[]="You are a student.";
   
    int i;
    printf("string a is:%s\nstring b is:%s\n",a,b);
    printf("copy string b to string a!\n");
    copy_string(b,a);

    printf("string a is:%s\nstring b is:%s\n",a,b);
    printf("sizeof(a)=%d\nstrlen(a)=%d\n",sizeof(a),strlen(a));
    printf("sizeof(b)=%d\nstrlen(b)=%d\n",sizeof(b),strlen(b));
    putchar('\n');
    for(i=0;i<=18;i++)
     printf("%c",b[i]);
    putchar('\n');
    getch();
}
void copy_string(char from[],char to[]){
    int i=0;
    while(from[i]!='\0')
    {
        to[i]=from[i];
        i++;
    }
    to[i]='\0';
   
}
结果无法复制麻烦看下图片:sorry


[ 本帖最后由 cheenbee 于 2011-11-19 11:25 编辑 ]
2011-11-19 11:06
kd83kdi93
Rank: 2
等 级:论坛游民
帖 子:65
专家分:52
注 册:2011-9-18
收藏
得分:7 
a和b数组初始化完成后就把能使用的内存空间固定了   你怎么能把元素个数大的数组中的元素一一放入小的数组?
2011-11-19 12:14
cheenbee
Rank: 2
等 级:论坛游民
帖 子:53
专家分:32
注 册:2011-11-3
收藏
得分:0 
回复 3楼 kd83kdi93
我只是想实验一下,突然想到,就做了,可是不懂原理!悲催····
2011-11-19 13:15
快速回复:ladies and gentlemen,please give a hand,a powerful hand!
数据加载中...
 
   



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

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016368 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved