A Simple Address-Of (&) Program in C

By Paul Quek (e-mail: quekpaul@hotmail.com), in Singapore


In "Hour 11" of Tony Zhang's Teach Yourself C in 24 Hours, First Edition, published by Sams Publishing in 1997, ( this is one book on C that I had really enjoyed reading -- Thanks, Mr. Zhang! ), the first program illustrates what the address-of  operator ( which is the ampersand character, & ) does.

Here's the original listing:

Listing 11.1. Obtaining the left values of variables.

1:  /* 11L01.c: Obtaining addresses */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     char  c;
7:     int   x;
8:     float y;
9:
10:    printf("c: address=0x%p, content=%c\n", &c, c);
11:    printf("x: address=0x%p, content=%d\n", &x, x);
12:    printf("y: address=0x%p, content=%5.2f\n", &y, y);
13:    c = 'A';
14:    x = 7;
15:    y = 123.45;
16:    printf("c: address=0x%p, content=%c\n", &c, c);
17:    printf("x: address=0x%p, content=%d\n", &x, x);
18:    printf("y: address=0x%p, content=%5.2f\n", &y, y);
19:    return 0;
20: }

The output from this program is also given in the text:

C:\app> 11L01
c: address=0x1AF4, content=@ x: address=0x1AF2, content=-32557 y: address=0x1AF6, content=0.00 c: address=0x1AF4, content=A x: address=0x1AF2, content=7 y: address=0x1AF6, content=123.45 C:\app>

Because I am inclined to modify any source program or fiddle with any source code ( that's part of the fun of reading computer programming books -- trying out the programs and then modifying them, little by little ), here's the final modified program:

/* 11L01m.c: Obtaining addresses */ /* "m" above indicates "modified" */ #include <stdio.h> main() { char cAlphabet; int iDigit; float fDigit; printf("Memory Addresses of 3 Variables, with their Random Contents\n\n"); printf("cAlphabet  : address=0x%p,    content=%c\n", &cAlphabet, cAlphabet); printf("iDigit      : address=0x%p,    content=%d\n", &iDigit, iDigit); printf("fDigit      : address=0x%p,    content=%5.2f\n\n\n\n\n", &fDigit, fDigit); /* Now we do some assignments */ printf("Now we do some assignments to the variables ...\n\n"); printf("cAlphabet = 'A';\n"); printf("iDigit    = 7;\n"); printf("fDigit    = 123.45;\n\n"); cAlphabet = 'A'; iDigit    = 7; fDigit    = 123.45; printf("Same Memory Addresses of Same 3 Variables, with their New, Assigned Contents\n\n"); printf("cAlphabet  : address=0x%p,    content=%c\n", &cAlphabet, cAlphabet); printf("iDigit      : address=0x%p,    content=%d\n", &iDigit, iDigit); printf("fDigit      : address=0x%p,    content=%5.2f\n\n\n", &fDigit, fDigit); printf("Note: all variables use the Hungarian Notation,\n"); printf("where, in this case, c is character-type, i is integer-type, and f is float-type.\n"); return 0; }

Here is the output:

Memory Addresses of 3 Variables, with their Random Contents cAlphabet  : address=0x2ACF:2675, content=U iDigit      : address=0x2ACF:2672, content=11007 fDigit      : address=0x2ACF:266E, content= 0.00 Now we do some assignments to the variables ... cAlphabet = 'A'; iDigit    = 7; fDigit    = 123.45; Same Memory Addresses of Same 3 Variables, with their New, Assigned Contents cAlphabet  : address=0x2ACF:2675, content=A iDigit      : address=0x2ACF:2672, content=7 fDigit      : address=0x2ACF:266E, content=123.45 Note: all variables use the Hungarian Notation, where, in this case, c is character-type, i is integer-type, and f is float-type.

Here is the downloadable zipped file:

  • A Simple Address-Of (&) Program in C -- 11L01m.c and 11L01m.exe






  • Return to homepage