Mohd Khalid Kasmin wrote:
 
> Thank you all for helping me in this matter. My understanding of
> * and & operator still very confused. I hope I'll get the hang of
> it soon.

Most (all?) C-Programming books contains at least one chapter dedicated
to pointers, and most C programmers are confused by pointers in the
beginning (and every now and then). pointers are a big source of
headaches.

In short:

Every variable/structure is stored somewhere in memory.

* references (or declares) a pointer that can have the address to
something. When a function declararation has pointer arguments then the
function expects addresses to things it shoud read parameters from or
write results to.

& takes the address to something. This address can be stored in a
pointer variable or sent to a function expectin a pointer. 

---
Henrik Nordström


A short example: (hopefully not confusing things even more)

/* A short macro that prints all values */
int printnr=0;
#define PRINTTHEM() \
  printf("%d: value1=%d value2=%d pointer=%p *pointer=%d\n", ++printnr,
\
         value1, value2, pointer, *pointer)

/* A few variables to play with */
int value1;    /* A normal variable of type int */
int value2;    /* Another normal variable of type int */
int *pointer;  /* A pointer variable that can point at a int */

/* Print addresses to value1 and value2 */
printf("    &value1=%p &value2=%p\n", &value1, &value2);

/* And some fun ;-) */
value1 = 44;   /* value1 assigned 44 */
value2 = 77;   /* value2 assigned 77 */
pointer = &value1; /* Pointer assigned the address of value1 */
PRINTTHEM();   /* v1=44 v2=77 p=&v1 *p=44 */
pointer = &value2; /* Pointer assigned the address of value2 */
PRINTTHEM();   /* v1=44 v2=77 p=&v2 *p=77 */
*pointer = 55; /* Assign the variable pointer points at (value2) the
value 55 */
PRINTTHEM();   /* v1=44 v2=55 p=&v2 *p=55 */
pointer = &value1; /* try to figure out how the rest of the comments
should read.. */
*pointer = 88;
PRINTTHEM();
pointer = &value2;
PRINTTHEM();
/* THE END */

Reply via email to