I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

Similar documents
Expressions and Precedence. Last updated 12/10/18

Computers Programming Course 6. Iulian Năstac

A flow chart is a graphical or symbolic representation of a process.

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

OBJECT ORIENTED PROGRAMMING

Procedural Programming & Fundamentals of Programming

C-LANGUAGE CURRICULAM

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Computers Programming Course 11. Iulian Năstac

Information Science 1

ONE DIMENSIONAL ARRAYS

Array Initialization

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

UNIT- 3 Introduction to C++

Pointers. Pointer References

Fundamental of Programming (C)

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

Computers Programming Course 10. Iulian Năstac

Multidimension array, array of strings

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Intermediate Programming, Spring 2017*

Week 5 9 April 2018 NWEN 241 More on pointers. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

8. Characters, Strings and Files

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

[0569] p 0318 garbage

Informatics Ingeniería en Electrónica y Automática Industrial

Arrays and Pointers. CSE 2031 Fall November 11, 2013

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Character Strings. String-copy Example

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Operators. Java operators are classified into three categories:

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

SBE201 Data Structures and Algorithms in C

Q 1. Attempt any TEN of the following:

by Pearson Education, Inc. All Rights Reserved.

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

POINTER & REFERENCE VARIABLES

Procedural Programming & Fundamentals of Programming

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Part I Part 1 Expressions

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

Computers Programming Course 5. Iulian Năstac

Chapter 21: Introduction to C Programming Language

Arrays, Strings, & Pointers

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

CSI33 Data Structures

C Programming Multiple. Choice

Multiple Choice Questions ( 1 mark)

Arrays and Pointers in C. Alan L. Cox

QUIZ: What value is stored in a after this

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

CSE2301. Arrays. Arrays. Arrays and Pointers

Review of the C Programming Language for Principles of Operating Systems

Important Questions for Viva CPU

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Pointers. Pointers. Pointers (cont) CS 217

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Operators in C. Staff Incharge: S.Sasirekha

Chapter 8 Character Arrays and Strings

1 Pointer Concepts. 1.1 Pointer Examples

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Arrays and Pointers (part 1)

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Programming. Elementary Concepts

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Arrays and Pointers. Overview. Arrays Introducing Pointers C-Style Character Strings Multidimensioned Arrays

JAVA OPERATORS GENERAL

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

Introduction to Computer Science Midterm 3 Fall, Points

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

Contents. Preface. Introduction. Introduction to C Programming

C Programming Language Review and Dissection III

ARRAYS(II Unit Part II)

Transcription:

Lebanese University Faculty of Science Computer Science BS Degree I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

2Computer Science BS Degree -Imperative Programming (I2204) 2018-2019 Pointers and Arrays Chapter 2

Chapter at a glance Correct understanding and use of pointers is critical pointers provide the means by which functions can modify their calling arguments pointers support dynamic allocation Pointers are one of the strongest but also one of the most dangerous features in C/C++ 3Computer Science BS Degree -Imperative Programming (I2204) 2018-2019

Chapter at a Glance pointers are challenging need to know when to use a pointer when to dereference the pointer when to pass an address to a variable rather than the variable itself when to use pointer arithmetic to change the pointer how to use pointers without making your programs unreadable arrays in C are interesting because they are pointed to the variable that you declare for the array is actually a pointer to the first array element intriguing features of pointers pointer arithmetic is used for stepping through arrays rather than using array indices 4Computer Science BS Degree -Imperative Programming (I2204) 2018-2019

Chapter Outline Definition Pointer variables declaration Pointer operators (* and &) Pointer assignments Pointer arithmetic Pointer comparisons The NULL pointer 5Computer Science BS Degree -Imperative Programming (I2204) 2018-2019

Chapter Outline Arrays Pointers and arrays Iterating an array using pointers Passing arrays Strings Arrays of char vs. Char pointers Long strings Multidimensional arrays Arrays of pointers Arrays of strings Multiple indirection (pointers to pointers ) Void pointers 6Computer Science BS Degree -Imperative Programming (I2204) 2018-2019

What Are Pointers? a pointer is a variable that holds a memory address this address is the location of another object (typically another variable) in memory if one variable contains the address of another variable "the first variable points to the second" p x 7Computer Science BS Degree -Imperative Programming (I2204) 2018-2019

Pointer Variables a pointer declaration consists of a base type, an asterix *, and the variable name type *name; int * p; char* q; technically any type of pointer can point anywhere in memory however all pointer arithmetic is done relative to its base type so it is important to declare the pointer correctly 8Computer Science BS Degree -Imperative Programming (I2204) 2018-2019

9Computer Science BS Degree -Imperative Programming (I2204) 2018-2019 The Pointer Operators 2 special pointer operators: addressing or referencing operator & dereference operator *

Addressing or Referencing Operator given a variable c of type T & is a unary operator that returns the memory address of its operand m = &c; m receives the address of c m references c m points to c m is a pointer and c is its pointee c m 3 10

Dereference Operator * applied to a pointer m of base type T, the unary operator * gives the value of the object of type T pointed by m * is the complement of & unary operator that returns the value located at the address that follows q = *m; dereference m and place its value in q retrieve m's pointee value (3) and put it in q c m q 3 3 11

Exercise A: Draw the memory state 12

Exercise B: Draw the memory state 13

Exercise B: Draw the memory state 14

Pointer Assignments = 3; both p1 and p2 now point to x address of x is displayed by using the %p printf() format specifier display an address in the format used by the host computer 15

Pointer Assignments = 3; x p1 p2 both p1 and p2 now point to x address of x is displayed by using the %p printf() format specifier display an address in the format used by the host computer 3 16

Pointer Arithmetic only two arithmetic: addition (+) subtraction (-) example let p1 be an integer pointer with a current value of 2000 assume integers are 2 bytes long p1++; // p1 contains 2002, not 2001 each time p1 is incremented, it will point to the next integer (base type) the same is true of decrements 17

Pointer Arithmetic each time a pointer is incremented, it points to the memory location of the next element of its base type each time it is decremented, it points to the location of the previous element 18

Pointer Arithmetic may add or subtract integers to or from pointers p1 = p1 + 12; may subtract one pointer from another in order to find the number of objects of their base type that separate the two all other arithmetic operations are prohibited may not multiply or divide pointers may not add two pointers may not apply the bitwise operators to them may not add or subtract type float or double to or from pointers 19

Pointer Comparisons can compare two pointers in a relational expression if(p<q) printf("p points to lower memory than q\n"); used when 2+ pointers point to a common object, such as an array 20

Exercise 1 draw the memory status at each line marked by * void PointerTest() { } int a = 1; int b = 2; int c = 3; int* p; int* q; //*1 p = &a; //*2 q = &b; //*3 c = *p; //*4 p = q; //*5 *p = 13; //*6 printf("%d", *q); 21

The NULL Pointer the constant NULL is a special pointer value which encodes the idea of "points to nothing" it is a runtime error to dereference a NULL pointer NULL is usually drawn as a diagonal line between the corners of the pointer variable's box... 22

