[问题] C 使用pointer 传array的问题

楼主: CoSNaYe ( ~~)   2015-01-04 15:59:43
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
X-code 6.1.1
我是一个C 语言的自学新手,正在自修"C语言程式设计 -蔡文能" 这本书。
做到Exercise 5-3 的时候,遇到一点问题想请问一下大家
"请使用指标的观念写一个strcat(s, t)的函数,将字串t抄到字串s的尾端"
以下是我写的程式码, 基本上跟解答几乎一模一样,
但怎么跑原始的那个字串(ori) 都不会改变,请问是出了什么问题?
我在十三诫那边看到一个 "不能用char* 改变字串常数"
是因为出在这个问题吗?
====================================================================
#include <stdio.h>
void strcat_user (char *, char *);
int main(int argc, const char * argv[]) {
char ori[30] = "hello, ";
char add[] = "world.";
strcat_user(ori, add);
//strcat_user(&ori[0], &add[0]);
printf("%s\n", ori);
return 0;
}
void strcat_user (char *ori, char *add){
while (*ori++)
;
while ((*ori++ = *add++))
;
}
==========================================================
如果是的话, 请问要怎么改?
因为我也试过把function 改成
void strcat_user (char *ori, char *add)
然后用标注的方式去把新字串加进去
int i =0;
while ( ori[i] != '\0')
i++;
while ((ori[i] = *add++))
;
但还是fail
麻烦大家了
感谢
作者: holydc (のヮの)   2015-01-04 21:36:00
蔡神!!
楼主: CoSNaYe ( ~~)   2015-01-04 22:45:00
我朋友推荐我这本的,原来他这么强
作者: tj386 (最爱欧阳三姊妹♥♥♥)   2015-01-05 00:47:00
while (*(ori++)) while (*(ori++) = *(add++)) 要括号

Links booklink

Contact Us: admin [ a t ] ucptt.com