免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10985 | 回复: 8
打印 上一主题 下一主题

[函数] 函数的返回值为字符串时该如何处理? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-10-03 00:23 |只看该作者 |倒序浏览
做了一个将输入的正整数转化二进制数的函数但字符串作为返回值返回到调用函数中时却出错,下面是代码,谢谢大家指正!在TC20下编译的,使用了clrscr()了。

/*Positive interger to binary code*/

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

char binary(int b){

    int i,j;
    char str[32],*pstr=&str[0],temp;

    for(i=0;i<32;i++)
    str=0;

    while(b>;0){

    if(b%2==0)
        *pstr='0';
    if(b%2==1)
        *pstr='1';

    b/=2;

    pstr++;
    }

    for(i=0,j=strlen(str)-1;i<j;i++,j--){
        temp=str;
        str=str[j];
        str[j]=temp;
    }

    return *str;
}


int startup(void){

    int a,flag=1;
    char ch;

    while(flag){

    clrscr();

    printf("\nEnter a choice: ";
    printf("\n(1) Exit\t(2) Continue\n";
    ch=getch();

    if(ch=='1'){

        clrscr();
        printf("\nBye!\n";
        getch();
        flag=0;
    }
    else if(ch=='2'){

        clrscr();

        printf("\nInput a positive integer: ";
        scanf("%d",&a);

        printf("\nThe binary code for this positive integer is %s", binary(a));

        getch();
    }
    else{
        printf("\nError Input! Press any key to input again.\n";
        getch();
        startup();
    }
    }

    return 0;
}

int main(void){

    startup();

    return 0;
}[code][/code]

论坛徽章:
0
2 [报告]
发表于 2005-10-03 00:48 |只看该作者

函数的返回值为字符串时该如何处理?

没仔细看

但看到
char str[32]
........
return *str;

就感觉不对劲了。你的函数的功能,是返回str字符串的首个字符,不过这估计不是你想要得。

出现错误是由于变量的生存期和作用域引起的。
char binary(int b){

   int i,j;
   char str[32]
这里str是一个局部自动字符数组,他的作用域限制在binary函数范围内,由binary函数返回时,他会被销毁,数据也就无效了,如果你的转换算法没错的话,这就是引起错误的原因了。

解决办法,就是
1。全局变量
2。在堆中动态分配,malloc-free函数对
3. 修改binary函数的接口,在startup分配字符数组,然后作为参数传递给binary函数,并在binary函数中用转换结果填充它

三种办法,
第一种:
在startup函数里面定义str字符数组,然后,把它作为参数传递给binary函数。
即binary函数应当定义成
void binary(int value, char buf[])
{
   ...
  填充buf
}
int startup(void)
{
    char result[33];

    ....
    binary(value, result); //调用结束后,result字符数组内应当填充转换后的字符串。
   //输出转换结果。


}

另一种,binary 返回char*

char* binary(int value)
{
    char* str = (char *)malloc(32 + 1);
    //转换成字符串,注意字符串是以'\0'结尾的。
   
    return str; //返回指针。
}
然后
int startup(void){
  ....
  char* result = NULL;
  ...
  result = binary(value);
  printf("The converted result is %s\n", result);
  //然后别忘了
  free(result); //释放动态分配的存储空间。
  ..............
}

最后一种
将char str[32]定义成全局数组。

论坛徽章:
0
3 [报告]
发表于 2005-10-03 01:09 |只看该作者

函数的返回值为字符串时该如何处理?

不是完全看懂,是我自己知识有限,但最后一种解决方案看来我可以用的,谢谢了!希望继续得到你的帮助!

论坛徽章:
0
4 [报告]
发表于 2005-10-03 01:16 |只看该作者

函数的返回值为字符串时该如何处理?

/*Positive interger to binary code*/

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

char str[32];

char binary(int b){

    int i,j;
    char *pstr=&str[0],temp;

    for(i=0;i<32;i++)
        str=0;

    while(b>;0){

        if(b%2==0)
            *pstr='0';
        if(b%2==1)
            *pstr='1';

        b/=2;

        pstr++;
    }

    for(i=0,j=strlen(str)-1;i<j;i++,j--){
            temp=str;
            str=str[j];
            str[j]=temp;
    }

    return str;
}


int startup(void){

    int a,flag=1;
    char ch;

    while(flag){

        clrscr();

        printf("\nEnter a choice: ";
        printf("\n(1) Exit\t(2) Continue\n";
        ch=getch();

        if(ch=='1'){

            clrscr();
            printf("\nBye!\n";
            getch();
            flag=0;
        }
        else if(ch=='2'){

            clrscr();

            printf("\nInput a positive integer: ";
            scanf("%d",&a);

            printf("\nThe binary code for this positive integer is %s", binary(a));

            getch();
        }
        else{
            printf("\nError Input! Press any key to input again.\n";
            getch();
            startup();
        }
    }

    return 0;
}

int main(void){

    startup();

    return 0;
}
[/code]

论坛徽章:
0
5 [报告]
发表于 2005-10-03 01:32 |只看该作者

函数的返回值为字符串时该如何处理?

我手头没有tc,所以大致改了一下,还是换个borland c++5.5或者vc,还有dev-c++吧,16位的东西,现在比较少用

  1. #include<stdio.h>;
  2. /* #include<conio.h>; */
  3. #include<string.h>;

  4. char str[32];

  5. char binary(int b){

  6.   int i,j;
  7.   char *pstr=&str[0],temp;

  8.   for(i=0;i<32;i++)
  9.     str[i]=0;

  10.   while(b>;0){

  11.     if(b%2==0)
  12.       *pstr='0';
  13.     if(b%2==1)
  14.       *pstr='1';

  15.     b/=2;

  16.     pstr++;
  17.   }

  18.   for(i=0,j=strlen(str)-1;i<j;i++,j--){
  19.     temp=str[i];
  20.     str[i]=str[j];
  21.     str[j]=temp;
  22.   }

  23.   return 0;
  24. }


  25. int startup(void){

  26.   int a,flag=1;
  27.   char ch;

  28.   while(flag){

  29.     /* clrscr();  */

  30.     printf("\nEnter a choice: ");
  31.     printf("\n(1) Exit\t(2) Continue\n");
  32.     ch=getchar();

  33.     if(ch=='1'){

  34.       /* clrscr();  */
  35.       printf("\nBye!\n");
  36.       getchar();
  37.       flag=0;
  38.     }
  39.     else if(ch=='2'){

  40.       /* clrscr(); */

  41.       printf("\nInput a positive integer: ");
  42.       scanf("%d",&a);
  43.       binary(a); //就是这里,填充全局变量。执行完毕后,str里面是转换后的字符串

  44.       printf("\nThe binary code for this positive integer is %s", str);

  45.       getchar();
  46.     }
  47.     else{
  48.       printf("\nError Input! Press any key to input again.\n");
  49.       getchar();
  50.       startup();
  51.     }
  52.   }

  53.   return 0;
  54. }

  55. int main(void){

  56.   startup();

  57.   return 0;
  58. }
复制代码

论坛徽章:
0
6 [报告]
发表于 2005-10-03 01:50 |只看该作者

函数的返回值为字符串时该如何处理?

楼上的代码我在Dev_C++中编译通过了,可以达到预期效果,谢谢。再请问关于“填充”的知识不知从哪儿可以了解更多?

论坛徽章:
0
7 [报告]
发表于 2005-10-03 02:00 |只看该作者

函数的返回值为字符串时该如何处理?

没什么可看得,就是变量的生存期和作用域、还有一点关于数组的问题。多看几遍书吧,把书上的例子是运行调试以下。所谓的‘填充’只不过是数组元素赋值操作。

论坛徽章:
0
8 [报告]
发表于 2005-10-03 02:56 |只看该作者

函数的返回值为字符串时该如何处理?

楼上老兄的经验之谈很有用啊,学习中...

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
9 [报告]
发表于 2005-10-03 06:00 |只看该作者

函数的返回值为字符串时该如何处理?

转换成2进制这么做多麻烦.用位运算就好..
况且这么写 都没考虑负数的情况.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP