Fundamentals of Programming Session 19

Similar documents
Fundamentals of Programming Session 19

Fundamentals of Programming Session 20

Fundamentals of Programming Session 20

Fundamentals of Programming Session 15

Fundamentals of Programming Session 12

Fundamentals of Programming Session 24

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Fundamentals of Programming Session 8

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

Fundamentals of Programming Session 14

C Pointers. 7.2 Pointer Variable Definitions and Initialization

Fundamentals of Programming Session 25

Fundamentals of Programming Session 4

C Pointers Pearson Education, Inc. All rights reserved.

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Fundamentals of Programming Session 7

Fundamentals of Programming Session 23

Fundamentals of Programming Session 13

Introduction to Programming

Fundamentals of Programming Session 9

Fundamentals of Programming Session 2

Fundamentals of Programming Session 1

COMsW Introduction to Computer Programming in C

Lab 3. Pointers Programming Lab (Using C) XU Silei

Chapter 5 - Pointers and Strings

Chapter 5 - Pointers and Strings

Pointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Lecture 05 POINTERS 1

Introduction to Programming

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

POINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn:

Introduction to Programming

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Chapter 7 C Pointers

by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming Session 1

Chapter 6 - Pointers

Week 3: Pointers (Part 2)

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Fundamentals of Programming Session 24

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Fundamental of Programming (C)

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

PROGRAMMAZIONE I A.A. 2017/2018

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

ENEE150 Final Exam Review

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

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

INTRODUCTION TO C++ C FORMATTED INPUT/OUTPUT. Dept. of Electronic Engineering, NCHU. Original slides are from

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Programming in C. Pointers and Arrays

CMSC202 Computer Science II for Majors

Huawei Test 3. 1 A card is drawn from a pack of 52 cards. The probability of getting a queen of club or a king of heart is:

Fundamentals of Programming Session 25

C Pointers. 6th April 2017 Giulio Picierro

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Arrays and Pointers (part 2) Be extra careful with pointers!

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

Introduction to Programming session 24

Pointers in C/C++ 1 Memory Addresses 2

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

C++ Programming Chapter 7 Pointers

APSC 160 Review Part 2

Programming in C - Part 2

Homework #3 CS2255 Fall 2012

Language comparison. C has pointers. Java has references. C++ has pointers and references

Pointers, Dynamic Data, and Reference Types

CSE2301. Arrays. Arrays. Arrays and Pointers

C: How to Program. Week /Apr/23

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Today s class. Review of more C Operating system overview. Informationsteknologi

Slides adopted from T. Ferguson Spring 2016

C Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then

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

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

Exam 3 Chapters 7 & 9

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

Tokens, Expressions and Control Structures

Intermediate Programming, Spring 2017*

CSC C69: OPERATING SYSTEMS

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

CpSc 1011 Lab 11 Pointers

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Variation of Pointers

Pointers in C. Recap: Anatomy of a Typical C Program. declarations variables functions. int main (void){ declarations; statements; return value; }

Lecture 04 Introduction to pointers

Arrays and Pointers (part 2) Be extra careful with pointers!

Pointers, Arrays and C-Strings

Pointers as Arguments

Tutorial 10 Pointers in C. Shuyue Hu

EL2310 Scientific Programming

Pointers and Arrays 1

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Transcription:

Fundamentals of Programming Session 19 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology

Outlines Using the const Qualifier with Pointers 2

Using the const Qualifier with Pointers 3 The const qualifier enables you to inform the compiler that the value of a particular variable should not be modified. If an attempt is made to modify a value that is declared const, the compiler catches it and issues either a warning or an error, depending on the particular compiler. There are four ways to pass a pointer to a function: a nonconstant pointer to non-constant data, a constant pointer to nonconstant data, a non-constant pointer to constant data, and a constant pointer to constant data. Each of the four combinations provides different access privileges. These are discussed in the next several examples.

Using the const Qualifier with Pointers The highest level of data access is granted by a nonconstant pointer to non-constant data. In this case, the data can be modified through the dereferenced pointer, and the pointer can be modified to point to other data items. Function converttouppercase of Fig. 7.10 declares its parameter, a non-constant pointer to non-constant data called sptr (char *sptr), in line 21. 4

5 Using the const Qualifier with Pointers

6 Using the const Qualifier with Pointers

Using the const Qualifier with Pointers 7 A non-constant pointer to constant data can be modified to point to any data item of the appropriate type, but the data to which it points cannot be modified. Such a pointer might be used to receive an array argument to a function that will process each element without modifying the data. For example, function printcharacters (Fig. 7.11) declares parameter sptr to be of type const char * (line 22).

8 Using the const Qualifier with Pointers

9 Using the const Qualifier with Pointers

Using the const Qualifier with Pointers Figure 7.12 illustrates the attempt to compile a function that receives a non-constant pointer (xptr) to constant data. This function attempts to modify the data pointed to by xptr in line 20 which results in a compilation error. 10

11 Using the const Qualifier with Pointers

12 Using the const Qualifier with Pointers

Using the const Qualifier with Pointers A constant pointer to non-constant data always points to the same memory location, and the data at that location can be modified through the pointer. This is the default for an array name. Pointers that are declared const must be initialized when they re defined (if the pointer is a function parameter, it s initialized with a pointer that is passed to the function). Figure 7.13 attempts to modify a constant pointer. 13

14 Using the const Qualifier with Pointers

15 Using the const Qualifier with Pointers

Using the const Qualifier with Pointers 16 The least access privilege is granted by a constant pointer to constant data. Such a pointer always points to the same memory location, and the data at that memory location cannot be modified. This is how an array should be passed to a function that only looks at the array using array subscript notation and does not modify the array. Figure 7.14 defines pointer variable ptr (line 13) to be of type const int *const, which is read from right to left as ptr is a constant pointer to an integer constant.

17 Using the const Qualifier with Pointers

18 Using the const Qualifier with Pointers

Question 1 What will be the output of the program? int main() { int ***r, **q, *p, i=8; p = &i; q = &p; r = &q; printf("%d, %d, %d\n", *p, **q, ***r); return 0; } Answer: 8, 8, 8 19

Question 2 What will be the output of the following program? int main() { char *p; p="hello"; printf("%s\n", *&*&p); return 0; } Answer: hello 20

Question 3 What will be the output of the following program? int main() { printf("%c\n", 5["Salaam"]); return 0; } Answer: m 21

Question 4 What will be the output of the program? int main() { int x=30, *y, *z; y=&x; /* Assume address of x is 500 and integer is 4 byte size */ z=y; *y++=*z++; x++; printf("x=%d, y=%d, z=%d\n", x, *--y, z--); return 0; } Answer: x=31, y=31, z=500 22