Chinaunix

标题: 一个字符串处理程序,core dump了,请帮忙看看是哪里出问题了 [打印本页]

作者: 我是个野鸭子    时间: 2014-10-21 17:01
标题: 一个字符串处理程序,core dump了,请帮忙看看是哪里出问题了
本帖最后由 我是个野鸭子 于 2014-10-21 17:04 编辑

编译环境RHEL6.4+GCC环境
具体源代码如下:

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

  3. void main(void);
  4. char* itoa(int, char*, int);

  5. void main(void)
  6. {
  7.   int num = 100;
  8.   char str[25];

  9.   itoa(num, str, 10);
  10.   printf("The number 'num' is %d and the string 'str' is %s. \n", num, str);
  11. }

  12. char* itoa(int value, char* result, int base)
  13. {

  14.   char* ptr = result, *ptr1 = result, tmp_char;
  15.   int tmp_value;

  16.   //check that the base if valid
  17.   if (base < 2 || base > 36) { *result = '/0'; return result; }
  18.   do {
  19.     tmp_value = value;
  20.     *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"
  21.       [35 + (tmp_value - value * base)];

  22.   } while (value);

  23.   //apply negative sign
  24.   if (tmp_value < 0) *ptr++ = '-';
  25.   *ptr-- = '/0';

  26.   while (ptr1 < ptr)
  27.   {
  28.     tmp_char = *ptr;
  29.     *ptr-- = *ptr1;
  30.     *ptr1++ = tmp_char;
  31.   }


  32.   return result;
  33. }
复制代码
请问什么地方core dump了?
还有代码中如下行是什么意思?
    *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"
      [35 + (tmp_value - value * base)];
作者: bruceteen    时间: 2014-10-22 08:56
抄代码要严肃点儿
http://www.strudel.org.uk/itoa/
作者: socay2    时间: 2014-10-22 23:02
  1. "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"
  2.       [35 + (tmp_value - value * base)];
复制代码
字符串表达式的结果 你可以当做一个指向这个串首地址的指针




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2