Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

 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.
Lecture 20 Arrays and Strings
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 Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
C programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
C strings (Reek, Ch. 9) 1CS 3090: Safety Critical Programming in C.
CHAPTER 8 CHARACTER AND STRINGS
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)
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
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.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
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. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
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
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
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.
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.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Computer Organization and Design Pointers, Arrays and Strings in C
Strings CSCI 112: Programming in C.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
INC 161 , CPE 100 Computer Programming
ECE Application Programming
Fundamentals of Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
Lecture-5 Arrays.
Module 2 Arrays and strings – example programs.
Strings A string is a sequence of characters treated as a group
Arrays in C.
Chapter 8 - Characters and Strings
CS111 Computer Programming
Lecture 8b: Strings BJ Furman 15OCT2012.
C Stuff CS 2308.
Engr 0012 (04-1) LecNotes
Pointers, Dynamic Data, and Reference Types
Pointers and Pointer-Based Strings
String in C++.
Chapter 16 Pointers and Arrays
Strings Chapter 13 Copyright © 2008 W. W. Norton & Company.
Strings What is a string? It is an array of characters terminated with
CPS120: Introduction to Computer Science
C Strings Prabhat Kumar Padhy
7 Arrays.
C++ Pointers and Strings
Exercise Arrays.
Chapter 9: Pointers and String
C++ Programming Lecture 20 Strings
Lecture 19: Working with strings
CS31 Discussion 1H Fall18: week 6
Characters and Strings
EECE.2160 ECE Application Programming
C++ Pointers and Strings
Introduction to Problem Solving and Programming
Presentation transcript:

Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers

Pointers pointer to constant value constant pointer to value const int * p p can’t change the value of variable it is pointing to constant pointer to value int* const p = &j; p can’t point to anything else, but j.

Characters and Strings A character in ASCII set is represented by integers from 0 – 255. char data type is used to represent that ASCII set of characters char ch; ch = ‘c’; // this is equivalent to ch = 99 because in ASCII table, ‘c’ character’s value is 99. %c, scanf(“ %c”) to force scanf skip any white space before char getchar(), returns an int as read from console char and ints interchangeable (betwee 0 – 255)

Characters and Strings String Literals sequence of characters enclosed in double quotes. C treats them as character arrays When C compiler encounters string literal, it allocates memory to store characters of the string plus one extra character to mark end of string. This special character is called the null character. It is represented as first character in ASCII table, i.e. its value is 0 and it is represented as ‘\0’ as character. The ‘0’ character representing a zero is not same as null character (i.e. the first ASCII character). The ‘0’ character has ASCII value 48. So, always remember the difference between null character (i.e. ‘\0’) and character zero (i.e. ‘0’)

Strings Storing strings char msg[8] = “message”; char msg[8] = “me”; use character arrays size should be alteast one more that the number of characters in the string. the last character should be null. char msg[8] = “message”; msg[] => ‘m’, ‘e’, ‘s’, ‘s’, ‘a’, ‘g’, ‘e’, ‘\0’. char msg[8] = “me”; msg[] => ‘m’, ‘e’, ‘\0’ char msg[] = “message”;

String char *ptr; ptr = “This is a text”; ptr = “this is new text”; Compiler allocates required memory. read-only. ptr = “this is new text”; this is fine. the pointer can be used to point to other string literal. Reading strings gets(str) reads characters including whitespace from console. str should be allocated memory by user so, if str is char str[100], it works, but if str is char *str, it wont’.

string.h library functions for string manipulations strlen() to find the length of the string. It has to be null terminated. size_t strlen(const char* str) strcpy() to copy characters of one string to other char * strcpy(char *dest, const char* src); dest should have enough memory allocated to fit string pointed to by src strncpy() strcmp returns 0 if two string match literally

Functions Variables scope Global variables static variables arrays as arguments void test (int arr[]) void test (int arr[][10]) treated as pointer – so no copy of actual array is made pass length as argument for function to know about the array’s size. functions with variable number of parameters void test (int num, char *str, ….); atleast one fixed parameter is must recursive functions

Pointer to functions return_type (*pointer_name) (argument list…) int (*ptr) (int a); used in callback functions array of pointers to functions void (*ptr[20]) (int a)