Minggu, 13 Oktober 2013

Pointer values in C language

Suppose, we have the following declaration:

int i=100, j=200, k=300;

This declaration tells the compiler to perform the following activities:

  • Reserve space for three integer values in memory.
  • Associate the variables i,j and k with these memory location.
  • Store 100,200 and 300 at the locations i,j and k respectively as shown as below:

Variable
Address
Memory Locations

0



2



4
100
I
……..
200
J
65534
300
K



variables
address
Values



The address of the variable cannot be accessed directly. The address of the variable can be obtained using address operator (& in C) .

Note : The address operator can be used with any variable that can be placed on the left side of an assignment operator.Since constants, expressions and array names can not be used on the left hand side of the assignment, and hence accessing address is invalid for constants, expressions and array names. The following are invalid:


Usage
Valid/Invalid
Reasons for invalidity
&100
Invalid
Address of a constant cannot be obtained.
&(p+100)
Invalid
Address of an expression cannot be obtained.
&(p+q)

Invalid
Address of an expression cannot be obtained.
Int a[10];
&a;
Invalid
Address of entire array cannot be obtained.
Register a;
&a;
Invalid
Address of a register variable cannot be obtained.

Note : if a is an array , then address of the first location of the array is obtained either using : &a[0] or a.

Assume that the compiler has chosen the address 65530 for the variable i, the address 65532 for the variable j and 65534 for the variable k. These address assigned to variables i,j and k are called pointer values.

Definition : Memory is divided into number of storage cells called locations. All the locations in computer memory are numbered sequentially from 0 to 65535 ( with memory size of 64k) . Out of these address, the system assigns some addresses of the memory locations to the variables. These memory addresses assigned to variables by the system are called pointer values. 

Tidak ada komentar:

Posting Komentar