You are on page 1of 5

UNIT-1 BASIC CONCEPT POINTERS If we write:

DATA STRUCTURE THRU C

n=&m; Then the address of variable m is stored in n. When a variable stores an address we declare that variable as a pointer data type. Thus n is declared as: int *n; This declaration says that n will store the address of an integer variable name. Declaring and Initializing: Method 1: int *p; Method 2: int* p; Method 3: int * p; Pointer can be declared in these three methods. Let us follow the first method which is widely adopted. Since pointer variables contain addresses that belong to a separate data type, they must be declared as pointers before we use them. The declaration of a pointer variable has the following form: Data type *pt_name; This tells the compiler three things about the variable pt_name. The asterisk(*) tells that the variable pt_name is a pointer variable. pt_name needs a memory location. pt_name points to variable of type data type.

Accessing a variable through its pointer: Once a pointer has been assigned the address of a variable, the question remains as to how to access the value of the variable using the pointer. This is done by using another unary operator *(asterisk), usually known as the indirection operator. Consider the following statements: int qty, *q,m; qty=165; q=&qty; m=*p; The first line declares qty and m as integer variables and q as a pointer variable pointing an integer. The second line assigns the value 165 to qty and the third line assigns the address of qty to the pointer variable q. The fourth line contains the indirection operator * . When the operator * is placed before a pointer variable in an expression , the pointer will return the value of the variable. The * can be called as value at address . Thus the value of q would be 165. The two statements q=&qty; m=*p; are equivalent to m=qty.
DYNAMIC MEMORY ALLOCATION

Usually when we use an array in c program, its dimension should be more than enough or may not be sufficient. To avoid this drawback we allocate the proper (sufficient) dimensions of an array during the run time of the program with the help of the library functions called memory management functions like malloc, calloc, realloc etc. The process of allocating memory at run time is known as dynamic memory allocation. Example; to assign sufficient memory for x we use the following statement x= (int *) malloc (n* sizeof (int) ) for in the place of initial declaration int x[n]. Similarly in the place of float y [100] we use

by Sharad,MCA

DATA STRUCTURE

UNIT-1 BASIC CONCEPT

DATA STRUCTURE THRU C

y = (float *) malloc (m* sizeof (float) ); ANSI C provides five standard functions that will help you allocate memory on the heap which are as follows: sizeof() malloc() calloc() realloc() free()

The following table describe the five different standard functions that helps you allocate memory dynamically; Function sizeof() malloc calloc realloc free Task The sizeof() function returns the memory size of the requested variable Allocates memory requests size of bytes and returns a pointer to the Ist byte of allocated space Allocates space for an array of elements initializes them to zero and returns a pointer to the memory Modifies the size of previously allocated space. Frees previously allocated space

sizeof(): The sizeof() function returns the memory size of requested variable. This call should be used in the conjunction with the calloc() function call, so that only the necessary memory is allocated, rather than a fixed size. Consider the following, Struct date { int hour, minute, second; }; int x; x = sizeof( struct date ); malloc(): A block mf memory may be allocated using the function called malloc. The malloc function reserves a block of memory of specified size and return a pointer of type void. This means that we can assign it to any type of the pointer. It takes the following form: ptr=(cast-type*)malloc(byte-size); ptr is a pointer of type cast-type the malloc returns a pointer (of cast type) to an area of memory with the size byte-size. The following is the example of using malloc function x=(int*)malloc(100*sizeof(int)); Example:
#include <stdlib.h> #include <stdio.h> #include <conio.h> int main() { int* array; int n, i; printf("Enter the number of elements: "); scanf("%d", &n);

by Sharad,MCA

DATA STRUCTURE

UNIT-1 BASIC CONCEPT array = (int*) malloc(n*sizeof(int)); for (i=0; i<n; i++) { printf("Enter number %d: ", i); scanf("%d", &array[i]); } printf("\nThe Dynamic Array is: \n"); for (i=0; i<n; i++) { printf("The value of %d is %d\n", i, array[i]); } printf("Size= %d\n", i); getch(); return 0; }

