Tag: cast

类型转换为C中的unsigned

int a = -534; unsigned int b = (unsigned int)a; printf(“%d, %d”, a, b); 打印-534, -534 为什么没有发生类型转换? 我预计它会是-534, 534 如果我修改代码 int a = -534; unsigned int b = (unsigned int)a; if(a < b) printf("%d, %d", a, b); 它不打印任何东西……毕竟a小于b ??

将int数组转换为short *时,为什么为元素赋值会覆盖整个整数?

我正在观看Jerry Cain的Programming Paradigms Lecture 3video ,其中展示了在int数组和短数组之间进行元素分配后的效果。 本质上,参数是如果你要分配一个int数组元素arr[3] = 128 ,然后暂时将int数组转换为short *并指定arr[6] = 2 ,则arr [3]应该变为128 + 512 = 640因为2将被解释为处于第2 ^ 9位置。 代码演示: #include int main() { printf(“sizeof(int) is %lu\n”, sizeof(int)); printf(“sizeof(short) is %lu\n”, sizeof(short)); int arr[5]; arr[3] = 128; ((short*)arr)[6] = 2; printf(“arr[3] is equal to %d\n”, arr[3]); //expect 640, get 2 instead return 0; […]

使用(void *)作为通用数据容器时的L值问题

这是程序中使用的结构: struct basic_block { void * aux; /* Many other fields, which are irrelevant. */ }; 现在: 在程序执行期间存在几个basic_block实例。 该程序分阶段/通过,一个接一个地执行。 aux字段用于在阶段执行期间存储stage和basic_block特定数据,并由阶段本身释放(因此下一阶段可以重用它)。 这就是为什么它是一个void * 。 我的舞台使用aux来存储一个struct ,所以每次我想访问某些东西时,我都要做一个强制转换: ( (struct a_long_struct_name *) (bb->aux))->foo_field = foo; 现在,我的问题:每次像这样投下它是一种痛苦,当它是更复杂的表达式的一部分时难以阅读。 我建议的解决方案是:使用宏为我做演员: #define MY_DATA(bb) \ ( (struct a_long_struct_name *) (bb)->aux) 然后我可以访问我的数据: MY_DATA(bb)->foo_field = foo; 但是:我不能将MY_DATA(bb) 本身用作L值(对于malloc ),所以我使用该宏并不是一个好主意: /* WRONG! I cannot assign to […]

从bsearch和lfind确定索引?

我试图在lfind和bsearch返回指向它找到的元素的指针之后获取数组中元素的索引。 到目前为止我有这个: (char *) (found – cv->baseAddress); 找到的是函数找到的地址,基址是元素0的地址。但是,编译器给出了这个错误: cvector.c:150:28:警告:减法中使用的’void *’类型的指针cvector.c:150:4:warning:return从指针生成整数而没有强制转换 我该怎么办?

使用%lu打印int类型 – C + XINU

我有一个给定的代码,在我看来这个代码有问题:我在XINU下编译。 接下来的变量是相关的: unsigned long ularray[]; int num; char str[100]; 有一个函数返回int: int func(int i) { return ularray[i]; } 现在代码是这样的: num = func(i); sprintf(str, “number = %lu\n”, num); printf(str); 问题是我用%lu打印得到大数字,这是不正确的。 如果我将%lu更改为%d ,我会得到正确的数字。 例如:使用%lu我得到27654342,而%di得到26,后者是正确的; 给出变量,给出函数的声明,我写主体但它必须返回int; 我的问题是: 我不熟悉’sprintf’可能有问题吗? 我将unsigned long分配给int然后用%lu打印int,是吗? 我该如何解决这个问题? 提前致谢。 谢谢大家回答。 我只是想提一下我在XINU下工作,我改变了文件编译的顺序和你知道的……它在%lu和%d上工作并显示相同的数字。 我很清楚,将’unsigned long’分配给int然后使用%lu进行打印是不正确的编码,可能会导致数据丢失。 但正如我所说,代码给出,我无法更改变量和打印命令。 我没有错误或警告。 我不知道为什么更改编译顺序修复了问题,如果有人有想法你更欢迎分享。 我要感谢所有试图帮助我的人。

用于qsort函数指针的Typecast

#include #include #include #include static int cmpstringp(const void *p1, const void *p2) { /* The actual arguments to this function are “pointers to pointers to char”, but strcmp(3) arguments are “pointers to char”, hence the following cast plus dereference */ return strcmp(* (char * const *) p1, * (char * const *) p2); } int […]

printf和cast float参数

作为我的程序的一部分,我使用: int ret = vprintf (format, args); 我进入堆栈的args ,我不知道实际上是什么被推到了堆栈上。 格式是一个字符串,我可以阅读。 上述方法有效,直到我必须打印浮点数。 当我打印浮动时,我得到一些奇怪的数字…… 我检查了如果我调用float fArg = *(reinterpret_cast(args) – 然后打印fArg打印正确的值(当args仅由一个实际参数组成时我尝试过) 所以我可能需要”%…f”子格式的特殊行为 – 相应的(子)参数应该转换为float。 ( …符号表示在f之前可以添加精度,宽度等)我如何实现它?

将int和const int转换为float时的结果不同

有人能够解释为什么int和const int在转换为float并在浮点数学中使用时给出不同的结果? 例如,参见这段代码: int _tmain(int argc, _TCHAR* argv[]) { int x = 1000; const int y = 1000; float fx = (float) x; float fy = (float) y; printf(“(int = 1000) * 0.3f = %4.10f \n”, 0.3f*x); printf(“(const int = 1000) * 0.3f = %4.10f \n”, 0.3f*y); printf(“(float)(int = 1000) * 0.3f = %4.10f \n”, […]

C:如何摆脱转换错误?

我有一个使用gcc版本4.6.3的项目,我被迫使用“-Wall -Werror -Wconversion”进行编译。 以下简单示例显示了我无法摆脱的错误: #include int main(void) { uint32_t u = 0; char c = 1; u += c; return (int)u; } 用上面的标志编译它给出: test.c:7:8: error: conversion to ‘uint32_t’ from ‘char’ may change the sign of the result [-Werror=sign-conversion] 好的。 只需添加一个类型转换,对吗? 不。 将第7行更改为u += (uint32_t)c不会使错误消失。 即使将其改为u = u + (uint32_t)c也不会让它消失。 有可能解决这个问题吗? 请注意,“char”来自字符串,因此我无法更改其类型。

将printf移动到不同的行会产生不同的输出? (C)

在C中,当我移动这个printf行时: printf(“%f\n”, 5 / 2); 它的输出变化到不同的行。 有任何想法吗? inheritance人代码: #include #include int main() { int a = 65; char c = (char)a; int m = 3.0/2; printf(“%c\n”, c); printf(“%f\n”, (float)a); printf(“%f\n”, 5.0 / 2); printf(“%f\n”, 5 / 2.0); printf(“%f\n”, (float)5 / 2); printf(“%f\n”, 5 / (float)2); printf(“%f\n”, (float)(5 / 2)); printf(“%f\n”, 5.0 / 2); printf(“%d\n”, m); […]