Fundamentals of Programming Session 19

Similar documents
Fundamentals of Programming Session 20

Fundamentals of Programming Session 19

Fundamentals of Programming Session 20

Fundamentals of Programming Session 15

Fundamentals of Programming Session 12

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

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

Fundamentals of Programming Session 7

by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming Session 24

Fundamentals of Programming Session 8

C Pointers. 7.2 Pointer Variable Definitions and Initialization

C Pointers Pearson Education, Inc. All rights reserved.

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

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

Fundamentals of Programming Session 14

Fundamentals of Programming Session 25

Fundamentals of Programming Session 4

Fundamentals of Programming Session 9

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

PROGRAMMAZIONE I A.A. 2017/2018

Introduction to Programming

Fundamentals of Programming Session 23

Problem Solving and 'C' Programming

Fundamental of Programming (C)

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

Chapter 5 - Pointers and Strings

Fundamentals of Programming

Chapter 5 - Pointers and Strings

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

... Lecture 12. Pointers

Introduction to Programming

Fundamentals of Programming Session 13

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

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

Fundamentals of Programming Session 2

Fundamental of Programming (C)

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

Intermediate Programming, Spring 2017*

C++ Programming Chapter 7 Pointers

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

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

C: How to Program. Week /Mar/05

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

PROGRAMMAZIONE I A.A. 2017/2018

Have the same meaning as variables in algebra Single alphabetic character Each variable needs an identifier that distinguishes it from the others a =

Advanced Systems Programming

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

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

Chapter 7 C Pointers

Chapter 6 - Pointers

Lecture 05 POINTERS 1

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

COMsW Introduction to Computer Programming in C

C Programming Language (Chapter 2 of K&R) Variables and Constants

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

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

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

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

Introduction to Programming

Programming in C++ 5. Integral data types

Tokens, Expressions and Control Structures

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

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

First of all, it is a variable, just like other variables you studied

Pointers, Arrays, and Strings. CS449 Spring 2016

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CS201- Introduction to Programming Current Quizzes

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

CSC 1107: Structured Programming

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Pointers, Dynamic Data, and Reference Types

More about BOOLEAN issues

Content. In this chapter, you will learn:

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

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

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

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

C++ For Science and Engineering Lecture 15

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

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

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

C: How to Program. Week /Apr/23

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

DECLARAING AND INITIALIZING POINTERS

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

COMP 2355 Introduction to Systems Programming

Lecture 3. More About C

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

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

Principles of C and Memory Management

by Pearson Education, Inc. All Rights Reserved. 2

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

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

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

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

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen

Type Checking. Prof. James L. Frankel Harvard University

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 sizeof Operator 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 following program? int main() { char *p; p="hello"; printf("%s\n", *&*&p); return 0; } Answer: hello 19

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

Question 3 What will be the output of the following program? int main() { char *str; str = "A%d\n"; str++; str++; printf(str-1, 300); return 0; } Answer: 300 21

Question 4 What will be the output of the following program? int main() { int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8}; int *p, *q; p = &arr[1][1][1]; q = arr[0][1]; printf("%d, %d\n", *p, *q); return 0; } Answer: 8, 3 22

sizeof Operator C provides the special unary operator sizeof to determine the size in bytes of an array (or any other data type) during program compilation. When applied to the name of an array as in Fig. 7.16 (line 14), the sizeof operator returns the total number of bytes in the array as an integer. Variables of type float are normally stored in 4 bytes of memory, and array is defined to have 20 elements. Therefore, there are a total of 80 bytes in array. 23

24 sizeof Operator

25 sizeof Operator

sizeof Operator 26 The number of elements in an array also can be determined with sizeof. For example, consider the following array definition: double real[ 22 ]; Variables of type double normally are stored in 8 bytes of memory. Thus, array real contains a total of 176 bytes. To determine the number of elements in the array, the following expression can be used: sizeof( real ) / sizeof( real[ 0 ] ) Figure 7.17 calculates the number of bytes used to store each of the standard data types.

27 sizeof Operator

28 sizeof Operator

sizeof Operator 29 Operator sizeof can be applied to any variable name, type or value (including the value of an expression). When applied to a variable name (that is not an array name) or a constant, the number of bytes used to store the specific type of variable or constant is returned. The parentheses used with sizeof are required if a type name with two words is supplied as its operand (such as long double or unsigned short). Omitting the parentheses in this case results in a syntax error. The parentheses are not required if a variable name or a oneword type name is supplied as its operand, but they can still be included without causing an error.

Question 5 What will be the output of the program assuming that the array begins at location 1002? int main() { int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8}; int *p, *q; p = &arr[1][1][1]; q = (int*) arr; printf("%d, %d\n", *p, *q); return 0; } Answer: 30 8 10

Question 6 What will be the output of the following program? int main(){ register int a = 25; int *p; p=&a; printf("%d ",*p); return 0; } Answer: It depends! Basically it should generate an error. 31

Question 7 32 What will be the output of the following program? int power(int**); int main() { int a=5, *aa; /* Address of 'a' is 1000 */ aa = &a; a = power(&aa); printf("%d\n", a); return 0; } int power(int **ptr) { int b; b = **ptr***ptr; return (b); } Answer: 25

Question 8 33 What does the following code do in C? int main() { char str[80]; char token[10]; char *p, *q; printf("enter a sentence: "); scanf("%s",str); p = str; while (*p) { q = token; while (*p) { *q = *p; q++ ; p++; } if (*p) p++; *q = '\0'; printf("%s\n", token); } return 0; } Answer: It gets a sentence and prints it!

Question 9 What will be the output of the following program? int main() { printf("%d, %d\n", sizeof(null), sizeof("")); return 0; } Answer: 4, 1 34

Question 10 What will be the output of the following program? int main() { int *p1,**p2; double *q1,**q2; printf("%d %d ",sizeof(p1),sizeof(p2)); printf("%d %d",sizeof(q1),sizeof(q2)); return 0; } Answer: 35 4 4 4 4