A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© shao 中级黑马   /  2015-5-26 01:19  /  909 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

最近在做练习题,如下。。。
想问为什么这样不行。。。。。。。
/*
编写一个函数void strlink(char s[], char t[])
将字符串t连接到字符串s的尾部
*/

  1. #include <stdio.h>
  2. #include <string.h>

  3. void strlink(char s[], char t[]);

  4. int main()
  5. {
  6.     char s1[] = "afafaf";
  7.     char t[] = "sfgthyj";
  8.     strlink(s1, t );
  9.     printf("%s\n", s1);
  10.     return 0;
  11. }

  12. void strlink(char s[], char t[])
  13. {
  14.     int i = 0;
  15.    while (s[i] != 0)
  16.        {
  17.            i++;
  18.        }
  19.     int j = 0;
  20.     while ( t[j] != 0)
  21.     {
  22.         s[i] = t[j];
  23.         i++;
  24.         j++;
  25.     }
  26. }
复制代码

6 个回复

倒序浏览
蜡笔小炎 发表于 2015-5-26 01:52
strlink(char s[], char t[])
你这样往函数里面传数组是不可以的,传进去的等同于*s和*t,只是数组的首地址 ...

数组的首地址就是数组的地址,这个应该是没有问题的。
问题应该使第二个while循环的条件,我就没想明白为啥不行。。。
回复 使用道具 举报
蜡笔小炎 发表于 2015-5-26 10:40
你没明白我的意思,strlink(char s[], char t[])传入的不是数组,只是*s和*t两个指针。
所以你里面用i++和 ...

 我这样改???

#include <stdio.h>
#include <string.h>

void strlink(char s[], char t[]);

int main()
{
    char s1[] = "afarqwhg";
    char t[] = "sfaqxskghd";
    strlink(s1, t );
    printf("%s\n", s1);
    return 0;
}

void strlink(char s[], char t[])
{
    int i = 0;
   while (s != 0)
       {
           i++;
       }
    int j = 0;
    while ( t[j] != 0)
    {
        s = t[j];
        i++;
        j++;
      
    }
     s ='\0';
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马