免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: liudengme
打印 上一主题 下一主题

string copy problem [复制链接]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
11 [报告]
发表于 2005-07-12 09:30 |只看该作者

string copy problem

又是透支银行帐户,读写未授权空间的问题。

论坛徽章:
0
12 [报告]
发表于 2005-07-12 09:31 |只看该作者

string copy problem

原帖由 "zhf3690" 发表:
前面定义了常量指针,使不可以被重写的。可能出错。
还有就是长度问题。

char* p = "abc";
p还是可以被赋其他值的吧,你是什么意思呢。。。。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
13 [报告]
发表于 2005-07-12 09:40 |只看该作者

string copy problem

原帖由 "jovesky" 发表:

char* p = "abc";
p还是可以被赋其他值的吧,你是什么意思呢。。。。


Some environment it can, and some environment it can NOT.

论坛徽章:
0
14 [报告]
发表于 2005-07-12 09:53 |只看该作者

string copy problem

原帖由 "jovesky" 发表:

char* p = "abc";
p还是可以被赋其他值的吧,你是什么意思呢。。。。

p可以指向另一个字符串,但不可以对p原来指向的字符串进行元素级修改。
请看下例(WIN2000+VC6.0):
  1. /**********************************************
  2. *file name    : pointer_string.c
  3. *description : char* p = "abc";
  4.                         p还是可以被赋其他值的吧
  5. *note           : p可以指向另一个字符串,但不能对
  6.                字符串中的特定位做修改。
  7. *author       : kernelxu
  8. *resource    :http://bbs.chinaunix.net/forum/viewtopic.php?                                    t=575640&show_type=
  9. *date          : 2005/07/12
  10. ***********************************************/

  11. #include <stdio.h>;

  12. /************************************************
  13. *function name :      main()
  14. *description      :   主函数
  15. *paramas         :   none
  16. *returns           :   int型
  17. *************************************************/
  18. int main(void)
  19. {
  20.         char *pstr = "abcdef";
  21.                 char *str  = "ghijkl";

  22.         //*pstr = 'A'; /*这样就要出错了:非法地址访问*/
  23.         pstr = str;    /*这样可以*/

  24.         printf("Print string pstr: %s\n", pstr);
  25.         printf("Print string str : %s\n", str);

  26.         return 0;
  27. }/*END OF main()*/
复制代码
我想,zhf3690是这个意思。

论坛徽章:
0
15 [报告]
发表于 2005-07-12 11:14 |只看该作者

string copy problem

是的,指向常量的指针还可以用来赋其它值,但是指向的常量空间的值不能被修改。

论坛徽章:
0
16 [报告]
发表于 2005-07-13 01:20 |只看该作者

string copy problem

1, Garbage book indeed !! throw it away!!
2, char *a = "I am a teacher.";  
    char a[] = "I am a teacher.";
   are totally different.
3, be ware if the len of src string is larger than the len of dest string.

4, my code

  1. #include <stdio.h>;
  2. #include <stdlib.h>;
  3. #include <string.h>;

  4. void copy_string(char *, char *);

  5. int main(void) {
  6.    char a[] = "I am a teacher.";
  7.    char b[] = "You are a student.";

  8.    printf("\nstring a = %s\nstring b = %s\n", a, b);
  9.   // copy_string(a, b);
  10.    copy_string(b, a);
  11.    printf("\nstring a = %s\nstring b = %s\n", a, b);
  12.    return 0;
  13. }

  14. void copy_string(char *from, char *to) {

  15.         int lenf = strlen(from);
  16.         int lent = strlen(to);

  17.         if(lenf>; lent){

  18.              char * to= (char *)malloc(lenf);
  19.         }

  20.     for(; *from != '\0'; from++, to++) {
  21.               *to = *from;
  22.             //  putchar(*to);

  23.            }
  24.    *to = '\0';
  25. }
复制代码

论坛徽章:
0
17 [报告]
发表于 2005-07-13 09:26 |只看该作者

string copy problem

char *a = "I am a teacher.";
char *b = "You are a student.";
这是定义了2个字符串常量,你能让2=3 吗?

论坛徽章:
0
18 [报告]
发表于 2005-07-13 11:39 |只看该作者

string copy problem

[quote]原帖由 "kernelxu"]想,zhf3690是这个意思。[/quote 发表:

恩,明白了,是应该这样的。。。
不过对于飞行员说的环境问题,我不太清楚哦。。。。

论坛徽章:
0
19 [报告]
发表于 2005-07-13 20:49 |只看该作者

string copy problem

2, char *a = "I am a teacher.";   
   char a[] = "I am a teacher.";
  are totally different.

为什么不一样??

论坛徽章:
0
20 [报告]
发表于 2005-07-13 23:22 |只看该作者

string copy problem

char *a = "I am a teacher.";   
我没记错的话,应该是把字符串长量的地址,给了a这个指针变量.

而char a[] = "I am a teacher.";
是把a的连续地址空间里放入这个字符串!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP