gpt4 book ai didi

无法更新指针值

转载 作者:太空宇宙 更新时间:2023-11-04 06:11:18 24 4
gpt4 key购买 nike

我可能对指针的工作原理存在根本性的误解,但我认为我可以为指针分配一个变量值,但是每当我打印指针的取消引用值时,它始终为 0 而不是“timestamp”的值。

volatile uint32_t *myAddress = (volatile uint32_t*)0x12341234;

uint32_t timestamp = 0x1111;

*myAddress = timestamp;

最佳答案

Cannot update pointer value

你的意思是不能更新指向的值

 volatile uint32_t *myAddress = (volatile uint32_t*)0x12341234;

uint32_t timestamp = 0x1111;

*myAddress = timestamp;

您使用(很可能)无效地址 0x12341234,以推断它具有未定义的行为

做类似的事情:

uint32_t v;

volatile uint32_t *myAddress = &v;

uint32_t timestamp = 0x1111;

*myAddress = timestamp;
// now v values 0x1111

示例:

#include <stdio.h>
#include <stdint.h>

int main()
{
uint32_t v = 0;

volatile uint32_t *myAddress = &v;

uint32_t timestamp = 0x1111;

*myAddress = timestamp; // now v values 0x1111

printf("0x%x 0x%x\n", (unsigned) v, (unsigned) *myAddress);

return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
0x1111 0x1111
pi@raspberrypi:/tmp $

关于无法更新指针值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55984637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com