pointers. by sivakumar.p pointers objectives introduction pointers dynamic memory allocation linked...

25
Pointers

Upload: eliseo-forrest

Post on 28-Mar-2015

280 views

Category:

Documents


1 download

TRANSCRIPT

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

Pointers

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

BYSIVAKUMAR.P

Pointers

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

OBJECTIVES

• INTRODUCTION• POINTERS• DYNAMIC MEMORY ALLOCATION• LINKED LISTS• APPLICATION

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

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.”

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

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.

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

Concepts of pointers

Pointer constants

Pointer values

Pointer variables

pointers

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

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.

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

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;

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

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.

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

• 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 .

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

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

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

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.

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

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.

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

Overview of functions

Function Description

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.

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

Common errors

• Memory leaks.• Not checking for allocation failures.• Some logical errors.

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

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.

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

Basic concepts• Each record of a linked list is often called an element or 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.

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

Types of linked list

1) Linear and circular

2)Single

3)Double

4)Multiple

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

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.

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

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

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

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).

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

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

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….)

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

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.

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

application

The Applications of Linked Lists are;

1. In Dynamic Memory Management.2.In Symbol Tables.3. Representing Sparse Matrix4m Sai....

4. 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