鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: aleiluyes

c语言return数组问题

[复制链接]
发表于 2013-7-17 09:02:57 | 显示全部楼层

你这认识有误,是数组名代表一个地址,而不是数组的第一个(啥叫数组的第一个?木听过,是数组的第一个元素吗?)
数组名相当于一个指针常量(常量指针和指针常量 请百度,一堆一堆的),指向了数组中的第一个元素的地址!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-17 12:07:42 | 显示全部楼层
我都把这个符号给你打出来了,你居然还问我怎么用。。。我汗。。“&”
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-17 12:32:33 | 显示全部楼层
好吧,看来这50鱼币还没送出去。我写段给你吧!
这台电脑没有VC,我就手动写出来,错不错,大概意思你明白就行好吧。代码如下:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-17 12:36:53 | 显示全部楼层
#include iostream
using namespce std;
int main()
{
      int a[10];
      jiafa(&a,10);
      getchar();
      return 0;
}
void jiafa(int &x,int n)
{
    for(i=0;i<n;i++)
      {
         x[i]++;
      }
}

评分

参与人数 1鱼币 +5 收起 理由
aleiluyes + 5 谢谢回答

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-17 12:39:05 | 显示全部楼层
忘了安家
void jiafa(int &x,int n)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-17 13:09:54 | 显示全部楼层
你的局部变量在调用的函数过程结束后就自动销毁了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-17 16:55:44 | 显示全部楼层
怎嘛不给分啊????
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-17 17:36:17 | 显示全部楼层
刀下留人 发表于 2013-7-17 12:36
#include iostream
using namespce std;
int main()

