Chapter 9: Pointers and String

Slides:



Advertisements
Similar presentations
Problem Solving & Program Design in C
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Programming and Data Structure
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
C programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Chapter 10.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
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.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
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.
1 This chapter covers both string constants (or literals, as they're called in the C standard) and string variables, which can change during the execution.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
Pointers and Arrays Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Computer Organization and Design Pointers, Arrays and Strings in C
(Numerical Arrays of Multiple Dimensions)
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
A First Book of ANSI C Fourth Edition
CNG 140 C Programming (Lecture set 10)
Module 2 Arrays and strings – example programs.
Arrays in C.
CSCE 210 Data Structures and Algorithms
Programming Languages and Paradigms
8 Pointers.
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
14th September IIT Kanpur
CS111 Computer Programming
Object Oriented Programming COP3330 / CGS5409
Lecture 8b: Strings BJ Furman 15OCT2012.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Week 9 – Lesson 1 Arrays – Character Strings
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
C Characters and Strings – Review Lab assignments
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter: 7-12 Final exam review.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Strings Chapter 13 Copyright © 2008 W. W. Norton & Company.
C++ Pointers and Strings
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
C Programming Lecture-8 Pointers and Memory Management
Exercise Arrays.
Chapter 9: Pointers and String
Standard Version of Starting Out with C++, 4th Edition
Programming Languages and Paradigms
Arrays and Pointers CSE 2031 Fall May 2019.
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
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 as Pointer in function Example 5 and lecture

String Literals String literal is sequence of characters enclosed within double quotes. “ hara hara Mahadeva”

Escape Sequence in String Literals “ Candy\nIs dandy\But liquor\nIs quicker Result Candy Is dandy But liquor Is quicker

How Strings Literals are stored Stored as character of array with null character C use \0 as escape sequence to store numm character.

String Variables Use array with datatype char Initializing a string variable Eg: char x[]=“ILOVEYOU”

Reading and writing strings printf() or puts() function are used %s is conversion specifier Example 7: char str[]=“Let there be peace”; printf(“%s\n”, str); printf(“%.4s”,str);

Reading String scanf or gets Use conversion specifier %s with printf()

Some string Manipulating Function strcmp() strrev() strupr() strlwr() And many more

Some examples Write C code for following problem by using and not using standard library function Check whether two string are same or not using Copy one string to another Count vowel and consonant in string Display different pattern using loop Delete all vowel in string Change to lower case and uppercase