繁体   English   中英

反转 32 位整数数组的元素顺序

[英]Reverse the order of the elements of an array of 32-bit integers

我有这个任务:

Reverse the order of an array of 32-bit integers

所以,我有这个数组:

 { 0x12345678, 0xdeadbeef, 0xf00df00d };

它应该如下所示:

{ 0xf00df00d, 0xdeadbeef, 0x12345678 };

我试过这个,但没有成功:

#include <stdint.h>

void reverse_array ( uint32_t *array, unsigned int count ) {
    uint32_t array[3] = {0x12345678, 0xdeadbeef, 0xf00df00d };
    reverse_array ( array, 3);
}

但它抛出了我:

main.c: In function ‘reverse_array’:
main.c:12:10: error: ‘array’ redeclared as different kind of symbol
uint32_t array[3] = {0x12345678, 0xdeadbeef, 0xf00df00d };
         ^~~~~
main.c:11:32: note: previous definition of ‘array’ was here
void reverse_array ( uint32_t *array, unsigned int count ) {
                               ^~~~~

此错误消息告诉您正在声明两个具有相同名称的不同变量:

void reverse_array ( uint32_t *array, unsigned int count )

在这里,您声明一个名称为array的参数。

    uint32_t array[3] = {0x12345678, 0xdeadbeef, 0xf00df00d };

在这里你声明了一个同名的局部变量。

问题是您将应该在main() function 中的代码放在reverse_array()中。 所以你的代码应该是这样的:

#include <stdint.h>

void reverse_array ( uint32_t *array, unsigned int count ) {
   // You need to figure out what code to put here
}

void main() {
    uint32_t array[3] = {0x12345678, 0xdeadbeef, 0xf00df00d };
    reverse_array ( array, 3);
}

现在您需要弄清楚如何实际反转数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM