Sunday 12 June 2011

What is a Pointer ? how is a pointer initialized?, An assignment on C programming.

Point:

A pointers is a special type of variable which holds the address or location of another variable. Pointers point to these locations by keeping a record of the spot at which they were stored. Pointers to variables are found by recording the address at which a variable is stored. It is always possible to find the address of a piece of storage in C using the special & operator. For instance: if location were a float type variable, it would be easy to find a pointer to it called location_ptr.

float location;

float *location_ptr,*address;

location_ptr = &(location);

or

address = &(location);

The declarations of pointers look a little strange at first. The star * symbol which stands in front of the variable name is C's way of declaring that variable to be a pointer. The four lines above make two identical pointers to a floating point variable called location, one of them is called location_ptr and the other is called address. The point is that a pointer is just a place to keep a record of the address of a variable, so they are really the same thing.

A pointer is a bundle of information that has two parts. One part is the address of the beginning of the segment of memory that holds whatever is pointed to. The other part is the type of value that the pointer points to the beginning of. This tells the computer how much of the memory after the beginning to read and how to interpret it. Thus, if the pointer is of a type int, the segment of memory returned will be four bytes long (32 bits) and be interpreted as an integer. In the case of a function, the type is the type of value that the function will return, although the address is the address of the beginning of the function executable.

Initialization of POINTER in C:

Once a pointer is declared, the programmer must initialize the pointer, that is, make the pointer point to something. Don’t make it point to nothing; it is dangerous.Like regular variables, uninitialized pointers will not cause a compiler error, but using an uninitialized pointer could result in unpredictable and potentially disastrous outcomes.Until pointer holds an address of a variable, it isn’t useful.C uses two pointer operators:

1. Indirection operator (*) – has been explained previously.

2. Address-of-operator (&) – means return the address of.

When the & operator is placed before the name of a variable, the address-of-operator returns the memory address of the variable/operand.

For example:

#include

int main(void)

{

int *m;

int location = 200;

m = &location;

printf("The data, *m = %d\n",*m);

printf("The address where the data pointed to, m = %d\n", m);

return 0;

}

Output:

Therefore a pointer must be initialized with a statement that uses & operator as shown below:

// declare a pointer variable, m of type int

int *m;

// assign the address of variable location

// to variable m, so pointer m is pointing to variable location

m = &location;

// the actual data assigned to variable location

location = 200;

This statement means places the memory address of variable location into variable m. The memory address refers to the computer’s internal location where the actual data is stored. What and where the address is? It is determined by the system.In other word, pointer variable m receives the address of variable location or the memory address of the variable location is assigned to pointer variable m

.


Graphically it can be represented as shown below:


Let re-examine the indirection operator (*). Actually * is a complement of &. It means returns the value of the variable located at the address that follows.

From the previous example:

int *m;

m = &location;

location = 200;

q = *m;

The q = *m; statement will place the actual data value stored in variable location into variable q. That means q receives the actual data value stored at the memory address hold by variable m or the value stored in the variable location pointed to by variable m. Very confused huh?The * operator appears before a pointer variable in only two places:

1. When declaring a pointer variable.

2. When dereferencing a pointer variable (to find the data it points to).


No comments:

Post a Comment