UNIT 4 Pointers Userdefineddatatypes R23
UNIT 4 Pointers Userdefineddatatypes R23
UNIT – 4
POINTERS & USER DEFINED DATA TYPES
POINTERS
INTRODUCTION
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 address
as their values.
DECLARATION OF POINTERS
The pointer is declared just like ordinary variables. They have to be
defined before they are used. The general form of a pointer declaration is as
follows:
data-type *Pointer_ Name
Here the “*” tells that variable Pointer _Name is pointer type variable.
i.e. it holds the address of another variable specified by the data-type.
Pointer_ Name needs a memory location. Pointer_ Name points to a variable
of type data_type.
20
Value at Location
Example:-
x=10;
p = &x;
Here let x is the variable whose value is 10 and suppose if its address
is 1000. Then address of x is 1000 which is returned to p.
Example: -
int x, n, *p;
x = 10;
p = &x;
n = *p
/*PROGRAM TO PRINT ADDRESS AND THE VALUE OF A VARIABLE BY
USING ‘&’ AND ‘*’ OPERATORS */
#include< stdio.h>
main ()
{
int n=20;
printf (“address of n is: %u \n”, &n);
printf (“value of n is: %d \n”, n);
printf (“value of n is: %d”,*(&n));
}
If they are not initialized then they are left uninitialized as follows:
POINTERS TO POINTERS
It is possible to make a pointer variable to point to another pointer
variable. Thus we can create a chain of pointers.
p2 p1 variable
address1 address2 value
Here if the base address is 1000 for “a” and integer occupies 2 bytes
then the five elements requires 10 bytes as shown below.
Element a[0] a[1] a[3] a[4] a[5]
Value 1 2 3 4 5
Other way round there always exists a possibility that when you run
the program you need to store more than 100 students’ marks, in this case
the array would fall short in size. Moreover there is no way to increase or
decrease the array size during execution, this is done by dynamic data
structures along with dynamic memory management techniques.
Local Variables
Stack
Free Memory Heap
Global Variables
Permanent Storage Area
C Program Instructions
STORAGE OF A C PROGRAM
The program instructions and the global variables as well as the static
variables are stored in the region known as permanent storage area. The
local variables are stored in the in the area called stack. The memory space
between these two is the free area called heap.
The allocation or de-allocation of the space takes place from the heap.
Therefore the size of the heap increases or decreases depending on the
allocation and de-allocation of variables. If there is no space for allocation
then the functions return NULL.
malloc()
This function is used to allocate one block of storage. It reserves the
size specified by the pointer and returns void. The syntax for malloc is as
follows
ptr = (cast-type *) malloc (byte-size);
or
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
10
INTRODUCTION TO PROGRAMMING
Example: -
x = (int *) malloc (100 * sizeof(int));
It is also used to allocate storage for complex data such as structures.
calloc()
This function is used to allocate multiple blocks of memory. It is used
to store run time data types such as arrays and structures. It allocates
storage for multiple blocks and initializes them all to zero and the pointer
will point to the first byte of the first block. Here all blocks are same size.
The syntax is as follows:
ptr = (cast-type *) calloc (n, element-size);
or
void * calloc(size_t element_count, size_t element_size);
Example: -
x = (int *) calloc(n, sizeof(int));
free()
Compile time storage is allocated when the variable is declared with
storage class. But with dynamic storage we need to allocate storage
depending on the free storage because storage is limited. Whenever we are
not in need of the variable for which the storage is allocated then such
variables storage is removed. It is done by using free function. The syntax is
as follows:
free (ptr);
or
void free(void *ptr);
realloc()
Reallocation is done when
1. Storage is allocated to a variable is not sufficient so to modify the
variable’s storage.
2. If the storage is allocated more than the required storage then also we
need to alter the storage.
DECLARATION OF STRUCTURE
There are two ways of declaring a structure. They are listed as follows:
Tagged structure
Type declaration structure
1. Tagged Structure
A tagged structure is used to define variables, parameters and return
types.
The format is as follows
struct tag_name
{
data type member 1;
data type member 2;
data type member 3;
.
.
.
data type member n;
};
Example: -
struct book_bank
{
char title[25];
char author[20];
int pages;
float price;
};
Example: -
typedef struct
{
char title[25];
char author[20];
int pages;
float price;
}book_bank;
Example: -
struct book_bank book1, book2, book3;
Here book1, book2, book3 are the structure variables of type struct
book_bank. Each of these variables can have the four members of the
structure book_bank. Then it is
struct book_bank
{
char title[25];
char author[20];
int pages;
float price;
};
Struct book_bank book1, book2, book3;
or
struct book_bank
{
char title[25];
char author[20];
int pages;
float price;
} book1, book2, book3;
Example: -
book2.price is the variable representing the price of book2 and can be
treated like any other ordinary variables.
STRUCTURE INITIALIZATION
Like other data type variables we can also initialize a structure
variable. However a structure must be declared as static.
int main(void)
{
static struct
{
int age;
float height;
} student = {20, 180.75};
………………
………………
}
This assigns the value 20 to student.age and 180.75 to
student.height. Suppose you want to initialize more than one structure
variable:
int main(void)
{
struct st_record
{
int age;
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
16
INTRODUCTION TO PROGRAMMING
float height;
};
static struct st_record student1={20,180.75};
static struct st_record student2={22,177.25};
…………………..
}
clrscr();
printf(“ Enter a person details:\n\n”);
printf(“ Enter person name : “);
scanf(“%s”,person.name);
printf(“Enter a person joining day : “);
scanf(“%d”,&person.day);
printf(“Enter a person joining month: “); scanf(“%d”,&person.month);
printf(“Enter a person joining year: “); scanf(“%d”,&person.year);
printf(“Enter a person salary: “);
scanf(“%d”,&person.salary);
printf(“\n\n persons name is : %s\n”,person.name);
printf(“\n\n persons joining day is : %s\n”,person.day);
printf(“\n\n persons joining month is : % s \ n ” , person. month );
printf(“\n\n persons joining year is : %s\n”,person.year);
printf(“\n\n persons salary is : %s\n”,person.salary);
return 0;
}
ARRAYS OF STRUCTURES
We may declare an array as structures, where each element of the
array representing a structure variable. For example:
struct class student[100];
student[1].sub3 79
student[2].sub1 34
student[2].sub2 23
student[2].sub3 14
{
printf(“ stu[%d]: %d\n”,i+1,student[i].total);
printf(“ SUBJECT TOTAL\n\n”);
printf(“sub1:%d\nsub2:%d\nsub3:%d\n”,total.sub1,
total.sub2, total.sub3);
printf(“\n Grand total : %d\n”,total.total);
}
return 0;
}
POINTERS TO STRUCTURES
The way we can have a pointer pointing to an int, or a pointer pointing
to a char, similarly we can have a pointer pointing to the struct. Such
pointers are known as ‘structure pointers’.
/*EXAMPLE PROGRAM ON STRUCTURE POINTERS*/
#include <stdio.h>
int main(void)
{
struct book
{
char title[25];
char author[25];
int no;
};
struct book b={“SHREETECH C Notes”,”srinivas”,102};
struct book *ptr;
ptr=&b;
printf(“%s %s %d\n”, b.title, b.author, b.no);
printf(“%s %s %d\n”, ptr->title, ptr->author, ptr->no);
return 0;
}
The first printf() is as usual. The second printf() however is peculiar.
We cannot use ptr.title, ptr.author and ptr.no because ptr is not a structure
UNIONS
Unions are a concept borrowed from structures and therefore follow
the same syntax as structures. The major difference between them is in
terms of storage. In structures each member has its own storage location,
whereas all the members of a union use the same location. It can handle
only one member at a time.
Declaration of Union
General Format
union name
{
type var1;
type var2;
.
.
.
};
Ex:
union item
{
int m;
float x;
char c;
}code;
This declares a variable code of type union item. The union contains
three members each with a different data type. However we can use only one
of them at a time. This is due to the fact that only one location is allocated
for a union variable, irrespective of its size.
c
The compiler allocates a piece of storage that is large enough to hold
the largest variable type in the union. In the declaration above the member x
requires 4 bytes which is the largest among the members. The above figure
shows how all the three variables share the same address.
ACCESSING UNION
To access a union member we can use the same syntax that we used
for the structure members. The operator is member operator indicate by “. “
Ex:
code.m;
code.x;
INITIALIZATION OF UNIONS
The union is initialized such that only the first type declared in the
union when the variable is declared
The other types can be initialized by assigning values by reading
When intializing, we must enclose the values with a set of braces even
if there is one value
The members of the structure are The members of the union are
accessed by using dot operators accessed by using member
as s1.rollno, s2. Per operators as s1.rollno, s2. per