Fundamentals of Programming Session 20

Similar documents
Fundamentals of Programming Session 19

Fundamentals of Programming Session 20

Fundamentals of Programming Session 19

by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming Session 7

Fundamentals of Programming Session 15

Fundamentals of Programming Session 12

Fundamentals of Programming Session 8

Fundamentals of Programming Session 25

Fundamentals of Programming Session 24

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

Fundamentals of Programming Session 9

Introduction to Programming

Fundamentals of Programming Session 4

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

Fundamentals of Programming Session 14

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

... Lecture 12. Pointers

Problem Solving and 'C' Programming

CSC 211 Intermediate Programming. Arrays & Pointers

Lecture 05 POINTERS 1

C++ Programming Chapter 7 Pointers

Fundamentals of Programming Session 23

Fundamentals of Programming Session 2

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

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

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

Fundamentals of Programming

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

Fundamental of Programming (C)

More about BOOLEAN issues

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

PROGRAMMAZIONE I A.A. 2017/2018

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

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Fundamental of Programming (C)

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

Operators and Expressions:

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

C: How to Program. Week /Mar/05

Chapter 11: Pointers

POINTER & REFERENCE VARIABLES

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

The New C Standard (Excerpted material)

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

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

Programming in C++ 5. Integral data types

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

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

Lecture 3. More About C

MYcsvtu Notes LECTURE 34. POINTERS

Programming in C++ 6. Floating point data types

Introduction to Programming

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

Chapter 6 - Pointers

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

Data Types and Variables in C language

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

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

Fundamentals of Programming Session 13

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Programming for Engineers Pointers

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

Structured programming. Exercises 3

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

Type Checking. Prof. James L. Frankel Harvard University

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Introduction to C. Systems Programming Concepts

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Introduction to Computer Science Midterm 3 Fall, Points

Intermediate Programming, Spring 2017*

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

Operators in C. Staff Incharge: S.Sasirekha

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

Pointers. 10/5/07 Pointers 1

Unit 3. Operators. School of Science and Technology INTRODUCTION

Work relative to other classes

Assist. Prof. Dr. Caner ÖZCAN

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

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

Infix to Postfix Conversion

Programming for Engineers: Operators, Expressions, and Statem

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

CS 61c: Great Ideas in Computer Architecture

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

Homework #3 CS2255 Fall 2012

Fundamentals of Programming Session 1

Chapter 3: Operators, Expressions and Type Conversion

Operators in java Operator operands.

Fundamentals of Programming CS-110. Lecture 3

Programming for Engineers Iteration

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

COP 3275: Chapter 04. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Part I Part 1 Expressions

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

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

C Pointers Pearson Education, Inc. All rights reserved.

Transcription:

Fundamentals of Programming Session 20 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 sizeof Operator Pointer Expressions and Pointer Arithmetic 2

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. 3

4 sizeof Operator

5 sizeof Operator

sizeof Operator 6 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.

7 sizeof Operator

8 sizeof Operator

sizeof Operator 9 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 1 What will be the output of the following program? int main() { printf("%d, %d\n", sizeof(null), sizeof("")); return 0; } Answer: 4, 1 10

Question 2 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: 11 4 4 4 4

Question 3 12 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 4 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. 13

Pointer Expressions and Pointer Arithmetic 14 Pointers are valid operands in arithmetic expressions, assignment expressions and comparison expressions. A pointer may be incremented (++) or decremented (--), an integer may be added to a pointer (+ or +=), an integer may be subtracted from a pointer (- or -=) and one pointer may be subtracted from another. If a pointer is being incremented or decremented by one, the increment (++) and decrement (--) operators can be used. Either of the statements ++vptr; vptr++;

Pointer Expressions and Pointer Arithmetic 15 Assume that array int v[5] has been defined and its first element is at location 3000 in memory. Assume pointer vptr has been initialized to point to v[0] i.e., the value of vptr is 3000. Figure 7.18 illustrates this situation for a machine with 4-byte integers. In conventional arithmetic, 3000 + 2 yields the value 3002. This is normally not the case with pointer arithmetic. The number of bytes depends on the object s data type. For example, the statement vptr += 2; would produce 3008 (3000 + 2 * 4) assuming an integer is stored in 4 bytes of memory.

16 Pointer Expressions and Pointer Arithmetic

Pointer Expressions and Pointer Arithmetic For example, if vptr contains the location 3000, and v2ptr contains the address 3008, the statement x = v2ptr - vptr; would assign to x the number of array elements from vptr to v2ptr, in this case 2 (not 8). Pointer arithmetic is meaningless unless performed on an array. A pointer can be assigned to another pointer if both have the same type. The exception to this rule is the pointer to void (i.e., void *), which is a generic pointer that can represent any pointer type. 17

Pointer Expressions and Pointer Arithmetic All pointer types can be assigned a pointer to void, and a pointer to void can be assigned a pointer of any type. In both cases, a cast operation is not required. A pointer to void cannot be dereferenced. Pointers can be compared using equality and relational operators, but such comparisons are meaningless unless the pointers point to elements of the same array. 18 A comparison of two pointers pointing to elements in the same array could show, for example, that one pointer points to a higher-numbered element of the array than the other pointer does.

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: 19 8 10

Question 6 20 What does the following code do in C? int main() { char str[80]; char token[80]; 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 7 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 21