Andrei Tudora
Andrei Tudora

Reputation: 1

Array to variable

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?

Upvotes: 0

Views: 64

Answers (2)

Mike
Mike

Reputation: 49433

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)

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409216

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.

Upvotes: 1

Related Questions