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.

Slides:



Advertisements
Similar presentations
Problem Solving & Program Design in C
Advertisements

Introduction to C Programming
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
Lectures 10 & 11.
Programming and Data Structure
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
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.
Kernighan/Ritchie: Kelley/Pohl:
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
C Strings. The char Data Type for Storing Characters The char data type can is used to declare a variable that can hold a single character. Examples:
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Chapter 10.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Introduction to C programming
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
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.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Sahar Mosleh California State University San MarcosPage 1 Character String.
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.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Arrays and Pointers.
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).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Strings, Pointers and Tools
Today’s Material Strings Definition Representation Initialization
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
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 may.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
1-d Arrays.
Computer Organization and Design Pointers, Arrays and Strings in C
Fundamentals of Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
Quiz 11/15/16 – C functions, arrays and strings
Arrays in C.
Pointers and Pointer-Based Strings
Presentation transcript:

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 address (rather than the value) of a data item Type of a pointer variable – pointer to the type of the data whose address it will store Example: int pointer, float pointer,… Can be pointer to any user-defined types also like structure types

Values vs Locations Variables name memory locations, which hold values 32 x 1024: address name value

Contd. Consider the statement int xyz = 50; This statement instructs the compiler to allocate a location for the integer variable xyz, and put the value 50 in that location Suppose that the address location chosen is 1380 xyz  variable 50  value 1380  address

Contd. During execution of the program, the system always associates the name xyz with the address 1380 The value 50 can be accessed by using either the name xyz or the address 1380 Since memory addresses are simply numbers, they can be assigned to some variables which can be stored in memory Such variables that hold memory addresses are called pointers Since a pointer is a variable, its value is also stored in some memory location

Contd. Suppose we assign the address of xyz to a variable p p = &xyz; p is said to point to the variable xyz p = &xyz; *p=xyz (50) Variable Value Address xyz 50 1380 p 1380 2545

Pointers A pointer is just a C variable whose value can contain the address of another variable Needs to be declared before use just like any other variable General form: data_type *pointer_name; Three things are specified in the above declaration: The asterisk (*) tells that the variable pointer_name is a pointer variable pointer_name needs a memory location pointer_name points to a variable of type data_type

Example int *count; float *speed; char *c; Once a pointer variable has been declared, it can be made to point to a variable using an assignment statement like int *p, xyz; : p = &xyz; This is called pointer initialization 7

Strings

Strings 1-d arrays of type char By convention, a string in C is terminated by the end-of-string sentinel ‘\0’ (null character) char s[21] - can have variable length string delimited with \0 Max length of the string that can be stored is 20 as the size must include storage needed for the ‘\0’ String constants : “hello”, “abc” “abc” is a character array of size 4

Character Arrays and Strings char C[8] = { 'a', 'b', 'h', 'i', 'j', 'i', 't', '\0' }; C[0] gets the value 'a', C[1] the value 'b', and so on. The last (7th) location receives the null character ‘\0’ Null-terminated (last character is ‘\0’) character arrays are also called strings Strings can be initialized in an alternative way. The last declaration is equivalent to: char C[8] = "abhijit"; The trailing null character is missing here. C automatically puts it at the end if you define it like this Note also that for individual characters, C uses single quotes, whereas for strings, it uses double quotes

Reading strings: %s format void main() { char name[25]; scanf("%s", name); printf("Name = %s \n", name); } %s reads a string into a character array given the array name or start address. It ends the string with ‘\0’

Note that character strings read in %s format end with ‘\0’ An example void main() { #define SIZE 25 int i, count=0; char name[SIZE]; scanf("%s", name); printf("Name = %s \n", name); for (i=0; name[i]!='\0'; i++) if (name[i] == 'a') count++; printf("Total a's = %d\n", count); } Seen on screen Satyanarayana Name = Satyanarayana Total a's = 6 Typed as input Printed by program Note that character strings read in %s format end with ‘\0’

Differences : array & pointers char *p = “abcde”; The compiler allocates space for p, puts the string constant “abcde” in memory somewhere else, initializes p with the base address of the string constant char s[ ] = “abcde”;  char s[ ] = {‘a’,’b’,’c’,’d’,’e’.’\0’}; The compiler allocates 6 bytes of memory for the array s which are initialized with the 6 characters a b c d e \0 p a b c d e \0 S

String Constant A string constant is treated as a pointer Its value is the base address of the string char *p = “abc”; printf (“%s %s\n”,p,p+1); /* abc bc is printed */ a b c \0 p

Library Functions for String Handling You can write your own C code to do different operations on strings like finding the length of a string, copying one string to another, appending one string to the end of another etc. C library provides standard functions for these that you can call, so no need to write your own code To use them, you must do #include <string.h> At the beginning of your program (after #include <stdio.h>)

String functions we will see strlen : finds the length of a string strcat : concatenates one string at the end of another strcmp : compares two strings lexicographically strcpy : copies one string to another

You cannot change contents strlen() You cannot change contents of s in the function int strlen(const char *s) Takes a null-terminated strings (we routinely refer to the char pointer that points to a null-terminated char array as a string) Returns the length of the string, not counting the null (\0) character int strlen (const char *s) { int n; for (n=0; *s!=‘\0’; ++s) ++n; return n; }

You cannot change contents strcat() You cannot change contents of s2 in the function char *strcat (char *s1, const char *s2); Takes 2 strings as arguments, concatenates them, and puts the result in s1. Returns s1. Programmer must ensure that s1 points to enough space to hold the result. char *strcat(char *s1, const char *s2) { char *p = s1; while (*p != ‘\0’) /* go to end */ ++p; while(*s2 != ‘\0’) *p++ = *s2++; /* copy */ *p = ‘\0’; return s1; }

strcmp() int strcmp (const char *s1, const char *s2); Two strings are passed as arguments. An integer is returned that is less than, equal to, or greater than 0, depending on whether s1 is lexicographically less than, equal to, or greater than s2.

strcmp() int strcmp (const char *s1, const char *s2); Two strings are passed as arguments. An integer is returned that is less than, equal to, or greater than 0, depending on whether s1 is lexicographically less than, equal to, or greater than s2. int strcmp(char *s1, const char *s2) { for (;*s1!=‘\0’&&*s2!=‘\0’; s1++,s2++) if (*s1>*s2) return 1; if (*s2>*s1) return -1; } if (*s1 != ‘\0’) return 1; if (*s2 != ‘\0’) return -1; return 0;

strcpy() char *strcpy (char *s1, char *s2); The characters is the string s2 are copied into s1 until \0 is moved. Whatever exists in s1 is overwritten. It is assumed that s1 has enough space to hold the result. The pointer s1 is returned.

strcpy() char *strcpy (char *s1, const char *s2); The characters is the string s2 are copied into s1 until ‘\0’ is moved. Whatever exists in s1 is overwritten. It is assumed that s1 has enough space to hold the result. The pointer s1 is returned. char * strcpy (char *s1, const char *s2) { char *p = s1; while (*p++ = *s2++) ; return s1; }

Example: Using string functions int main() { char s1[ ] = "beautiful big sky country", s2[ ] = "how now brown cow"; printf("%d\n",strlen (s1)); printf("%d\n",strlen (s2+8)); printf("%d\n", strcmp(s1,s2)); printf("%s\n",s1+10); strcpy(s1+10,s2+8); strcat(s1,"s!"); printf("%s\n", s1); return 0; } Output 25 9 -1 big sky country beautiful brown cows!