在下面的代码行中,我需要pm通过其中一个字段中的字节偏移量来调整指针.是否有更好/更简单的方法来做到这一点,除了不断铸造背面和来回char *并PartitionMap *使得指针运算仍然有效呢?
PartitionMap *pm(reinterpret_cast<PartitionMap *>(partitionMaps));
for ( ; index > 0 ; --index)
{
pm = (PartitionMap *)(((char *)pm) + pm->partitionMapLength);
}
return pm;
Run Code Online (Sandbox Code Playgroud)
对于那些无法从代码中理解的人来说,它是通过继承自的缓冲区中的可变长度描述符进行循环PartitionMap.
同样对于那些有关的人,partitionMapLength总是返回运行它的系统支持的长度.我正在遍历的数据符合UDF规范.
如何以单字节精度可移植地执行指针运算?
请记住:
char 在所有平台上都不是1个字节sizeof(void) == 1 仅作为GCC的扩展名提供c compiler-construction portability void-pointers pointer-arithmetic
我不明白这里的优先顺序.鉴于:
*(p++)
Run Code Online (Sandbox Code Playgroud)
以下是我认为会发生的事情:
(p++)
Run Code Online (Sandbox Code Playgroud)
然后
*p
Run Code Online (Sandbox Code Playgroud)
为什么不是p首先递增的地址,然后取消引用,因为后缀在括号中?
*(p++)
Run Code Online (Sandbox Code Playgroud)
为什么地址不会增加,然后由于括号而被解除引用.
*p++似乎并不相同*(p++),但它们是相同的.
我必须在Linux上的C中编写一个函数来读取或写入通用数据.
我可以读取(或写入)大数据,所以我用了多少字节读了一会儿.例如,在下一次调用i时,读入原始指针+我读取的字节数.但是我不知道这种类型,所以我使用了一个空格*但是gcc说:
membox.c: In function ‘myRW’:
membox.c:301:22: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
w = read(fd, data + (s*type) , len - s);
^
membox.c:308:23: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
w = write(fd, data + (s*type) , len - s);
Run Code Online (Sandbox Code Playgroud)
我能这样做吗?我应该忽略这个警告?
如果我取消引用一个数组并添加超过数组中可用元素的5,那么打印的值是多少?我在我的IDE中返回32767,这没有多大意义.
#include <stdio.h>
int main(void)
{
int i;
int meatBalls[5] = {1, 2, 3, 4, 5};
printf("%12s %18s %6s\n", "Element", "Address", "Value");
for(i=0; i < 5; i++) {
printf("meatBalls[%d] \t %p \t %d \n", i, &meatBalls[i], meatBalls[i]);
}
printf("\nmeatBalls \t\t %p \n", meatBalls); /*already a pointer*/
/*dereference meatBalls and it will update to the value e.g. 1*/
printf("\n*meatBalls \t\t %d \n", *meatBalls); /*dereference with asterisk*/
printf("\n*(meatBalls+2) \t\t %d \n", *(meatBalls+2)); /*dereference but also go through the array from 0 + …Run Code Online (Sandbox Code Playgroud) 考虑以下两个无用的C++函数.
使用GCC(4.9.2,32或64位)编译时,两个函数都返回与预期相同的值.
使用Visual Studio 2010或Visual Studio 2017(非托管代码)编译,两个函数都返回不同的值.
我尝试过的:
这里发生了什么?这似乎是VS中的一个基本错误.
char test1()
{
char buf[] = "The quick brown fox...", *pbuf = buf;
char value = (*(pbuf++) & 0x0F) | (*(pbuf++) & 0xF0);
return value;
}
char test2()
{
char buf[] = "The quick brown fox...", *pbuf = buf;
char a = *(pbuf++) & 0x0F;
char b = *(pbuf++) & 0xF0;
char value = a | b;
return value;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
运行我的程序时,它会cout<<Core32->name()在第二次执行时终止.在研究了这个问题之后,我了解到在这种情况下指针算术会导致问题.还有,我也能够通过替代来解决该问题Core32++;与Core32+04;(真的不知道为什么这个工程).不过我的问题是为什么我能够调用方法Core32->procfun()而不是Core32->name().我查找对象切片,但它似乎没有解决我的问题.
编辑:想知道它为什么只能调用procfun()而不是name().这是旧考试的一个例子
#include <iostream>
#include<string>
using namespace std;
class Prozessor{
public:
virtual string name()=0;
};
class ARM : public Prozessor{
public:
virtual string name(){return "VAR";};
virtual int procbitw()=0;
string procfun(){return "VFP";};
};
class ARMv7:public ARM{
public:
int procbitw(){return 32;};
string procfun(){return "VFP";};
};
class ARMv8:public ARM{
public:
int procbitw(){return 64;};
string procfun(){return "VFP, NEON";};
};
int main()
{
const unsigned int n_cores=4;
ARM *HetQuadCore[n_cores]={new ARMv7, new ARMv7, new ARMv8, new …Run Code Online (Sandbox Code Playgroud) 我在理解以下代码片段的输出时遇到了一些麻烦。
#include<stdio.h>
int main()
{
char *str;
str = "%d\n";
str++;
str++;
printf(str-2, 300);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
代码的输出为300。
我知道直到printf语句前的行str指向字符- %。我需要帮助的是理解为什么printf打印300功能。
我编写了一个程序来翻转彼此相邻的数组元素。例如
1, 2, 1, 2 变成 2, 1, 2, 1
我写的代码是:
#include <iostream>
template <class T>
void swap(T& a, T& b){
T temp = a;
a = b;
b = temp;
}
template <class T>
void oReverse(T* p, int size){
for(T* e = p + size; p < e; p+=1){
swap(*p, *p++);
}
}
int main(){
int arr[] = {1,2,1,2,1,2};
oReverse(arr, 6);
for(int i = 0; i < 6; i++){
std::cout << arr[i] << " ";
}
return 0;
} …Run Code Online (Sandbox Code Playgroud) 在这段代码中
strcpy(s, "bacalaureat");
i = strchr(s, 'a') - s; // i = 1
Run Code Online (Sandbox Code Playgroud)
的值为i1。为什么会这样,-s上面的作用是什么?