极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11555|回复: 2

我要求助了,被C的类型搞死了

[复制链接]
发表于 2016-7-4 22:23:23 | 显示全部楼层 |阅读模式

问题大致如下:

这是一个类似于输入框的程序,当键盘输入数字时,输入框的数字会在后面追加一位,当输入擦除键的时候,输入框的数字会删除最后一位。

下面的程序中:
strNumValue是一个unsigned char类型的公共字符数组变量
strNum是输入的数字,是unsigned char类型的字符变量

编译时,无法通过,提示strlen(strNumValue)函数从unsigned char*到const char*非法转换。
请高手看看问题出在哪里了,怎么改?

  1. unsigned char strNumValue[5];

  2. //……
  3. void SetNumValue( unsigned char strNum )
  4. {
  5.         if(strNum=='-')
  6.         {
  7.                 strNumValue[strlen(strNumValue)-1] = '\0';//将数组的最后一个数字字符改为结束标记
  8.         }
  9.         else
  10.         {
  11.                 strcat(strNumValue,strNum);
  12.         }
  13. //……
复制代码


unsigned char strNumValue[5];

……
void SetNumValue( unsigned char strNum )
{
        if(strNum=='-')
        {
                strNumValue[strlen(strNumValue)-1] = '\0';//将数组的最后一个数字字符改为结束标记
        }
        else
        {
                strcat(strNumValue,strNum);
        }
……
回复

使用道具 举报

发表于 2016-7-5 11:29:05 | 显示全部楼层
本帖最后由 pathletboy 于 2016-7-5 11:32 编辑

不知道为什么你需要使用strlen

  1. #define NUM_BUFFER_SIZE 5
  2. typedef struct
  3. {
  4.     unsigned char buffer[NUM_BUFFER_SIZE];
  5.     unsigned int index;
  6. } num_buffer_t;

  7. num_buffer_t nums = {
  8.     index: 0
  9. };

  10. void SetNumValue( unsigned char strNum )
  11. {
  12.     switch (strNum)
  13.     {
  14.         case '-':
  15.             if (nums.index > 0)
  16.             {
  17.                 nums.buffer[nums.index--] = 0;
  18.             }
  19.             break;
  20.         default:
  21.             if (nums.index < NUM_BUFFER_SIZE - 1)
  22.             {
  23.                 nums.buffer[nums.index++] = strNum;
  24.                 nums.buffer[nums.index] = 0;
  25.             }
  26.             break;
  27.     }
  28. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-7-5 13:56:55 | 显示全部楼层
pathletboy 发表于 2016-7-5 11:29
不知道为什么你需要使用strlen

#define NUM_BUFFER_SIZE 5

谢谢高手指点啊
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 23:09 , Processed in 0.032655 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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