相关疑难解决方法(0)

C编程 - 指针地址字节拆分

我有以下变量var,它是一个无符号的32位值.我正在研究一个小端系统.

unsigned long var = 1;
// Representation: 00000000 00000000 00000000 00000001
Run Code Online (Sandbox Code Playgroud)

现在我想通过以下方式访问第一个和最后一个16位:

(((unsigned short*)&var)[0])) => 00000000 00000001

(((unsigned short*)&var)[1])) => 00000000 00000000
Run Code Online (Sandbox Code Playgroud)

这种类型的指针地址类型拆分是否有任何C文档?我得到了这个工作,但我不知道这是如何工作的.

谢谢

c pointers

-1
推荐指数
1
解决办法
60
查看次数

为什么将无符号转换为直接用C签名会得到正确的结果?

在C中,有符号整数和无符号整数在内存中的存储方式不同.当类型在运行时清除时,C还隐式转换有符号整数和无符号整数.但是,当我尝试以下代码段时,

#include <stdio.h>

int main() {    
    unsigned int a = 5;
    signed int b = a;
    signed int c = *(unsigned int*)&a;
    signed int d = *(signed int*)&a;

    printf("%u\n", a);
    printf("%i\n", b);
    printf("%i\n", c);
    printf("%i\n", d);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

预期产量:

5
5                   //Implicit conversion occurs
5                   //Implicit conversion occurs, because it knows that *(unsigned int*)&a is an unsigned int
[some crazy number] //a is casted directly to signed int without conversion
Run Code Online (Sandbox Code Playgroud)

然而,实际上,它输出

5
5
5
5
Run Code Online (Sandbox Code Playgroud)

为什么?

c c++ unsigned-integer

-1
推荐指数
1
解决办法
107
查看次数

How to type-pun in C

Follow-up to extended discussion in Casting behavior in C

I'm trying to emulate a Z80 in C, where several 8-bit registers can be combined to create 16-bit registers.

This is the logic I'm trying to use:

struct {
    uint8_t b;
    uint8_t c;
    uint16_t *bc;
} regs[1];
...
regs->bc = (uint16_t *)&(regs->b);
Run Code Online (Sandbox Code Playgroud)

Why is this incorrect, and how can I do it correctly (using type-punning if needed)?

I need to do this multiple times, preferably within the same structure.

For those …

c z80 emulation type-punning

-1
推荐指数
1
解决办法
163
查看次数

有人可以向我解释这是如何工作的

我在C语言期末考试中看到了这个问题,输出为513,我不知道为什么

#include <stdio.h>
int main(void){
    char a[4] = {1,2,3,4};
    print("%d" , *(short*)a);
}
Run Code Online (Sandbox Code Playgroud)

c pointers

-1
推荐指数
1
解决办法
146
查看次数

C 语言 - 指针和强制转换

我知道指针是如何工作的,但是我在理解这个指针转换时遇到了一些麻烦。

float f;
scanf("%f", &f);
unsigned int x = *(unsigned int*)&f;
Run Code Online (Sandbox Code Playgroud)

有人可以解释我这是如何工作的吗?

c pointers casting

-1
推荐指数
1
解决办法
68
查看次数

通过 C 中的按位表示比较 2 个浮点数

我考试时遇到了这个问题,但我无法真正解决它,希望得到一些帮助。

仅填充空白,当且仅当 x<y 时函数必须返回 true。假设 x,y 不能为 NaN(但可以为 +-inf),不允许进行转换,仅使用 ux, uy, sx, sy

bool func(float x, float y) {
    unsigned* uxp = ______________ ;
    unsigned* uyp = ______________ ;
    unsigned  ux  = *uxp;
    unsigned  uy  = *uyp;
    unsigned  sx = (ux>>31); 
    unsigned  sy = (uy>>31);
    return ___________________________;
}
Run Code Online (Sandbox Code Playgroud)

c floating-point unsigned bit-manipulation ieee

-1
推荐指数
1
解决办法
207
查看次数

取消引用这个指针给了我-46,但我不知道为什么

这是我运行的程序:

#include <stdio.h>

int main(void)
{
    int y = 1234;
    char *p = &y;
    int *j = &y;
    printf("%d %d\n", *p, *j);
}
Run Code Online (Sandbox Code Playgroud)

我对输出有点困惑.我所看到的是:

-46 1234
Run Code Online (Sandbox Code Playgroud)

我把这个程序写成了一个实验,不知道它会输出什么.我期待可能有一个字节y.

这里发生了什么"幕后"?解除引用如何p给我-46

正如其他人指出的那样,我必须进行明确的施法才能导致UB.我没有改变这一行char *p = &y;,char *p = (char *)&y;所以我没有使下面的答案无效.

此程序不会导致此处指出的任何UB行为.

c pointers casting dereference

-3
推荐指数
2
解决办法
1243
查看次数

设置一个双字节数组

什么是在字节数组中分配一个double到8个字节的快速方法?

我有一个大约4k字节的字节数组,我试图从中取出8个字节并将其复制到一个double中.我试图避免memmove和memcpy出于速度原因,因为分配变量要快得多.我在嵌入式世界工作,任何其他快速实现都受到赞赏.

void foo(double *pdest)
{
   // Try 1: I am using 1 element in the array, it won't work
   *pdest = (double)p->stk[stkpos];

   // Try 2: I am attempting to loose the single element element
   *pdest = (double)((double*)&p->stk[stkpos]);
}
Run Code Online (Sandbox Code Playgroud)

这两种解决方案都没有对我有用,我不确定如何实现这一目标.

c embedded double variable-assignment

-3
推荐指数
2
解决办法
97
查看次数

为什么类型转换从int打印"0.0000"

下面是我为了解类型转换而编写的一些代码,但我不明白为什么浮点数或双精度值的值打印为"0.000000",即使我从整数数组中输入或尝试从联合的地址解释.

#include <stdio.h>

union test1
{
 int x;
 float y;
 double z;
};


int main()
{
 int x[2]={200,300};
 float *f;
 f=(float*)(x);
 printf("%f\n",*f); /*(1)why is this value 0.0*/

 *f=(float)(x[0]); /*(2)this works as expected and prints 200.00000*/
  printf("%f\n",*f);

  union test1 u;
  u.x=200;
  /*(3)this line give compilation error why*/
  //printf ("sizeof(test1) = %d \n", sizeof(test1));
  /*(4) this line also prints the value of float and double as 0.0000 why*/
  printf ("sizeof(test1) = %lu u.x:%d u.y:%f u.z:%lf \n", sizeof(u), u.x, u.y, u.z);
  return 0; …
Run Code Online (Sandbox Code Playgroud)

c pointers casting sizeof

-5
推荐指数
1
解决办法
409
查看次数

双指针指向int变量覆盖内存

我直接贴代码了。

#include <stdio.h>
struct A
{
    int a;
    int b;
};
int main()
{
    struct A test;
    double *p = (double *)&(test.a);
    *p = 5;

    printf("variable a: %d\n", &test.a);
    printf("variable b: %d\n", &test.b);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在centos7中运行这段代码,编译器是.gcc4.8.5并且我的电脑使用little end来存储。

正如你所看到的,变量的内存b将被覆盖,我期望的a是 is0x0000 0005bis 0x0000 0000

但答案是:

variable a: 0
variable b: 1075052544
Run Code Online (Sandbox Code Playgroud)

为什么变量a0x 0000 0000并且b0x4014 0000

c memory double int pointers

-5
推荐指数
1
解决办法
1138
查看次数