Chapter 9: Pointers and String

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Programming and Data Structure
Introduction to Programming Lecture 39. Copy Constructor.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Pointers CSE 2451 Rong Shi.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
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.
Midterm Review CS1220 Spring Disclaimer The following questions are representative of those that will appear on the midterm exam. They do not represent.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Pointers A pointer is a variable that contains a memory address as it’s value. The memory address points to the actual data. –A pointer is an indirect.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Parameter Passing: Arrays 1.Create new variables (boxes) for each of the formal parameters allocated on a fresh stack created for this function call. int.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers verses Variables
Stack and Heap Memory Stack resident variables include:
Computer Skills2 for Scientific Colleges
Day 03 Introduction to C.
© 2016 Pearson Education, Ltd. All rights reserved.
Motivation and Overview
Pointers in C.
Day 03 Introduction to C.
Student Book An Introduction
CNG 140 C Programming (Lecture set 10)
Hassan Khosravi / Geoffrey Tien
Programming Languages and Paradigms
Lecture 6 C++ Programming
Programming fundamentals 2 Chapter 2:Pointer
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
14th September IIT Kanpur
CSC 253 Lecture 8.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Using Arrays in C Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning.
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Programming with Pointers
Computer Skills2 for Scientific Colleges
Indirection.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
5.1 Introduction Pointers Powerful, but difficult to master
Homework Starting K&R Chapter 5 Good tutorial on pointers
Pointers and Arrays Beyond Chapter 16
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
C++ Pointers and Strings
C Programming Lecture-8 Pointers and Memory Management
Chapter 9: Pointers and String
CSCE 206 Lab Structured Programming in C
Arrays and Pointers CSE 2031 Fall May 2019.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
More on C++ G Carl Evans.
C++ Pointers and Strings
Programming fundamentals 2 Chapter 3:Pointer
C Pointers Another ref:
SPL – PS2 C++ Memory Handling.
Introduction to Pointers
Dynamic Data Structures
Presentation transcript:

Chapter 9: Pointers and String

Pointer: Definition A pointer is a variable which contains the address in memory of another variable. Through pointers a developer can directly access memory from his/her code which makes memory related operations very fast. But, with great power comes great responsibility.

Why Pointers Pointers make the programs simple and reduce their length. Pointers are helpful in allocation and de-allocation of memory during the execution of the program. Thus, pointers are the instruments of dynamic memory management. Pointers enhance the execution speed of a program. Pointers are helpful in traversing through arrays and character strings. Storage of strings through pointers saves memory space. Pointers may be used to pass on arrays, strings, functions, and variables as arguments of a function. Passing on arrays by pointers saves lot of memory because we are passing on only the address of array instead of all the elements of an array, which would mean passing on copies of all the elements and thus taking lot of memory space. Pointers are used to construct different data structures such as linked lists, queues, stacks, etc.

Declaring Pointer Variable Styntax: datatype *pointer name; Example: int *ptr; char *p;

The address and indirection operator Address Operator: & is address operator. Assigns address to pointer variable Eg: int x =10 , *ptr; ptr=&x; Indirection operator * is indirection operator Eg: int x =10 , *ptr, y; y=*ptr;

Example 1

Pointers as Argument We can pass pointer as argument. As address is directly passed, this can change the variable value. Calling function should pass address Called function should accept address in pointer Eg: swap(&x,&y); ………………. void swap (int *p, int *q) { ………………….}

Example 2

Example 3: Finding Largest and smallest Element in array

Pointer as return value Can be used to return value

Pointer arithmetic C supports only three forms of pointer arithmetic Adding an integer to pointer Subtracting an integer from pointer Subtracting one pointer from another

Passing array through function Example 4

Passing array through function using pointer Example 6