关于数组声明8-12

/*代码的测试心得是声明不能用指针,要用数组带长度,否则,编译通过不能运行,显示段错误,以下是内部代码,没库的不能通过编译,旨在说明数组声明问题*/

Code:
  1. #include <stdio.h>  
  2. #include <unistd.h>  
  3. #include <string.h>  
  4. #include <stdlib.h>  
  5. #include <signal.h>  
  6. #include <sys/file.h>  
  7. #include <sys/stat.h>  
  8. #define CFO_PARA_ERR -1  
  9. #define CFO_API_OK 0  
  10. #define CFO_STRING_TYPE_UNKNOWN 0  
  11. #define CFO_STRING_TYPE_STRING 1  
  12. #define CFO_STRING_TYPE_INT 2   
  13. int cfo_get_string_info_from_string(char *stringint *offset, size_t *size, unsigned char *type);   
  14.   
  15. main()   
  16. {   
  17.     char *string = "210abcdefg";   
  18.     int offset[10];   
  19.     size_t size[10];   
  20.     unsigned char type[10];   
  21.     int i;   
  22.     i = cfo_get_string_info_from_string(string, offset, size, type);   
  23.     printf("fanhui: %d  ", i);   
  24.     printf("type: %d   ", type[0]);   
  25.     printf("size: %d   ", size[0]);   
  26.     printf("offset: %d ", *offset);   
  27.     printf("/n");   
  28. }   
  29.   
  30. int cfo_get_string_info_from_string(char *stringint *offset, size_t *size, unsigned char *type)   
  31. {   
  32.     if ((string == NULL) || (offset == NULL) || (size == NULL) || (type == NULL))   
  33.     {   
  34.         return CFO_PARA_ERR;   
  35.     }   
  36.     size_t string_size = strlen(string);   
  37.     int offset_temp = 0;   
  38.     size_t size_temp;   
  39.     unsigned char type_temp;   
  40.     int i;   
  41.     for (i=0; i<string_size; i+=1)   
  42.     {   
  43.         /*............*/  
  44.         if ((string[i] >= '!') && (string[i] <= '~'))   
  45.         {   
  46.             break;   
  47.         }   
  48.     }   
  49.   
  50.     /*...............*/  
  51.     if (i == string_size)   
  52.     {   
  53.         *offset = 0;   
  54.         *size = 0;   
  55.         *type = CFO_STRING_TYPE_UNKNOWN;   
  56.         type[0] = 10;   
  57.         return CFO_API_OK;   
  58.     }   
  59.   
  60.     /*...................*/  
  61.     if ((string[i] == '/"')   
  62.        || (string[i] == '/'')   
  63.        || ((string[i] >= '0') && (string[i] <= '9')))   
  64.     {   
  65.         /*.........*/  
  66.         if ((string[i] == '0') && (string[i+1] == 'x'))   
  67.         {   
  68.             *offset = i;   
  69.             *type = CFO_STRING_TYPE_INT;   
  70.             size_temp = 2;   
  71.             for (i+=2; i<string_size; i+=1)   
  72.             {   
  73.                 if (((string[i] >= '0') && (string[i] <= '9'))   
  74.                    ||((string[i] >= 'a') && (string[i] <= 'f'))   
  75.                    ||((string[i] >= 'A') && (string[i] <= 'F')))   
  76.                 {   
  77.                     size_temp += 1;   
  78.                 }   
  79.                 else  
  80.                 {   
  81.                     break;   
  82.                 }   
  83.             }   
  84.             *size = size_temp;   
  85.             //type[0] = 10;   
  86.             return CFO_API_OK;   
  87.         }   
  88.         /*.......*/  
  89.         else if (((string[i] >= '0') && (string[i] <= '9')) && (string[i+1] != 'x'))   
  90.         {   
  91.             *offset = i;   
  92.             *type = CFO_STRING_TYPE_INT;   
  93.             size_temp = 1;   
  94.             for (i+=1; i<string_size; i+=1)   
  95.             {   
  96.                 if ((string[i] >= '0') && (string[i] <= '9'))   
  97.                 {   
  98.                     size_temp += 1;   
  99.                 }   
  100.                 else  
  101.                 {   
  102.                     break;   
  103.                 }   
  104.             }   
  105.             *size = size_temp;   
  106.             //type[0] = 10;   
  107.             return CFO_API_OK;   
  108.         }   
  109.         /*......*/  
  110.         else  
  111.         {   
  112.             offset_temp = i + 1;   
  113.             type_temp = CFO_STRING_TYPE_UNKNOWN;   
  114.             size_temp = 0;   
  115.             for (i+=1; i<string_size; i+=1)   
  116.             {   
  117.                 /*............*/  
  118.                 if ((string[i] == '/"') || (string[i] == '/''))   
  119.                 {   
  120.                     type_temp = CFO_STRING_TYPE_STRING;   
  121.                     break;   
  122.                 }   
  123.                 /*...........*/  
  124.                 else if ((string[i] == '#') || (string[i] == '/r') || (string[i] == '/n'))   
  125.                 {   
  126.                     type_temp = CFO_STRING_TYPE_UNKNOWN;   
  127.                     break;   
  128.                 }   
  129.                 else  
  130.                 {   
  131.                     size_temp += 1;   
  132.                 }   
  133.             }   
  134.             /*.............*/  
  135.             if (i == string_size)   
  136.             {   
  137.                 type_temp = CFO_STRING_TYPE_UNKNOWN;   
  138.             }   
  139.   
  140.             /*.......*/  
  141.             if (type_temp == CFO_STRING_TYPE_STRING)   
  142.             {   
  143.                 *offset = offset_temp;   
  144.                 *size = size_temp;   
  145.                 *type = CFO_STRING_TYPE_STRING;   
  146.                 //type[0] = 10;   
  147.                 return CFO_API_OK;   
  148.             }   
  149.             else  
  150.             {   
  151.                 *offset = offset_temp - 1;   
  152.                 *size = 0;   
  153.                 *type = CFO_STRING_TYPE_UNKNOWN;   
  154.                 //type[0] = 10;   
  155.                 return CFO_API_OK;   
  156.             }   
  157.         }   
  158.     }   
  159.     else  
  160.     {   
  161.         *offset = i;   
  162.         *size = 0;   
  163.         *type = CFO_STRING_TYPE_UNKNOWN;   
  164.         //type[0] = 10;   
  165.         return CFO_API_OK;   
  166.     }   
  167. }   
  168.   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值