什么是1个字节添加到C / C ++指针的正确方法是什么? [英] What's the correct way to add 1 byte to a pointer in C/C++?
问题描述
我使用这个code。通过1个字节,现在移动指针,但是我感觉有不清楚..
I'm using this code to move pointer by 1 byte now, but I'm feeling something unclear..
int* a = (int*)malloc(sizeof(int));
void* b = ((char*)a)+1;
字符
1个字节,但对于字节操作的目的没有定义。我相信,有另一种方式来做到这一点的字节的操作。什么是在正确办法的字节的操作?
char
is 1 byte, but not defined for byte operation purpose. I believe there's another way to do this byte operation. What's the correct way to byte operation?
PS。我修改样本code是有效的。现在它编译成C ++铿锵。
PS. I modified sample code to be valid. It's now compiled as C++ with Clang.
推荐答案
我想你感到困惑:
字符
1个字节,但对于字节操作的目的没有定义。我相信,有另一种方式来做到这一点的字节的操作。什么是在正确办法的字节的操作?
char
is 1 byte, but not defined for byte operation purpose. I believe there's another way to do this byte operation. What's the correct way to byte operation?
你到底是希望什么字节
来的意思是,如果不是完全一样的东西,字符
意味着什么?
What exactly are you expecting byte
to mean, if not the exact same thing that char
means?
在C和C ++中,字符的是字节。 根据定义。什么是的不可以的情况下,是的字节的必然的八位的。一个字节包含至少8位。有否保证一个给定的平台,甚至使得它的可能引用的内存块就是究竟 8位。
In C and in C++, chars are bytes. By definition. What is not the case is that bytes are necessarily octets. A byte contains at least 8 bits. There is no guarantee that a given platform even makes it possible to reference a chunk of memory that is exactly 8 bits.
这篇关于什么是1个字节添加到C / C ++指针的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!