×
☰ Menu

Accessing the Address of a Variable

Pointer is a variable. It is used to store the address of any other variable. To be more specific, an integer pointer points to an integer variable, a character pointer points to a character variable. In general, a pointer of specific type points to a variable of the same type. All pointers take only 2 bytes.

Because the actual position of a variable in memory is system-dependent, we don't know the address of a variable right away. So, how can we figure out what a variable's address is? This may be done using the & operator and is available in C. We have already seen the use of this address operator in the scanf function. The & operator immediately preceding a variable returns the address of the variable associated with it.

For example:

p = &myname;

would assign the address 12345 (Location of the variable) to the variable p. The & operator can be recalled as 'address of'. The & operator can be used only with a simple variable or an array element.

 

The following are illegal use of (&) address operator:

1: &125 (pointing at constants).

2: int y[10];

&y (pointing at array names).

3: &(x+y) (pointing at expressions). If x is an array, then expressions such as &x[0] and &x[i+3]