karthikramesh55
karthikramesh55

Reputation: 3

How can one use pointer constants to point to a specific memory address (e.g. an address like 0x0001a00) in ANSI C?

I am experimenting with pointers in C programming for a project and was looking to get some guidance on whether there are other ways to initialize a pointer constant to the memory address 0x0001a000.

The following was my approach:

volatile int *firstAddress = (volatile int *)0x0001a000; 
printf("First Memory address is: %p\n", firstAddress);

Are there shorter ways to initialize the above in C programming?

Upvotes: 0

Views: 52

Answers (1)

dbush
dbush

Reputation: 224677

This is exactly how you would initialize such a constant, however the results are very implementation specific.

If the given address isn't one explicitly documented as valid, you'll likely invoke undefined behavior.

You also can't really make it any more concise than this. Conversions between integers and pointers requires a cast.

Upvotes: 1

Related Questions