DATA STRUCTURE THRU C

calloc(): Calloc is another memory allocation function that is normally used to request the multiple blocks of storage each of same size and then sets all bytes to zero. The general form of calloc is: ptr=(cast-type*) calloc(n,elem-size); The above statement allocates contiguous space for n blocks each size of the elements size bytes. All bytes are initialized to zero and a pointer to the first byte of allocated region is returned. If there is not enough space a null pointer is also returned. Example: int main () { int number,i; printf("Enter the number "); scanf("%d",&number); printf("Number which is here are",number); int *ptr = (int *) calloc (number,sizeof(int)); for (i=0; i<number;i++) { ptr[i] = i +i; } for (i=0; i<number;i++) { printf("Result is %d %d\n",i,ptr[i]); } }

realloc(): The memory allocated by using calloc or malloc might be insufficient or excess sometimes in both the situations we can change the memory size already allocated with the help of the function called realloc. This process is called the reallocation of memory. The general statement of reallocation of memory is : ptr=realloc(ptr,newsize); free(): Compile time storage of a variable is allocated and released by the system in accordance with its storage class. With the dynamic runtime allocation, it is our responsibility to release the space when it is not required at all.When the storage is limited,the release of storage space

by Sharad,MCA

DATA STRUCTURE

UNIT-1 BASIC CONCEPT

DATA STRUCTURE THRU C

becomes important . When we no longer need the data we stored in a block of memory and we do not intend to use that block for the storing any other information, Using the free function,we may release that block of memory for future use. free(ptr); ptr is a pointer that has been created by using calloc or malloc.
Example: int main () { int number,i; printf("Enter the number "); scanf("%d",&number); printf("Number which is here are",number); for (i=0; i<number;i++) { ptr[i] = i +i; } for (i=0; i<number;i++) { printf("Result is %d %d\n",i,ptr[i]); } free(ptr); }

ALGORITHM SPECIFICATION Algorithm: Definition: An algorithm is a finite set of instructions which, if followed, accomplish a particular task. In addition every algorithm must satisfy the following criteria: Input: there are zero or more quantities which are externally supplied. Output: at least one quantity is produced. Definiteness: each instruction must be clear and unambiguous. Finiteness: if we trace out the instructions of an algorithm, then for all cases the algorithm will terminate after a finite number of steps. DATA ABSTRACTION (ABSTRACTION DATA TYPE) The ADT consists of a set of definitions that allow programmers to use the functions while hiding the implementation. This generalization of operations with unspecified implementations is known as abstraction. An ADT is a data declaration packaged together with the operations that are meaningful on the data type. Declaration of Data. Declaration of Operation. Uses of ADT: It helps to efficiently develop well designed program. Facilitates the decomposition of the complex task of developing a software system into a number of simpler subtasks. Helps to reduce the number of things the programmer has to keep in mind at any time . Breaking down a complex task into a number of earlier subtasks also simplifies testing and debugging. Example: ADT Natural number Objects: An ordered set of numbers starting from 0 to maximum integer INT_MAX.

by Sharad,MCA

DATA STRUCTURE

UNIT-1 BASIC CONCEPT

DATA STRUCTURE THRU C

Functions/Operation: For all x, y natural numbers, TRUE, FALSE Boolean And +, - , *, <, == are the usual integer operation. Boolean iszero(x) Boolean isequal(x, y) Natural_number successor(x) Etc PERFORMANCE ANALYSIS Performance analysis is also called as Priori. It involves manual calculations. The values obtained are machine independent. Analysis involveso Time complexity: Approximate time required to execute the instructions. o Space complexity: Storage required to execute the instructions.

PERFORMANCE MEASUREMENT Performance measurement is also called as posteriori. This involves actual measurement of time and space. The values obtained are machine dependent. Measurement involveso Run Time: Total time taken from the beginning of execution till the end. o Memory: Total memory occupied in the system.

by Sharad,MCA

DATA STRUCTURE

You might also like