小编Soh*_*abi的帖子

如何评估(int*)%int?

我正在实现一个循环队列,当队列已满时,我希望指针返回到数组地址的第一个.

struct Sample{

    void* ptr;
};

int main() {

    char a[20] = "hello";

    struct Sample sample;

    sample.ptr = (a + 2) % 6;

    printf("%s\n", sample.ptr);

}
Run Code Online (Sandbox Code Playgroud)

当我编译代码时,我收到以下错误:

invalid operands to binary % (have ’char* ’ and ‘int’)

我知道问题来自%,例如,如果我将第12行更改为sample.ptr = (a + 2)它将起作用.但我需要评估%.我怎样才能做到这一点?

c

-4
推荐指数
1
解决办法
121
查看次数

标签 统计

c ×1