Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers. BY SIVAKUMAR.P Pointers OBJECTIVES INTRODUCTION POINTERS DYNAMIC MEMORY ALLOCATION LINKED LISTS APPLICATION.

Similar presentations


Presentation on theme: "Pointers. BY SIVAKUMAR.P Pointers OBJECTIVES INTRODUCTION POINTERS DYNAMIC MEMORY ALLOCATION LINKED LISTS APPLICATION."— Presentation transcript:

1 Pointers

2 BY SIVAKUMAR.P Pointers

3 OBJECTIVES INTRODUCTION POINTERS DYNAMIC MEMORY ALLOCATION LINKED LISTS APPLICATION

4 Introduction Pointer are a fundamental part of C. If you cannot use pointers properly then you have basically lost all the power and flexibility that C allows. The secret to C is in its use of pointers. A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type.

5 WHY POINTERS USE IN C It is the only way to express some computations. It produces compact and efficient code. It provides a very powerful tool. Pointers Explicitly with Arrays, Structures, Functions.

6 Concepts of pointers Pointer constants Pointer values Pointer variables pointers

7 Three concepts Memory addresses within a computer are referred to as pointer constants. We cannot change them.we can only use them to store data values. We cannot save the value of a memory address directly. We can only obtain the value through the variable stored there using the address operator. Once we have a pointer value, it can be stored into another variable. Its called pointer variable.

8 How to pointer as variable To declare a pointer to a variable do: int *pointer; Example: int x = 1, y = 2; int *ip; ip = &x; y = *ip; x = ip; *ip = 3;

9 Arrays of Pointers We can have arrays of pointers since pointers are variables. Example use: Sort lines of text of different length. NOTE: Text can't be moved or compared in a single operation. Arrays of Pointers are a data representation that will cope efficiently and conveniently with variable length text lines.

10 Store lines end-to-end in one big char array. n will delimit lines. Store pointers in a different array where each pointer points to 1st char of each new line. Compare two lines using strcmp() standard library function. If 2 lines are out of order -- swap pointer in pointer array.

11 This eliminates: complicated storage management. high overheads of moving lines.

12 Advantage Pointers are more efficient in handling arrays and data tables. Pointers can be used to return multiple values from a function via function arguments. The use of pointer arrays to character strings results in saving of data storage space in memory. Pointer allow c to support dynamic memory management. Pointers reduce length complexity of programs.

13 Dynamic memory allocation C dynamic memory allocation refers to performing dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely malloc, realloc, calloc and free.

14 Overview of functions FunctionDescription malloc allocates the specified number of bytes. realloc increases or decrease the size of the specified block of memory. calloc allocates the specified number of bytes and initializes them to zero. free releases the specified block of memory back to the system.

15 Common errors Memory leaks. Not checking for allocation failures. Some logical errors.

16 Linked List Linked lists are among the simplest and most common data structures. They can be used to implement several other common abstract data types. A linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a datum and areference (in other words, a link) to the next node in the sequence; more complex variants add additional links. This structure allows for efficient insertion or removal of elements from any position in the sequence.data structurenodesreference

17 Basic concepts Each record of a linked list is often called an element or node.node The field of each node that contains the address of the next node is usually called the next link or next pointer. The remaining fields are known as the data, information, value, cargo, orpayload fields. The head of a list is its first node. The tail of a list may refer either to the rest of the list after the head, or to the last node in the list. In Lisp and some derived languages, the next node may be called the cdr (pronounced could-er) of the list, while the payload of the head node may be called the car.Lispcdrcar

18 Types of linked list 1) Linear and circular 2)Single 3)Double 4)Multiple

19 Linear and circular lists Linked List Circular Linked List In the last node of a list, the link field often contains a null reference, a special value used to indicate the lack of further nodes. A less common convention is to make it point to the first node of the list; in that case the list is said to be circular or circularly linked; otherwise it is said to be open or linear.

20 Singly Linked List Singly linked lists contain nodes which have a data field as well as a next field, which points to the next node in the linked list. A singly linked list whose nodes contain two fields: an integer value and a link to the next node

21 Doubly Linked List In a doubly linked list, each node contains, besides the next-node link, a second link field pointing to the previous node in the sequence. The two links may be called forward(s) andbackwards, or next and prev(ious).doubly linked list A doubly linked list whose nodes contain three fields: an integer value, the link forward to the next node, and the link backward to the previous node

22 Multiple linked list In a multiply linked list, each node contains two or more link fields, each field being used to connect the same set of data records in a different order. Example: ( by name, by department, by date of birth, etc….)

23 Application pointers: 1. Easy access 2.To return more than one value from a function. 3. To pass as arguments to functions. struct student { char name[10]; int rollno; }; If you pass this structure object as argument to function then, 14 bytes(10+4) of memory will be passed to the function. Instead, if you pass the pointer to the structure as argument then only 4 bytes (or 8 bytes)of memory will be passed to the function.

24 application The Applications of Linked Lists are; 1. In Dynamic Memory Management. 2.In Symbol Tables. 3. Representing Sparse Matrix 4m Sai For representing Polynomials. It means in addition/subtraction /multipication.. of two polynomials. Eg:p1=2x^2+3x+7 and p2=3x^3+5x+2 p1+p2=3x^3+2x^2+8x+9

25 THANK YOU


Download ppt "Pointers. BY SIVAKUMAR.P Pointers OBJECTIVES INTRODUCTION POINTERS DYNAMIC MEMORY ALLOCATION LINKED LISTS APPLICATION."

Similar presentations


Ads by Google