用C指针指针数组?

时间:2021-09-10 21:14:01

I want an array of pointers and I want to set byte values in the memory addresses where the pointers (of the array) are pointing.

我想要一个指针数组,我想在指针(数组)指向的内存地址中设置字节值。

Would this work:

这会工作:

unsigned int *pointer[4] = {(unsigned int *) 0xFF200020, (unsigned int *) 0xFF20001C, (unsigned int *) 0xFF200018, (unsigned int *) 0xFF200014};
*pointer[0] = 0b0111111; // the value is correct for the address

Or is the syntax somehow different?

或者语法有些不同?

EDIT: I'm coding for an SOC board and these are memory addresses that contain the case of some UI elements.

编辑:我正在编写SOC板,这些是包含一些UI元素的内存地址。

unsigned int *element1 = (unsigned int *) 0xFF200020;
*element1 = 0b0111111;

works so I'm just interested about the C syntax of this.

工作所以我只对这个C语法感兴趣。

EDIT2: There was one 0 too much in ... = 0b0...

编辑2:在... = 0b0中有一个0太多...

3 个解决方案

#1


1  

Short answer:

Everything you've written is fine.

你写的一切都很好。

Thoughts:

  • I'm a big fan of using the types from stdint.h. This would let you write uint32_t which is more clearly a 32 bit unsigned number than unsigned long.

    我非常喜欢使用stdint.h中的类型。这可以让你写uint32_t,它更明显是一个32位无符号数而不是无符号长数。

  • You'll often see people write macros to refer to these registers:

    您经常会看到人们编写宏来引用这些寄存器:

    #define REG_IRQ (*(volatile uint32_t *)(0xFF200020))
    REG_IRQ = 0x42;
    
  • It's possible that you actually want these pointers to be to volatile integers. You want it to be volatile if the value can change outside of the execution of your program. That is, if that memory position doesn't act strictly like a piece of memory. (For example, it's a register that stores the interrupt flags).

    您可能希望这些指针成为易失整数。如果值可以在程序执行之外更改,那么您希望它是易失性的。也就是说,如果该记忆位置不像一块记忆那样严格。 (例如,它是一个存储中断标志的寄存器)。

    With most compilers I've used on embedded platforms, you'll have problems from ignoring volatile once optimizations have been enabled.

    对于我在嵌入式平台上使用的大多数编译器,一旦启用了优化,就会忽略volatile。

  • 0b00111111 is, sadly, non-standard. You can use octal, decimal, or hexadecimal.

    遗憾的是,0b00111111是非标准的。您可以使用八进制,十进制或十六进制。

#2


1  

Sure, this should work, providing you can find addresses in your own segment.

当然,这应该可行,只要您可以在自己的细分中找到地址。

Most probably, you'll have a segmentation fault when running this code, because 0xFF200020 have really few chances to be in your program segment.

最有可能的是,运行此代码时会出现分段错误,因为0xFF200020很少有机会进入您的程序段。

#3


1  

This will not throw any error and will work fine but hard-coding memory address the pointer is pointing to is not a good idea. De-referencing some unknown/non-existing memory location will cause segmentation fault but if you are sure about the memory location and hard-coding values to them as done here is totally fine.

这不会抛出任何错误,并且可以正常工作,但指针指向的硬编码内存地址不是一个好主意。取消引用某些未知/不存在的内存位置将导致分段错误,但如果您确定内存位置并对其进行硬编码值,则完全没问题。

#1


1  

Short answer:

Everything you've written is fine.

你写的一切都很好。

Thoughts:

  • I'm a big fan of using the types from stdint.h. This would let you write uint32_t which is more clearly a 32 bit unsigned number than unsigned long.

    我非常喜欢使用stdint.h中的类型。这可以让你写uint32_t,它更明显是一个32位无符号数而不是无符号长数。

  • You'll often see people write macros to refer to these registers:

    您经常会看到人们编写宏来引用这些寄存器:

    #define REG_IRQ (*(volatile uint32_t *)(0xFF200020))
    REG_IRQ = 0x42;
    
  • It's possible that you actually want these pointers to be to volatile integers. You want it to be volatile if the value can change outside of the execution of your program. That is, if that memory position doesn't act strictly like a piece of memory. (For example, it's a register that stores the interrupt flags).

    您可能希望这些指针成为易失整数。如果值可以在程序执行之外更改,那么您希望它是易失性的。也就是说,如果该记忆位置不像一块记忆那样严格。 (例如,它是一个存储中断标志的寄存器)。

    With most compilers I've used on embedded platforms, you'll have problems from ignoring volatile once optimizations have been enabled.

    对于我在嵌入式平台上使用的大多数编译器,一旦启用了优化,就会忽略volatile。

  • 0b00111111 is, sadly, non-standard. You can use octal, decimal, or hexadecimal.

    遗憾的是,0b00111111是非标准的。您可以使用八进制,十进制或十六进制。

#2


1  

Sure, this should work, providing you can find addresses in your own segment.

当然,这应该可行,只要您可以在自己的细分中找到地址。

Most probably, you'll have a segmentation fault when running this code, because 0xFF200020 have really few chances to be in your program segment.

最有可能的是,运行此代码时会出现分段错误,因为0xFF200020很少有机会进入您的程序段。

#3


1  

This will not throw any error and will work fine but hard-coding memory address the pointer is pointing to is not a good idea. De-referencing some unknown/non-existing memory location will cause segmentation fault but if you are sure about the memory location and hard-coding values to them as done here is totally fine.

这不会抛出任何错误,并且可以正常工作,但指针指向的硬编码内存地址不是一个好主意。取消引用某些未知/不存在的内存位置将导致分段错误,但如果您确定内存位置并对其进行硬编码值,则完全没问题。