复制void指针C的值

时间:2014-12-17 23:53:00

标签: c avr

我的函数问题应该在我的KS0108 lcd上打印任何类型的整数。这里的问题是:当我想在第一次切换中将void* numb值复制到void* temp时 - 没有任何反应。在使用%I获取0时进行第二次切换。 (void* numb的值约为48,类型为_uint8_t) 代码:

void glcd_WriteInt(void* numb, type type)
{
    void* temp = 0;
    int8_t buff[10];
    int8_t size = 0;
    int8_t flag = 1;
    int8_t sign = 1;
    switch(type)
    {
        case _int8_t:   memcpy(temp, numb, sizeof(int8_t)); break; 
        case _int16_t:  memcpy(temp, numb, sizeof(int16_t)); break;
        case _int32_t:  memcpy(temp, numb, sizeof(int32_t)); break;
        case _int64_t:  /*memcpy(temp, numb, sizeof(int64_t)); break;*/return;  //if want 64 - uncomment
        case _uint8_t:  *(uint8_t*) temp = *(uint8_t*) numb/*memcpy(temp, numb, sizeof(uint8_t))*/; break;
        case _uint16_t: memcpy(temp, numb, sizeof(uint16_t)); break;
        case _uint32_t: memcpy(temp, numb, sizeof(uint32_t)); break;
        case _uint64_t: /*memcpy(temp, numb, sizeof(uint64_t)); break;*/return; //if want 64 - uncomment
    }
    while(flag)
    {
        switch(type)
        {
            case _int8_t:
                buff[size] = *(int8_t*) temp % 100;
                flag = (*(int8_t*) temp /= 100) != 0;
                break;
            case _int16_t:
                buff[size] = *(int16_t*) temp % 100;
                flag = (*(int16_t*) temp /= 100) != 0;
                break;
            case _int32_t:
                buff[size] = *(int32_t*) temp % 100;
                flag = (*(int32_t*) temp /= 100) != 0;
                break;
            case _int64_t:
                /*buff[size] = *(int64_t*) temp % 100;      //if want 64 - uncomment
                flag = (*(int64_t*) temp /= 100) != 0;*/
                break;
            case _uint8_t:
                buff[size] = *(uint8_t*) temp % 100;
                flag = (*(uint8_t*) temp /= 100) != 0;
                break;
            case _uint16_t:
                buff[size] = *(uint16_t*) temp % 100;
                flag = (*(uint16_t*) temp /= 100) != 0;
                break;
            case _uint32_t:
                buff[size] = *(uint32_t*) temp % 100;
                flag = (*(uint32_t*) temp /= 100) != 0;
                break;
            case _uint64_t:
                /*buff[size] = *(uint64_t*) temp % 100;     //if want 64 - uncomment
                flag = (*(uint64_t*) temp /= 100) != 0;*/
                break;
        }
        if(buff[size] < 0){
            buff[size] = -buff[size];
            sign = -1;
        }
        size++;
    }

    if(sign == -1) glcd_Character((char) 45);
        glcd_WIntTab(buff, size);
}

我尝试使用memcpy的版本,就像这里:强制转换。令人惊讶的是,几天前这个功能还可以。

1 个答案:

答案 0 :(得分:0)

你的主要问题是你有

void *temp = 0;

声明,并且您尝试写入NULL指针,因为您使用void *temp = 0初始化它。

即使你没有初始化它,它也不会工作,因为它不是一个有效的指针,所以要么用malloc分配内存并将其分配给void *temp指针,要么使用以这种方式结合

union Type {
    int8_t _int8_t;
    int16_t _int16_t;
    int32_t _int32_t;
    int64_t _int64_t;
    uint8_t _uint8_t;
    uint16_t _uint16_t;
    uint32_t _uint32_t;
    uint64_t _uint64_t;
};

void glcd_WriteInt(void* numb, enum type type)
{
    union Type temp;

    int8_t buff[10];
    int8_t size = 0;
    int8_t flag = 1;
    int8_t sign = 1;

    switch (type)
    {
        case _int8_t:   memcpy(&temp, numb, sizeof(int8_t)); break;
        case _int16_t:  memcpy(&temp, numb, sizeof(int16_t)); break;
        case _int32_t:  memcpy(&temp, numb, sizeof(int32_t)); break;
        case _int64_t:  /*memcpy(&temp, numb, sizeof(int64_t)); break;*/return;  //if want 64 - uncomment
        case _uint8_t:  memcpy(&temp, numb, sizeof(uint8_t))/*memcpy(temp, numb, sizeof(uint8_t))*/; break;
        case _uint16_t: memcpy(&temp, numb, sizeof(uint16_t)); break;
        case _uint32_t: memcpy(&temp, numb, sizeof(uint32_t)); break;
        case _uint64_t: /*memcpy(&temp, numb, sizeof(uint64_t)); break;*/return; //if want 64 - uncomment
    }

    while (flag)
    {
        switch (type)
        {
            case _int8_t:
                buff[size] = temp._int8_t % 100;
                flag = (temp._int8_t /= 100) != 0;
                break;
            case _int16_t:
                buff[size] = temp._int16_t % 100;
                flag = (temp._int16_t /= 100) != 0;
                break;
            case _int32_t:
                buff[size] = temp._int32_t % 100;
                flag = (temp._int32_t /= 100) != 0;
                break;
            case _int64_t:
                buff[size] = temp._int64_t % 100;
                flag = (temp._int64_t /= 100) != 0;
                break;
            case _uint8_t:
                buff[size] = temp._uint8_t % 100;
                flag = (temp._uint8_t /= 100) != 0;
                break;
            case _uint16_t:
                buff[size] = temp._uint16_t % 100;
                flag = (temp._uint16_t /= 100) != 0;
                break;
            case _uint32_t:
                buff[size] = temp._uint32_t % 100;
                flag = (temp._uint32_t /= 100) != 0;
                break;
            case _uint64_t:
                buff[size] = temp._uint64_t % 100;
                flag = (temp._uint64_t /= 100) != 0;
                break;
        }

        if (buff[size] < 0)
        {
            buff[size] = -buff[size];
            sign = -1;
        }
        size++;
    }
    if (sign == -1)
        glcd_Character((char) 45);
    glcd_WIntTab(buff, size);
}

我相信工会是一个更好的解决方案。