Marouane Sharry
Marouane Sharry

Reputation: 49

Define a register as a macro in C?

I'm trying to solve a project in C (beginner - college level) and I have a small problem in a function .

I have two registers defined as macros :

#define register1 0x02004000
#define register2 0x02004200

I want to access to the content of the first register and put in it the content of the 2nd register.

I do the following :

*register1 = *register2 + 2 

But it won't work because "#define" defines the registers as ints .

So how can I access the registers with the use of #define ?

(It might be a bad proposition but I'm still learning C :D )

Upvotes: 0

Views: 737

Answers (1)

dbush
dbush

Reputation: 224832

You need to cast the constants to the appropriate pointer type.

So for example if each of these points to an int, you would do the following:

#define register1 ((int *)0x02004000)
#define register2 ((int *)0x02004200)

And technically, what you have aren't actually registers but memory addresses. Registers are part of the CPU and don't have an address.

Upvotes: 3

Related Questions