递增 'int i = 0' 以在初始化的字符数组的位置递增

Increment `int i = 0` to increment in the position of an initialized char array

本文关键字:字符 数组 初始化 位置 int 递增      更新时间:2023-10-16

假设我有一个字符变量:

int i = 0;
char currentByte = (*(char*)((intptr_t)bytes + i));

在代码中更长的时间,我想根据i的数量更改currentByte。C++年我该如何继续?

我想做这样的事情:

// Go through bytes, and go to next byte:
i++;

然后将currentByte

char currentByte = (*(char*)((intptr_t)bytes + 0));

char currentByte = (*(char*)((intptr_t)bytes + 1));

我想要实现的是不必在我的代码中到处写"((char(((intptr_t(bytes + i(("。对不起,我的C++生锈了。

函数是你的朋友:

char GetByte(int pos)
{
return (char*)((intptr_t)bytes + pos);
}

而且,也许,取决于您i的范围:

char GetCurrentByte()
{
return GetByte(i);
}

bytes已经是一个字节数组了吗?

char* pCurrentByte = &bytes[0]; // or i
pCurrentByte++; // Now at (bytes + 1)
char currentByte = *pCurrentByte;