How do I read a memory address with borland c?

1 answer

Answer

1137652

2026-03-08 00:40

+ Follow

In order to read a memory address you must hold a reference to that address. If the object at that address is a named object, then the object's name is a reference, thus we can simply take its address using the address-of operator (unary &):

int i = 42;

printf ("Variable i resides at memory address 0x%x\n", &i);

If we wish to store the address we must use a pointer variable of the appropriate type:

int* p = %i;

Like any other variable, a pointer variable has an address of its own, thus we can read its address:

printf ("Variable p resides at memory address 0x%x\n", &p);

To read the address being pointed at we simply examine the pointer's value:

printf ("Variable p refers to memory address 0x%x\n", p);

Note this address is the same as the address of i, the object being referred to (pointed at).

To read the value stored at the address being pointed at we must dereference the pointer (unary *). Dereferencing is also known as indirection because we are indirectly accessing the object's value:

printf ("Variable p refers to the value %d\n", *p); // e.g., 42

Pointers can refer to both named and anonymous objects. Anonymous objects are simply objects allocated on the heap at runtime. Since they don't exist at compile time we cannot name them:

int* anon = malloc (sizeof (int)); // allocate memory

Note that anon is the name of the pointer, not the object being pointed at.

printf ("The anonymous variable resides at memory address 0x%x\n", anon);

free (anon); // always release heap allocations as soon as we are finished with them!

Pointers also make it possible to pass memory addresses to and from functions (also known as pass by reference):

void f (int* p) {

}

int a = 42;

f (&a); // pass the address of a to the f() function

Note that all variables in C are passed by value, but when we pass a pointer by value we are passing an address and an address is a reference. Passing references is useful when the object being referred to cannot be efficiently passed by value. Typically this means any value that is larger than the Word value of the underlying architecture (e.g., 4 bytes on a 32-bit system). Arrays in particular are never passed by value, hence an array implicitly converts to a pointer.

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.