Reverse the order of the elements of an array of 32-bit integers

-3

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 ) {
                               ^~~~~
c
arrays
asked on Stack Overflow Oct 28, 2019 by NeoVe • edited Oct 28, 2019 by tadman

1 Answer

3

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.

    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(). 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.

answered on Stack Overflow Oct 28, 2019 by Code-Apprentice • edited Oct 28, 2019 by Code-Apprentice

User contributions licensed under CC BY-SA 3.0