如何转换和打印结构成员的`void`指针数组?

huangapple go评论60阅读模式
英文:

How to covert and print the void pointer array of structure member?

问题

关于学习,我正在创建数据结构库,在那里我创建了堆栈的结构并添加了用于数组的void指针,当我尝试访问void指针数组的值时,会引发数据类型无效转换错误。有人能告诉我我哪里出错了?

typedef struct
{
    size_t size;
    size_t top;
    void *st_base;
} stack_t;

dsa_err_t stack_init(stack_t *stack, uint32_t size)
{
    DSA_CHECK_ARG(stack);

    stack->st_base = (int32_t *)calloc((size_t)size, sizeof(int32_t));
    if (!(stack->st_base))
    {
        return DSA_NO_MEM;
    }
    stack->top = 0;
    stack->size = size;
    return DSA_OK;
}

dsa_err_t stack_push(stack_t *stack, int32_t item)
{
    DSA_CHECK_ARG(stack && stack->st_base);

    if (stack->top >= (stack->size - 1))
    {
        return DSA_FAIL;
    }

    stack->st_base[(stack->top)++] = item; // 表达式必须是可修改的值错误
    return DSA_OK;
}

dsa_err_t stack_pop(stack_t *stack, int32_t *item)
{
    DSA_CHECK_ARG(stack && stack->st_base);

    if (stack_is_empty(stack) || stack->top > (stack->size - 1))
    {
        return DSA_FAIL;
    }
    *item = (int32_t)stack->st_base[stack->top - 1]; // 无效类型转换错误
    stack->st_base[stack->top--] = 0;
    return DSA_OK;
}

注意:上述翻译只包括代码部分,不包括问题或其他内容。

英文:

As for learning I was creating the library of data structure there I create the structure of stack and added the void pointer use for array and when I m trying to access the value of void pointer array is throw the invalid conversion of data type error. Can anyone tell me where I m doing wrong?

typedef struct
{
    size_t size;
    size_t top;
    void *st_base;
} stack_t;

dsa_err_t stack_init(stack_t *stack, uint32_t size)
{
    DSA_CHECK_ARG(stack);

    stack->st_base = (int32_t *)calloc((size_t)size, sizeof(int32_t));
    if (!(stack->st_base))
    {
        return DSA_NO_MEM;
    }
    stack->top = 0;
    stack->size = size;
    return DSA_OK;
}
dsa_err_t stack_push(stack_t *stack, int32_t item)
{
DSA_CHECK_ARG(stack && stack->st_base);

if (stack->top >= (stack->size - 1))
{
    return DSA_FAIL;
}

stack->st_base[(stack->top)++] = item; ---> EXPRESSION MUST BE MODIFIABLE VALUE ERROR
return DSA_OK;
}

dsa_err_t stack_pop(stack_t *stack, int32_t *item)
{
DSA_CHECK_ARG(stack && stack->st_base);

if (stack_is_empty(stack) || stack->top > (stack->size - 1))
{
    return DSA_FAIL;
}
*item = (int32_t)stack->st_base[stack->top - 1]; ---> INVALID TYPE OF CONVERSION ERROR
stack->st_base[stack->top--] = 0;
return DSA_OK;
}

答案1

得分: 3

void * 是一个不完整的类型,因此无法进行解引用(而数组索引是解引用的一种形式)。您需要首先将其转换为可用的指针类型。

((int32_t *)stack->st_base)[(stack->top)++] = item;
...
*item = ((int32_t *)stack->st_base)[stack->top - 1];
英文:

A void * is an incomplete type and therefore can't be dereferenced (and array indexing is a form of dereferencing). You need to convert it to a usable pointer type first.

((int32_t *)stack->st_base)[(stack->top)++] = item;
...
*item = ((int32_t *)stack->st_base)[stack->top - 1];

huangapple
  • 本文由 发表于 2023年3月10日 01:22:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定