Tag: 转换

检查一个整数类型的值是否适合另一个整数类型

Type1和Type2有两种类型,由不同的头提供,对于某些整数类型都是typedef,但不知道哪些(并且可能在编译器,操作系统等方面有所不同)。 该程序是否保证正常工作(如果它编译) Type1 a = …; if (a == (Type2) a) { printf(“a fits into Type2”); else { printf(“a doesn’t fit into Type2”); } 或者是否有我不考虑的角落案件?

传递参数使得指针来自整数

我找不到我的问题。 不断给我这些错误: “c:2:5: note: expected ‘int *’ but argument is of type ‘int'” “c:28:1: warning: passing argument 1 of ‘CountEvenNumbers’ makes pointer from integer without a cast [enabled by default]” 这是代码: 1 #include 2 int CountEvenNumbers(int numbers[], int length); 3 int main(void) 4 { 5 int length; 6 int X;int Z; int Y; int W; […]

C编程(函数指针转换)

int eax = ((int(*)())(“\xc3 <- This returns the value of the EAX register"))(); 这是怎么回事? 字符串被转换为函数指针

这个指针构建是否会破坏严格的别名规则?

这是Quake III Arena的快速反平方根实现: float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; // evil floating point bit level hacking i = 0x5f3759df – ( i >> 1 ); // what? y = […]

C将浮点数转换为int

我正在使用C (不是C ++)。 我需要将浮点数转换为int 。 我不想舍入到最接近的数字,我只是想消除整数部分之后的内容。 就像是 4.9 – > 4 .9 – > 4

将fpos_t强制转换为int或char

我正在使用一个函数,该函数使用按位操作和文件的长度: fpos_t flen; 当我尝试将其转换为int或char,或尝试对其进行算术运算时,它失败并出现以下编译错误: error: aggregate value used where an integer was expected

C-在1D字节数组中索引x,y,z坐标

我想评估曲面的值来实现行进四面体算法,但我不明白如何使用.raw无格式数据。 将带有卷数据集的.raw文件加载到1D字节数组后,应该应用什么算术转换来获取与X,Y,Z相关的值? 这是我知道加载.raw文件的唯一方法,我可以创建一个3D字节数组而不是这个吗? 怎么样? int XDIM=256, YDIM=256, ZDIM=256; const int size = XDIM*YDIM*ZDIM; bool LoadVolumeFromFile(const char* fileName) { FILE *pFile = fopen(fileName,”rb”); if(NULL == pFile) { return false; } GLubyte* pVolume=new GLubyte[size]; //<- here pVolume is a 1D byte array fread(pVolume,sizeof(GLubyte),size,pFile); fclose(pFile);

在C ++中将1D数组作为2D数组访问

这困扰了我一段时间。 很多时候,我发现自己制作了一个大缓冲区来保存“最大”的数据量。 这有助于我避免每次下一个数据集的大小发生变化时动态分配和释放缓冲区。 例如,假设我的数组对于实际有用的大小来说太大了,但我知道有用数据的长度。 int amountOfData = 9; char data1D[100] = some data that is only 9 bytes long stored in a 100 byte array 假设我有一个算法,我想在这个使用2D数组索引的数据集上运行。 所以我希望能够按如下方式访问数据: cout << "I am accessing this data as a 2D array: " << data1D[0][1] << endl; 让我们说这个算法我知道2D数组的xlength和ylength将是: int xlength = 3; int ylength = 3; 对于此迭代,因为amountOfData = 9 […]

使用指针算法计算类型大小的替代方法

以下代码是100%可移植的吗? int a=10; size_t size_of_int = (char *)(&a+1)-(char*)(&a); // No problem here? std::cout<<size_of_int;// or printf("%zu",size_of_int); PS :问题仅限于学习目的。 所以请不要给出像Use sizeof()等的答案

如何在C ++中初始化指向特定内存地址的指针

可能重复: 指向特定固定地址的指针 关于这一点的有趣讨论从这里开始,但没有人能够提供C ++的方式: #include int main(void) { int* address = (int *)0x604769; printf(“Memory address is: 0x%p\n”, address); *address = 0xdead; printf(“Content of the address is: 0x%p\n”, *address); return 0; } 在C ++中最合适的方法是什么?