1. Shroff S.R. Rotary Institute of Chemical Technology
Principle Supporter & Sponsor-United Phosphorous Ltd(UPL)/Shroff family
Managed By Ankleshwar Rotary Education Society
Approved by AICTE, New Delhi, Govt. of Gujarat & GTU Affiliated
2.
3. Definition :
Pointers are the variables that contain the address of another
variable within the memory.
Need of Pointers :
1) It enhances the capability of the language to manipulate data.
2) Pointers reduce the length and complexity of the program.
3) It is used for creating data structures such as linked lists, trees,
graphs and so on.
4) It increases the execution speed.
4. Basics of Pointers :
Every data item in the computer is stored in the memory in one or
more adjacent locations depending upon its type. The computer’s
memory is sequential collection of storage cells. Each cell is
known as byte and has a number called address associated with
it. The addresses are named in a serial manner, starting from
zero. A computer system having 64 k memory will have its last
address as 65,535.
……………………………………Memory Cell
Address 0 1 2 3 4 …………………………………….. 65535
Memory Organization
5. Example : int x = 5;
The compiler automatically assigns memory for this value. Since every
location in the memory has a unique address, we represent the x’s location in
the memory as below :
x variable
5 value
1002 address
The variable x is associated with the memory address 1002. Since
memory addresses are just numbers, they can also be assigned to any
variables that can be stored in the memory location, like other simple
variables. Such variables that contain the memory address are called
Pointers.
Representation of x=5 in the memory
6. Pointer Declaration :
Syntax :
data_type *ptvar;
Name of the pointer variable.
Asterisk (*) indicates that this variable is a pointer.
Data type of the pointer’s object.
This statement tells the compiler that :
1) ptvar is a pointer.
2) ptvar needs memory location.
3) ptvar points to a variable of type data_type.
7. Examples :
int *p; - declares that ‘p’ is a pointer that points to an integer
data type (int refers to the data type of the variable
being pointed to by p).
float *abc; - declares that ‘abc’ is a pointer that points to a floating
point variable.
char *status; - declares that ‘status’ is a pointer that points to a char
variable.
8. Array of Pointers :
Since pointers are the variables that store the address, thus, array of
pointers is the collection of addresses.
Syntax : data_type *array_name [size];
Example : int arr[] = {2,1,8,10};
int *ptr[] = {arr, arr+1, arr+2, arr+3};
The above example is represented in the memory as below :
arr[0] arr[1] arr[2] arr[3]
2 1 8 10arr
201 202 203 204
ptr[0] ptr[1] ptr[2] ptr[3]
201 202 203 204ptr
3011 3012 3013 3014
9. Functions and Pointers :
A function can take a pointer of any data type as argument and can return
a pointer of any data type.
Pointers to Functions :
A pointer to function is defined as the address of the code executed
when the function is called.
Syntax :
return_type *pointer_name(argument list);
The * along with the pointer_name acts as the function name.
10. Example :
float *maxp(float, float);
float *maxp(float *xp, float *yp)
{
return xp>=yp?xp:yp;
}
In the above example, maxp is a pointer to function accepting two
float values and returning a float value.
Types of declaration of pointers :
int* pi(void) : declares pi to be a function returning a pointer to integer
with no arguments.
int *pi(void) : declares pi to be a pointer returning an integer value with
no arguments.
11. Unions
Unions like structures are the user defined data type and
somewhat similar to structures as they also contain members of
different data types.
Only difference is that – all members in the Union share
the same storage area in the computer’s memory while, each
members in the Structure is assigned its own unique storage
area.
Since the members of the union share the same memory,
only one member can be active at a time. Thus, Unions are
useful as they efficiently use the computer’s memory.
12. Declaring Unions
union union_name
{
data_type member1;
data_type member2;
…………………………
}var1, var2, …;
union book
{
char title[15];
char *author;
int pages;
float price;
} b1, b2, b3;
union book
{
char title[15];
char author[10];
int pages;
float price;
} b1, *bptr;
Or union book *bptr;
Syntax Example Example
Accessing union members :
Example : printf(“%s”, b1.title); // union variable accessing union member.
printf(“%s”, bptr -> title); // union pointer variable accessing union
member.