Giving an address value to a pointer

A

Akanksha

HI all

I am doing some low level programming in C and I want to assign my
pointer a specific memory address.
Currently I am just writing

void* temp = 0x80100000;

I get the following error: value of type "unsigned int" cannot be
used to initialize an entity of type "void *"

How can i assign this memory address to a pointer in C?

Akanksha
 
S

sharath.ee

HI all

I am doing some low level programming in C and I want to assign my
pointer a specific memory address.
Currently I am just writing

void* temp = 0x80100000;

I get the following error: value of type "unsigned int" cannot be
used to initialize an entity of type "void *"

How can i assign this memory address to a pointer in C?

Akanksha

Hi


Even i have the same problem...How do u assign a memory address to a
pointer in C (not c++)..

Sharath
 
C

CryptiqueGuy

HI all

I am doing some low level programming in C and I want to assign my
pointer a specific memory address.
Currently I am just writing

void* temp = 0x80100000;

I get the following error: value of type "unsigned int" cannot be
used to initialize an entity of type "void *"

This initialization is a constraint violation. So some form of
diagnostic is mandatory. Thats the reason you get an error.

Use:
void* temp = (void*)0x80100000;
How can i assign this memory address to a pointer in C?

By casting it to the requisite pointer type you can assign (or
intialize) the given integer constant to the pointer.
 
K

Keith Thompson

Akanksha said:
I am doing some low level programming in C and I want to assign my
pointer a specific memory address.
Currently I am just writing

void* temp = 0x80100000;

I get the following error: value of type "unsigned int" cannot be
used to initialize an entity of type "void *"

How can i assign this memory address to a pointer in C?

*If* you can do it, the syntax is:

void *temp = (void*)0x80100000;

The language allows any integer type to be converted to a pointer
type, but such conversions cannot be done implicitly; a cast is
required. The result of the conversion is implementation-defined, and
may not be a valid pointer -- but if you happen to know that the
result of converting 0x80100000 to void* is valid on your system,
that's how to do it.
 
A

Army1987

Akanksha said:
HI all

I am doing some low level programming in C and I want to assign my
pointer a specific memory address.
Currently I am just writing

void* temp = 0x80100000;

I get the following error: value of type "unsigned int" cannot be
used to initialize an entity of type "void *"

How can i assign this memory address to a pointer in C?

void *temp = (void *)0x80100000;
And good luck. What you're doing is implementation-defined at best,
and (depending on what you're going to do after that) undefined
behaviour at worst.
 
C

CBFalconer

Akanksha said:
I am doing some low level programming in C and I want to assign my
pointer a specific memory address.
Currently I am just writing

void* temp = 0x80100000;

I get the following error: value of type "unsigned int" cannot be
used to initialize an entity of type "void *"

How can i assign this memory address to a pointer in C?

You can't in standard C. For your implementation (non-portable)
see your system documentation.
 
W

Walter Roberson

Keith Thompson said:
void *temp = (void*)0x80100000;
The language allows any integer type to be converted to a pointer
type, but such conversions cannot be done implicitly; a cast is
required. The result of the conversion is implementation-defined, and
may not be a valid pointer -- but if you happen to know that the
result of converting 0x80100000 to void* is valid on your system,
that's how to do it.

Expanding slightly on with Keith said:

Even if there is a memory address 0x80100000, and you are
allowed to access it (e.g., some OS's reserve addresses with
the high bit set for kernel use), the C language makes no
promise that

void *temp = (void*)0x80100000;

will result in temp pointing to memory address 0x80100000 .
The behaviour is, as Keith noted, implementation-defined,
and the implementation could choose to define any of a number
of weird, wonderful, or non-sensical conversions. For example,
it could require that to get at address 0x80100000 that you
had to "just know" that the integer to convert is 0xDEAD1112.
Or it could (plausibly, even) require that the integer be in a
specific "endian" order such as 0x00008010 . Check your system
documentation for details.
 
K

Keith Thompson

Expanding slightly on with Keith said:

Even if there is a memory address 0x80100000, and you are
allowed to access it (e.g., some OS's reserve addresses with
the high bit set for kernel use), the C language makes no
promise that

void *temp = (void*)0x80100000;

will result in temp pointing to memory address 0x80100000 .
The behaviour is, as Keith noted, implementation-defined,
and the implementation could choose to define any of a number
of weird, wonderful, or non-sensical conversions. For example,
it could require that to get at address 0x80100000 that you
had to "just know" that the integer to convert is 0xDEAD1112.
Or it could (plausibly, even) require that the integer be in a
specific "endian" order such as 0x00008010 . Check your system
documentation for details.

Yes, but most implementations will behave sanely. A footnote
in C99 6.3.2.3 says:

The mapping functions for converting a pointer to an integer or an
integer to a pointer are intended to be consistent with the
addressing structure of the execution environment.

This is non-binding, but an implementation would have to be
deliberately perverse to violate it if the underlying system allows
such conversions to be done sensibly.
 
A

Army1987

CBFalconer said:
You can't in standard C. For your implementation (non-portable)
see your system documentation.

Yes. void *temp = (void *)0x80100000; it is the result which is
implementation-defined, but the construct itself is standard C.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,174
Latest member
BlissKetoACV
Top