简体   繁体   English

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

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

I have this task:我有这个任务:

Reverse the order of an array of 32-bit integers

So, I have this array:所以,我有这个数组:

 { 0x12345678, 0xdeadbeef, 0xf00df00d };

It should look like this:它应该如下所示:

{ 0xf00df00d, 0xdeadbeef, 0x12345678 };

I've tried this, but with no success:我试过这个,但没有成功:

#include <stdint.h>

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

But it throws me:但它抛出了我:

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 ) {
                               ^~~~~

This error message tells you that you are declaring two different variables with the same name:此错误消息告诉您正在声明两个具有相同名称的不同变量:

void reverse_array ( uint32_t *array, unsigned int count )

Here you declare a parameter with the name array .在这里,您声明一个名称为array的参数。

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

And here you declare a local variable with the same name.在这里你声明了一个同名的局部变量。

The problem is that you put the code that should be in your main() function inside of reverse_array() .问题是您将应该在main() function 中的代码放在reverse_array()中。 So your code should look like this:所以你的代码应该是这样的:

#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);
}

Now you need to figure out how to actually reverse the array.现在您需要弄清楚如何实际反转数组。

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

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