When an array is declared in a program, the compiler allocates a base address and sufficient amount of storage to contain all the elements of the array in contiguous (continuous) memory locations. The base address is the location of the first element (index 0) of the array. The compiler also defines the array name as a constant pointer to the first element. Suppose array x is declared as shown below:
1
|
int x[5] = {1,2,3,4,5};
|
Suppose the array is allocated memory as shown below:.
The name x is defined as a constant pointer pointing to the first element, x[0]and therefore the value of x is 2000, the location where x[0] is stored.
If we declare p as an integer pointer, then we can make the pointer p point to the array x by the following assignment:
1
|
p = x
|
In a one dimensional array the address of element n can be calculated by using the below expression:
address = base address + (index * scale factor)
In a two dimensional array with m rows and n columns the address of an element can be calculated as shown below:
address = base address + [(i * n + j) * scale factor]
Where i represents ith row and j represents jth column.
The computer’s memory is a sequential collection of storage cells. Each cell, commonly known as a byte, has a number called address associated with it. Typically, the addresses are numbered consecutively, starting from zero. The last address depends on the memory size. A computer system having 64k memory will have its last address as 65,535.
Whenever we declare a variable in our programs, the system allocates somewhere in the memory, an appropriate location to hold the value of the variable. This location will have its own address number.
A pointer is a derived data type in C. It is built from one of the fundamental data types available in C. Pointers contain memory addresses as their values. Since these memory addresses are the locations in the computer memory where program instructions and data are stored, pointers can be used to access and manipulate data stored in the memory.
Take your time to comment on this article.
Comments
Post a Comment