0

I have a C code that I'm analizing and there is something like this:

variable = (unsigned long)rx;

If rx is an array of hex numbers and variable is of unsigned long, what will variable hold? The first element in unsigned long format?

hsz
  • 148,279
  • 62
  • 259
  • 315

2 Answers2

1

Remember that arrays decays to pointers. So what will happen is that the cast will convert the pointer (i.e. memory address) to an unsigned long and assign that to variable.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

Since arrays decay to pointers anytime you use an array's name, and do not deference it, you're going to get an address.

thus:

variable = (unsigned long)rx;

Will assign rx[]'s address to variable as an unsigned long value. Whereas this:

variable = (unsigned long)*rx;

or this:

variable = (unsigned long)rx[0];

will give you the value of the first element of rx[] (note that the syntax rx[0] is basically saying take the base address of rx + a 0 offset, dereference it and return the result)

Community
  • 1
  • 1
Mike
  • 47,263
  • 29
  • 113
  • 177