Pointers.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Introduction to C Programming
Stacks, Queues, and Linked Lists
Linked Lists.
Linked Lists: deleting...
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
CHP-5 LinkedList.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Data Structures Using C++ 2E
Programming and Data Structure
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
Kernighan/Ritchie: Kelley/Pohl:
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Pointers Applications
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Stack and Heap Memory Stack resident variables include:
Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
C LANGUAGE Characteristics of C · Small size
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Characters and Strings
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Data Structure & Algorithms
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Computer Organization and Design Pointers, Arrays and Strings in C
Linked List ADT used to store information in a list
Lecture 6 of Computer Science II
Data Structure By Amee Trivedi.
Linked List :: Basic Concepts
Lectures linked lists Chapter 6 of textbook
Data structures and algorithms
CSCE 210 Data Structures and Algorithms
LINKED LISTS CSCD Linked Lists.
Lists.
Circular Buffers, Linked Lists
C Programming Lecture-8 Pointers and Memory Management
Data Structures & Algorithms
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Presentation transcript:

Pointers

Pointers BY SIVAKUMAR.P

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

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.

Concepts of pointers pointers Pointer constants Pointer values Pointer variables pointers

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.

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;

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. 

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 .

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

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.

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.

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.

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

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.

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.

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

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.

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

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

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

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. 

application The Applications of Linked Lists are; 1. In Dynamic Memory Management. 2.In Symbol Tables. 3. Representing Sparse Matrix 4m 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

THANK YOU