Hi all,
consider this,
int x=19;
now &x = 1789; /*this is illegal in c*/
now my question is is there a round about way to do this in
C....??????
The only circumstances I can think of for doing this is when you're
writing nonportable code that needs to access memory directly. Some
compilers have their own directives for doing this; there's no
guaranteed portable way to do this, because it's a nonportable thing
to do to begin with, but the method I use is
#define x (*(int*)1789) /* I'd actually write the memory location in
hexadecimal */
This method doesn't give you a variable x, but such a concept would be
dubious anyway. Your line
int x=19;
is equivalent to
auto int x=19; /* nobody ever uses auto because it's everywhere either
forbidden or default */
which tells the compiler to find storage for x that lasts until it
goes out of scope; so if you place it in a function that calls itself
recursively, there have to be two different xs in the different
invocation. You appear to be trying to place them both at memory
location 1789, which contradicts the semantics of auto, as well as
being nonportable. Causing a static variable to be placed at a
particular memory location would be more reasonable (and I've come
across a compiler for a language which strongly resembled C which had
a #pragma to do it (although it violated various parts of the
standards, for instance it had an 8-bit int by default)), but still
inadvisable unless you know what you're doing and are willing to write
nonportable code, and there's no syntax that can be used to portably
do that. The #define method is about as portable as the dubious
operation of writing to fixed memory locations is anyway, and protects
you from doing things like trying to declare a variable in a fixed
memory location as auto; it does have problems with scoping, but those
can be partially reduced using #undef if you really need to (but
having such values program-wide has been useful the few times I've
wanted to use it).
If you're not trying to access a fixed memory location (that operation
only makes sense on microcontrollers and in other situations where
you're trying to interface with hardware directly rather than go
through the parts of an implementation that make code portable), what
are you trying to do?