How to Check a Variable’s Location in C Programming - dummies

How to Check a Variable’s Location in C Programming

By Dan Gookin

In C programming, a variable’s type and size are uncovered first by declaring that variable as a specific type, but also by using the sizeof keyword. The second description of a variable, its contents, can be gleaned by reading the variable’s value using the appropriate C language function.

The third description of a variable is its location in memory. You gather this information by using the & operator and the %p placeholder, as shown in O Variable, Wherefore Art Thou?

O VARIABLE, WHEREFORE ART THOU?

#include <stdio.h>
int main()
{
 char c = 'c';
 int i = 123;
 float f = 98.6;
 double d = 6.022E23;
 printf("Address of 'c' %pn",&c);
 printf("Address of 'i' %pn",&i);
 printf("Address of 'f' %pn",&f);
 printf("Address of 'd' %pn",&d);
 return(0);
}

When the & operator prefixes a variable, it returns a value representing the variable’s address, or its location in memory. That value is expressed in hexadecimal. To view that value, the %p conversion character is used, as shown in O Variable, Wherefore Art Thou?

Exercise 1: Type the source code from O Variable, Wherefore Art Thou? into your editor. Build and run.

The results produced by the program generated from Exercise 1 are unique, not only for each computer but also, potentially, for each time the program is run.

Address of 'c' 0x7fff5fbff8ff
Address of 'i' 0x7fff5fbff8f8
Address of 'f' 0x7fff5fbff8f4
Address of 'd' 0x7fff5fbff8e8

Variable c is stored in memory at location 0x7fff5fbff8ff — that’s decimal location 140,734,799,804,671. Both values are trivial, of course; the computer keeps track of the memory locations, which is just fine by me.

image0.jpg

This illustration shows how those addresses map out in memory.

Individual array elements have memory locations as well, as shown in Memory Locations in an Array on Line 10. The & operator prefixes the specific element variable, coughing up an address. The %p conversion character in the printf() function displays the address.

MEMORY LOCATIONS IN AN ARRAY

#include <stdio.h>
int main()
{
 char hello[] = "Hello!";
 int i = 0;
 while(hello[i])
 {
 printf("%c at %pn",hello[i],&hello[i]);
 i++;
 }
 return(0);
}

Exercise 2: Create a new project by using the source code shown in Memory Locations in an Array. Build and run.

Again, memory location output is unique on each computer.

H at 0x7fff5fbff8f0
e at 0x7fff5fbff8f1
l at 0x7fff5fbff8f2
l at 0x7fff5fbff8f3
o at 0x7fff5fbff8f4
! at 0x7fff5fbff8f5

Unlike the example from Exercise 1, the addresses generated by Exercise 2 are contiguous in memory, one byte after another.

Exercise 3: Code a program to display five values in an int array along with each element’s memory address. You can use Memory Locations in an Array to inspire you, although a for loop might be easier to code.

  • By the way, the & memory location operator should be familiar to you. It’s used by the scanf() function, which requires a variable’s address, not the variable itself. That’s because scanf() places a value at a memory location directly. How? By using pointers, of course!

  • The & operator is also the bitwise AND operator; however, the compiler is smart enough to tell when & prefixes a variable and when & is part of a binary math equation.