运行不了T^T 貌似你写的是c++
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-17 18:45:22 | 显示全部楼层
你的原题没有必要将数组值传递啊。我也随手写一个递归版本,帖出来大家一起看看吧
  1. /*
  2. 3-5:编写函数itob(n,s,b),将整数n转换为以b为低的数,并将转换结果以字符的形式保存到字符串s中。例如,itob(n,s,16)把整数n格式化成十六进制整数保存在s中。
  3. */

  4. #include <stdio.h>
  5. #include <string.h>
  6. #define MAX_SIZE 50
  7. void itob( unsigned int n, char result[], unsigned int b );

  8. int main() {
  9.         char result[MAX_SIZE];
  10.         unsigned int test = 255;
  11.         itob( test, result, 2 );
  12.         puts(result);

  13.         itob( test, result, 8 );
  14.         puts(result);

  15.         itob( test, result, 16 );
  16.         puts(result);
  17. }

  18. static void reverse( char* str );
  19. static void itob_real( unsigned int n, char result[], unsigned int b );

  20. void itob( unsigned int n, char result[], unsigned int b ) {
  21.         if( n==0 ) {
  22.                 strcpy( result, "0" );
  23.                 return;
  24.         }
  25.         itob_real(n,result,b);
  26.         reverse(result);
  27. }
  28. static void itob_real( unsigned int n, char result[], unsigned int b ) {
  29.         static char map[16]="0123456789ABCDEF";
  30.         if( n==0 ){
  31.                 result[0]='\0';
  32.         }else {
  33.                 result[0] = map[n%b];
  34.                 itob_real( n/b, result+1, b );
  35.         }
  36. }

  37. static void reverse( char* str ) {
  38.         int start=0, end = strlen(str)-1;
  39.         while( start < end ) {
  40.                 char tmp = str[start];
  41.                 str[start]=str[end];
  42.                 str[end]=tmp;
  43.                 ++start;
  44.                 --end;
  45.         }
  46. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-18 01:06:53 | 显示全部楼层
aleiluya 发表于 2013-7-17 17:36
运行不了T^T 貌似你写的是c++

我不能保证代码完全正确。但是运行是可以的。一般都能通过!下面我追加了一句函数没有安家。你没看到?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-18 01:16:54 | 显示全部楼层
万变不离其踪!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-18 01:17:27 | 显示全部楼层
不管怎么样,总算帮你解决了!虽然不是我解决的!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-18 03:50:30 | 显示全部楼层
本帖最后由 aleiluya 于 2013-7-18 03:51 编辑

哈哈,首先谢谢大家的积极回答,今天准备进行练习3-6的时候看到itob,就想回去看看以前的练习就去看了已经完成的3-4,正如同我所说的数组的第一个就是地址,传输数组时就是在传送地址,所以根本不需要return,因为在修改被调用函数数组时就是在修改调用函数数组所以:
  1. /*
  2.     编写函数itob(n,s,b),将整数n转换为以b为低的数,并将转换结果以字符的形式保存到字符串s中。例如,itob(n,s,16)把整数n格式化成十六进制整数保存在s中。
  3.   */
  4. #include <stdio.h>

  5. char hex(int n, char s[]);
  6. char itob(int n, char s[], int b);
  7. char binary(int n, char s[]);

  8. void main()
  9. {

  10.         int n, b;
  11.         static char s[50];

  12.         printf("请安以下的格式输入:你想要转换的数 以什么形式(只限二进制和十六进制)\n");
  13.         while (1)
  14.         {
  15.                 scanf("%d %d", &n, &b);
  16.                 if ((b == 2) || (b == 16))
  17.                 {
  18.                         itob(n, s, b);  //只需要调用,不需要return。
  19.                         printf("%s\n", s);  //函数的调用就是在修改数组。
  20.                 }
  21.                 else
  22.                 {
  23.                         printf("输入的格式错误,请重新输入!\n");
  24.                 }
  25.         }
  26. }

  27. /******************************************************/
  28. char itob(int n, char s[], int b)  //本问题函数
  29. {
  30.         switch (b)
  31.         {
  32.         case 2:

  33.                 binary(n, s);   //只需要调用,不需要return。
  34.         
  35.                 break;
  36.         case 16:

  37.                 hex(n, s);  //只需要调用,不需要return。

  38.                 break;
  39.         }

  40. }

  41. /******************************************************/
  42. char binary(int n, char s[])
  43. {
  44.         unsigned int i, temp;                // 声明为无符号是因为不能是负数。

  45.         temp = ~0;                                        // 求得在任何机器上最大有多少位,并把结果保存在temp。

  46.         for (i = 0; temp > 0; i++)
  47.         {
  48.                 temp >>= 1;                                // 每循环右移一次就把次数保存在i。
  49.         }



  50.         i--;                                                // 减去本身的次数。
  51.         for (temp = 1; i > 0; i--)        // 把最高位弄成1,其余0。
  52.         {
  53.                 temp <<= 1;
  54.         }
  55.         i = 0;
  56.         while (temp > 0)                        // 用位操作把n以二进制形式提取出来。
  57.         {
  58.                 if ((n & temp) == 0)
  59.                 {
  60.                         s[i++] = '0';
  61.                 }
  62.                 else
  63.                 {
  64.                         s[i++] = '1';
  65.                 }
  66.                 temp >>= 1;                                // 每输出一次就右移一位,进行下一位输出准备。
  67.         }
  68.         s[i] = '\0';

  69. }

  70. // / /// /// /// /// /// /// /// /// /// /// /// ///

  71. char hex(int n, char s[])
  72. {
  73.         unsigned int i, j, temp;        // 声明为无符号是因为不能是负数。
  74.         s[0] = '0';
  75.         s[1] = 'X';
  76.         temp = ~0;                                        // 求得在任何机器上最大有多少位,并把结果保存在temp。

  77.         for (i = 0; temp > 0; i++)
  78.         {
  79.                 temp >>= 1;                                // 每循环右移一次就把次数保存在i。
  80.         }

  81.         i /= 4;
  82.         i--;
  83.         for (temp = 15; i > 0; i--)        // 把最高位弄成1,其余0。
  84.         {
  85.                 temp <<= 4;
  86.         }
  87.         i = 2;
  88.         while (temp)                                // 第一个循环把左边的空白位删除。
  89.         {
  90.                 if (n & temp)                        // 判断是否是n变量的最高位。
  91.                 {
  92.                         while (temp)                // 第二个循环取值。
  93.                         {
  94.                                 j = n & temp;
  95.                                 while (((j & 15) == 0) && j)        // 如果j不是0就把数字右移到最右边。
  96.                                 {
  97.                                         j >>= 4;
  98.                                 }

  99.                                 if (j > 9)                // 大于9以上的用字母保存。
  100.                                 {
  101.                                         s[i++] = (j - 10) + 'A';
  102.                                 }
  103.                                 else
  104.                                 {
  105.                                         s[i++] = j + '0';
  106.                                 }
  107.                                 temp >>= 4;                // 每循环一次就右移为下次做准备。
  108.                         }
  109.                 }
  110.                 s[i++] = '0';                        // 不是数值的最高位就补0。
  111.                 temp >>= 4;                                // 每循环一次就右移为下次做准备。
  112.         }
  113.         s[--i] = '\0';                                // 字符串结束标志。--是为了覆盖掉循环的0。
  114.         
  115. }
复制代码



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-18 03:53:14 | 显示全部楼层
仰望天上的光 发表于 2013-7-16 06:04
05.char try(char s[]);
这个函数返回char类型,char不是数组,也不是字符指针
printf("is:%s", try(s)); ...

哈哈,正确答案在33楼,谢谢回答。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-18 03:53:48 | 显示全部楼层
编程难 发表于 2013-7-16 09:41
上面的写法可以输出,但要意识到问题很严重。具体的等你学的再深入点慢慢理解。
顺便提一句,c/c++中不能返 ...

哈哈,正确答案在33楼,原来自己已经会了,就是忘了,谢谢回答。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-18 03:54:20 | 显示全部楼层
我是师兄 发表于 2013-7-16 10:01
C语言中,数组很容易转换为指针。和基本类型做一个对比,同样作为函数的输入参数,基本类型是值传递,而数 ...

哈哈,正确答案在33楼,原来自己已经会了,就是忘了,谢谢回答。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-18 03:55:08 | 显示全部楼层
刀下留人 发表于 2013-7-16 14:49
你传一个引用过去就可以了。不用返回,你改变的数组就是原来的数组。“&”这个是引用的标志!~

哈哈,正确答案在33楼,原来自己已经会了,就是忘了,谢谢回答。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-7-18 04:07:55 | 显示全部楼层
仰望天上的光 发表于 2013-7-17 18:45
你的原题没有必要将数组值传递啊。我也随手写一个递归版本,帖出来大家一起看看吧

菜鸟,好难看懂TAT
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-18 09:31:49 | 显示全部楼层
结贴散分吧,对楼主很无语。只要楼主会了就好!

评分

参与人数 1鱼币 +5 收起 理由
aleiluyes + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-7-19 01:12:06 | 显示全部楼层
aleiluya 发表于 2013-7-18 03:53
哈哈,正确答案在33楼,原来自己已经会了,就是忘了,谢谢回答。

哈哈!会了就好的!

评分

参与人数 1鱼币 +5 收起 理由
aleiluyes + 5

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-19 06:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表