Bad Pointer Example void BadPointer() { int* p; // allocate the pointer, but not the pointee *p = 42; // this dereference is a serious runtime error } What happens at runtime when the bad pointer is dereferenced? 23

Bad Pointer Example 24

Bad Pointer Example the bad code will compile fine, but at run-time, each dereference with a bad pointer will corrupt memory in some way the program will crash sooner or later. it is up to the programmer to ensure that each pointer is assigned a pointee before it is used 25

Exercise 2 addr1 and addr2 are pointers pointing to reals write a program that displays the values of addr1, addr2 and their contents 26

p q Exercise 3 write a program which exchanges the pointees of two pointers of type char before after x x e p e y j q j y 27

Exercise 4 write a function that exchanges the values of two variables of type int write a program that calls this function (call example) 28

Recall for Arrays declare an array using [ ] following variable name int x[5]; must include size of array in the [ ] when declaring unless you are also initializing int x [ ] = {1, 2, 3, 4, 5}; can also include size >= number of items being initialized remaining array elements are uninitialized can access array elements as in x[4] array indices start at 0 arrays can be passed as parameters, the type being received would be denoted as int x[ ] 29

Pointers and Arrays pointers and arrays close relationship char str[80], *p; p = str; a variable declared as an array of some type acts as a pointer to that type when used by itself it points to first element of array p[i] *(p+i) &p[i] p+i 30

Pointers and Arrays a pointer can be indexed like an array name p has been set to the address of 1 st element in str access 5 th element in str str[4] *(p+4) p[4] *(str+4) difference an array variable cannot change its value str++; //wrong str=&c; //wrong An array variable is a constant pointer p[i] *(p+i) &p[i] p+i 31

Iterating Through the Array way 1: usual way int j; for(j = 0; j < n; j++) a[j]++; 2 ways to iterate through an array way 2: method using pointer arithmetic int *pj; for(pj = a; pj < a + n; pj++) (*pj)++; 32

Iterating Through the Array NOTE (*pj)++; increments what pj points to, *(pj++); increments the pointer to point at the next array element what do each of these do? *pj++; ++*pj; *++pj; 33

Precede Operator Description Associativity nce ++ -- Suffix/postfix increment and decrement Left-to-right () Function call [] Array subscripting. Structure and union member access 1 Structure and union member access through > pointer (type){list Compound literal } ++ -- Prefix increment and decrement Right-to-left + Unary plus and minus! ~ Logical NOT and bitwise NOT (type) Type cast 2 * Indirection (dereference) & Address-of sizeof Size-of _Alignof Alignment requirement 3 * / % Multiplication, division, and remainder Left-to-right 4 + Addition and subtraction 5 << >> Bitwise left shift and right shift 6 7 ==!= For relational = and respectively 8 & Bitwise AND 9 ^ Bitwise XOR (exclusive or) 10 Bitwise OR (inclusive or) 11 && Logical AND 12 Logical OR 13?: Ternary conditional Right-to-Left = Simple assignment += = Assignment by sum and difference Assignment by product, quotient, and 14 *= /= %= remainder <<= >>= Assignment by bitwise left shift and right shift &= ^= = Assignment by bitwise AND, XOR, and OR 15, Comma Left-to-right < <= For relational operators < and respectively > >= For relational operators > and respectively 34

Example: putstr() function writes a string to the standard output device, one character at a time void putstr(char s[]) { } /* index s as an array */ for(int t=0; s[t]; ++t) putchar(s[t]); another way to write the same thing void putstr(char *s){ } /* access s as a pointer */ while(*s) putchar(*s++); 35

Array Example Using a Pointer int x[4] = {12, 20, 39, 43}, *y; y = &x[0]; printf("%d\n", x[0]); printf("%d\n", *y); printf("%d\n", *y+1); printf("%d\n", (*y)+1); printf("%d\n", *(y+1)); y+=2; printf("%d\n", *y); *y = 38; printf("%d\n", *y-1); printf("%d\n", *y++;) printf("%d\n", *y); (*y)++; printf("%d\n", *y); 36

Passing Arrays when declaring parameters to functions declaring an array variable without a size is equivalent to declaring a pointer what is being passed is a pointer to the array in the formal parameter list, you can either specify the parameter as an array or a pointer often this is done to emphasize the fact that the pointer variable will be used in a manner equivalent to an array int array[100]; afunction(array); void afunction(int a[ ]) { } is exactly equivalent to void afunction(int *a) { } 37

Strings no string type in C implement strings as arrays of chars with the last byte in the string being a zero or the null character '\0' char str[10]; // str is an array of 10 chars or a string or char *str; // str points to the beginning of a string of unspecified length string.h library with numerous string functions they all operate on arrays of chars and include: 38

string.h strcpy(s1, s2) copies s2 into s1 (including \0 as last char) strncpy(s1, s2, n) same but only copies up to n chars of s2 strcmp(s1, s2) returns a negative int if s1 < s2, 0 if s1 = = s2 and a positive int if s1 > s2 strncmp(s1, s2, n) same but only compares up to n chars strcat(s1, s2) concatenates s2 onto s1 (this changes s1, but not s2) strncat(s1, s2, n) same but only concatenates up to n chars strlen(s1) returns the integer length of s1 strchr(s1, ch) return a pointer to the first occurrence of ch in s1 (or NULL if ch is not present) strrchr(s1, ch) same but the pointer points to the last occurrence of ch strpbrk(s1, s2) return a pointer to the first occurrence of any character in s1 that matches a character in s2 (or NULL if none are present) strstr(s1, s2) substring, return a pointer to the char in s1 that starts a substring that matches s2, or NULL if the substring is not present 39

Implementing some of string.h int strcmp(char *s, char *t) { int i; for(i=0;s[i] = = t[i];i++) if(s[i] = = \0 ) return 0; return s[i] t[i]; } int strcmp(char *s, char *t) { for( ; *s = = *t; s++, t++) if(*s = = \0 ) return 0; return *s - *t; } The conciseness of the last strcmp and strcpy make them hard to understand int strlen(char *s) { int n; for(n = 0; *s!= \0 ; s++) n++; return n; } void strcpy(char *s, char *t) { int i = 0; while((s[i] = t[i])!= \0 ) i++; } void strcpy(char *s, char *t) { while((*s = *t)!= \0 ) { s++; t++; } } void strcpy(char *s, char *t) { while((*s++ = *t++)!= \0 ); } 40

Pointers and Text Strings statically declared, initialized string : static const char *myformat = "Total Amount"; myformat array of 13 characters implied null character ('\0') tacked on to the end of the string after the 't' as the 13 th item in the array char myflower[]={'p','e','t','e','r','\0'}; 41

Arrays of char vs. char Pointers char tabstr [8] = "hello"; tabstr[2]='j'; puts(tabstr);//hejlo char tab[8]; tabstr=tab;//wrong not allowed char *ptr="lol"; tabstr=ptr;//wrong not allowed can change content of tabstr if not exceed 7 characters cannot change address of array char * ptrstr = "hello"; ptrstr[2]='j';//wrong not allowed char *ptr2="kifak"; ptrstr=ptr2; puts(ptrstr);//kifak ptrstr=tabstr; puts(ptrstr);//hello if pointer is assigned a string at declaration compiler creates string constant variable ptrstr = address of string can change value of ptrptr point to another string cannot change content of chain initially created 42

TIP declare char s[20] inside functions declare char* s as function parameters when dealing with constant strings 43

Long strings initialization of long string can be split across lines of source code as follows: static char *longstr ="My name is Rudolph and I " "work as a reindeer around Christmas time " "up at the North Pole. My boss is a swell " "guy. He likes to give everybody gifts."; 44

static const 45

Multidimensional Arrays allow multidimensional arrays by using more [ ] int matrix[5][10]; functions can be compiled separately must denote all but one dimension of a multiple dimensional array in a function s parameter list void afunction(int amatrix[ ][10]); arrays are referenced through pointers multiple ways to declare and access 2+ dimensional arrays more relevant when dealing with an array of strings 46

Multidimensional Arrays int a[4][4]={ 1, 2, 3, 4, printf("%d ",a[2][3]); printf("%d ",*a[1]); printf("%d ",**(a+3)); 11, 22, 33, 44, printf("%d ",*(*(a+2)+1)); output 444 11 1111 222 111, 222, 333, 444, 1111,2222,3333,4444}; 47

Array of Pointers int *a[3]; // array of 3 pointers int x[2] = {1, 2}; int y[3] = {3, 4, 5}; int z[4] = {6, 7, 8, 9}; *a = &x[0]; // a[0] points to x[0] *(a+1) = &y[0]; // a[1] points to y[0] *(a+2) = &z[0]; // a[2] points to z[0] array a is a jagged array, it is not rectangular, or of equal dimensions 48

Arrays of Strings implement an array of strings as a 2-D array of chars? char array[10][10]; 2 disadvantages all strings will be 10 chars long requires 2 nested for-loops for most operations such as string comparison or string copying, which can become complicated implement as an array of pointers instead char *array[10]; each pointer points to one string follow the string through the pointer go to the next string using a for-loop because strcpy, strcmp, strlen all expect pointers, we can use these by passing an array element (since each array element is a pointer to a string) 49

Example char *x[ ] = {"hello\0", "goodbye\0", "so long\0", "thanks for all the fish\0"}; char *y; int i; for(i=0;i<4;i++){ } y = x[i]; // used to move through a single string while(*y!='\0') { } // iterate for each string in x // x[i] is an array, printf("%c", *y); y++; printf("\n"); replace by puts(y); 50

Note on the previous example if we had used char x[ ][ ] = { }; storage space would have been 4 strings of length 24 (the length of the longest string) char x[][]={ }; 4x24xsizeof(char)= 96 bytes char *x[]={ }; 4xsizeof(char *=int)(for the array of pointers)+ (6+8+8+24)*sizeof(char)= 62 bytes 51

Pointers to Pointer can have an array of arrays which is really an array of pointers or pointer to pointers may wish to use pointers to pointers outside of arrays as well 52

Multiple Indirection can have a pointer point to another pointer that points to the target value 53

Example float **newbalance; newbalance is not a pointer to a floating-point number but rather a pointer to a float pointer to access target value indirectly pointed to by a pointer to a pointer must apply asterisk operator twice 54

Complete Example #include <stdio.h> void main() { int x, *p, **q; x = 10; p = &x; q = &p; printf("%d", **q); /* print the value of x */ } 55

void Pointers (void *) C support the pointer to void type specified as void * variables of this type are pointers to data of an unspecified type in this context (but not the others) void acts as a universal type a program can convert a pointer to any type of data to a pointer to void and back to the original type without losing information 56

Example #include <stdio.h> #include <conio.h> int main(void){ int x=2,x2, *px; float y=2.1f, y2, *py; void *p; // p can point to either x or y p=&x; //to dereference p, //must cast first printf("%d\t",*(int *)p); x2=*(int *)p; printf("%d\t",x2); } //to assign the value of p //to another pointer //also must cast first px=(int *)p; printf("%d\n",*px); //same with float p=&y; printf("%.1f\t",*(float *)p); y2=*(float *)p; printf("%.1f\t",y2); py=(float *)p; printf("%.1f\n",*py); getch(); return 0; 57