c pointers

c pointers

We know c is very powerful language and the real power of c lies in pointers. The use of pointers makes the code more efficient. There are some use of pointers are

(i) Returning more than one value from function
(ii) Accessing DMA(Dynamic memory allocation)
(iii) Implementation Data structures using trees, linked list and graphs.
(iv) Simulation call by reference.
(v) Improving efficiency

About memory

so before studying pointers this is important to understand how memory is allocated or organized in computer.The memory in the computer arrange data in bytes or made up of bytes and arrange in sequential manner. And each byte is identify by a block number or index number which is called address of that byte.

We know that before using any variable it is necessary to declare that variable. since compiler reserve space for that variable. so datatype also declare with the variable name so compiler can know how much space needs to be reserved. let us take example how the compiler reserve space for variable or what happens when declare variable in c. suppose we declare salary of type int

Memory

so compiler has reserved bytes numbered 2530, 2531, 2532, 2533 and 2534 for the variable salary, so the Base address of variable salary is 2530 . so we assign some value to this variable salary=3000;

in this example value will be stored in 4 bytes in the binary form. The number of bytes allocated depend on the size of data types of variables, such as take example if we take double data type it will allocates 8 bytes.

Address Operator

after the concept of memory organize let us understand about the address operator concept. C provides an address operator (&) which return or give the address of variable when any variable follow by (&) operator or when place & operator before the variable.This operator is called as “the address of” so &salary means address of variable salary, lets us see example this program print the address of variable salary using address operator.

/* programm to print address of variable using address operator*/
#include<stdio.h>
int
main()
{
int age;
int id,salary;
printf("Address of age=%p \n",&age);
printf("Address of Id=%p \n",&id);
printf("Address of salary=%p \n",&salary);
return 0;
}

OUTPUT

Address of age=0088AGC3
Address of Id=9903dddd
Address of salary=GDe44443

The format specifier %p is used to display or print address. These address may be different time wise when you run your program. Depends on operating system.

The Address operator can not be used with the constant and the register variable or expression. Let us take some examples

&k valid used with any variable
&arr[0] valid used with any array element
&456 Invalid used with any constant
&(a-b) Invalid used with any expression
&ghn Invalid with ghn register variable

Pointer variable

A pointer is a variable that stores memory location. as other variables pointer has also name, and has to be declare and consume some space in memory.it is called pointer because its point to targeted memory in location.

Declaration of pointer variable

like other variables in programming, pointer variable should also be declare before used. so The general syntx of pointer declaration is

data_type *pname;

In this example pname is the name of pointer variable, which should be a valid identifier in c, the (*) precedence tell compiler that variable is declare as a pointer. and the data type is the type of data valid in c

Let us take example with pointer declaration

int *ptr;
float *fptr;
char *chptr;

In the above example here ptr is the pointer variable that points to variable of int type, similarly fptr and chptr are pointer variables that points to float and char type.

talk about default value of pointers global and static pointer automatic initialize to NULL but when we declare an automatic pointer variable its default value is garbage. so we should assign any address before using it.

int *ptr,salary=3000;
float *fptr,f=7.8;
ptr=&salary;
fptr=&f;


So in this example ptr contains the address of salary variable, and it points to salary variable same as fptr contains the address of f variable

pointer

In this example value of pointer ptr is 7000 which is the address of salary variable. and the value of fptr is 4020 which is the address of f