[英]Increment `int i = 0` to increment in the position of an initialized char array
假設我有一個 char 變量:
int i = 0;
char currentByte = (*(char*)((intptr_t)bytes + i));
在代碼中更長的時間,我想根據i
的數量更改currentByte
。 我將如何在 C++ 中對此進行 go ?
我想做這樣的事情:
// 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
的 scope :
char GetCurrentByte()
{
return GetByte(i);
}
bytes
已經是字節數組了嗎?
char* pCurrentByte = &bytes[0]; // or i
pCurrentByte++; // Now at (bytes + 1)
char currentByte = *pCurrentByte;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.