Chapter 11: Pointers
|
|
- Garey Rodgers
- 6 years ago
- Views:
Transcription
1 Chapter 11: Pointers Christian Jacob 1 Pointer Basics What Are Pointers? Pointer Operators Pointer Expressions 14 2 Pointers and Arrays Pointer Arithmetic And Array Indexing Pointers Versus Arrays An Example Indexing of Pointers Interchangeability of Pointers and Arrays 25 3 String Constants and Pointers 26 4 Arrays of Pointers 27 5 Problems with Pointers Uninitialized Pointers The Null Pointer Convention Invalid Pointer Comparisons Forgetting to Reset a Pointer 32 TOC 1 Back
2 Chapter 11: Pointers Christian Jacob 6 Multiple Indirection Pointers to Pointers 34 TOC 2 Back
3 Pointer Basics Chapter 11: Pointers Christian Jacob 1 Pointer Basics 1.1 What Are Pointers? A pointer is a variable that contains a memory address. Very often this address is the location of another variable. The general form of a pointer variable declaration in C++ is: type *variable-name; type variable-name * the pointer s base type. It must be a valid C++ type. the name of the pointer variable the at address operator Returns the value of the variable located at the address specified by its operand. int *int_pointer; // pointer to integer float *float_pointer; // pointer to float First Back TOC 3 Notes Prev Next Last
4 Pointer Basics Chapter 11: Pointers Christian Jacob 1.2 Pointer Operators There are two special operators that are used with pointers: & : address of operator A unary operator which returns the memory address of its operand. * : value at address operator A unary operator which returns the value of the variable located at the address specified by its operand. Example: int balance; int *balptr; balptr = &balance; bal_ptr balance value First Back TOC 4 Notes Prev Next Last
5 Pointer Basics Chapter 11: Pointers Christian Jacob Example (continued): int balance, value; int *balptr; balance = 3200; // Step 1 balptr = &balance; // Step 2 value = *balptr; // Step 3 Step 1: Step 2: Step 3: 12 - bal_ptr bal_ptr bal_ptr balance value balance value balance value First Back TOC 5 Notes Prev Next Last
6 Pointer Basics Chapter 11: Pointers Christian Jacob Importance of the Base Type How does C++ know how many bytes to copy into value from the address pointed to by balptr? How does the compiler know the proper number of bytes for any assignment using a pointer? Answer: The base type of the pointer determines the type of data that the compiler assumes the pointer is pointing to. The following code fragment is incorrect: int *int_ptr; double f; //... int_ptr = &f; // ERROR Technically correct, but not recommended (using a type cast operator): int_ptr = (int *) &f; First Back TOC 6 Notes Prev Next Last
7 Pointer Basics Chapter 11: Pointers Christian Jacob Example for why to be careful about type casts with pointers: #include <iostream.h> int main() { double x, y; int *ptr; x = ; ptr = (int *) &x; // use cast to assign // double* to int* y = *ptr; cout << y; // What will this do? // What will this print? return(0); First Back TOC 7 Notes Prev Next Last
8 Pointer Basics Chapter 11: Pointers Christian Jacob Assigning Values Through a Pointer Pointers can be used on the left side of assignment statements. The following code fragment assigns a value to the location pointed to by the pointer. int *ptr; *ptr = 101; At the location pointed to by p, assign the value 101. Increment and decrement operations work on pointers, too. (*ptr)++; At the location pointed to by p, increment the value by 1. First Back TOC 8 Notes Prev Next Last
9 Pointer Basics Chapter 11: Pointers Christian Jacob Example program for assigning values through pointers: #include <iostream.h> int main() { int *ptr, num; // 1 ptr = # // 2 *ptr = 100; // 3 cout << num << ; (*ptr)++; // 4 cout << num << ; (*ptr)*2; // 5 cout << num << \n ; Step 1 12 *_int ptr 50 _int num - return 0; First Back TOC 9 Notes Prev Next Last
10 Pointer Basics Chapter 11: Pointers Christian Jacob Example program for assigning values through pointers: #include <iostream.h> int main() { int *ptr, num; // 1 ptr = # // 2 *ptr = 100; // 3 cout << num << ; (*ptr)++; // 4 cout << num << ; (*ptr)*2; // 5 cout << num << \n ; Step ptr 50 _int num - return 0; First Back TOC 10 Notes Prev Next Last
11 Pointer Basics Chapter 11: Pointers Christian Jacob Example program for assigning values through pointers: #include <iostream.h> int main() { int *ptr, num; // 1 ptr = # // 2 *ptr = 100; // 3 cout << num << ; (*ptr)++; // 4 cout << num << ; (*ptr)*2; // 5 cout << num << \n ; Step ptr num - return 0; First Back TOC 11 Notes Prev Next Last
12 Pointer Basics Chapter 11: Pointers Christian Jacob Example program for assigning values through pointers: #include <iostream.h> int main() { int *ptr, num; // 1 ptr = # // 2 *ptr = 100; // 3 cout << num << ; (*ptr)++; // 4 cout << num << ; (*ptr)*2; // 5 cout << num << \n ; Step ptr num - return 0; First Back TOC 12 Notes Prev Next Last
13 Pointer Basics Chapter 11: Pointers Christian Jacob Example program for assigning values through pointers: #include <iostream.h> int main() { int *ptr, num; // 1 ptr = # // 2 *ptr = 100; // 3 cout << num << ; (*ptr)++; // 4 cout << num << ; (*ptr)*2; // 5 cout << num << \n ; Step ptr num - return 0; First Back TOC 13 Notes Prev Next Last
14 Pointer Basics Chapter 11: Pointers Christian Jacob 1.3 Pointer Expressions Pointers can be used in most C++ expressions. Keep in mind to use parentheses around pointer expressions. Pointer Arithmetic Only four arithmetic operators can be used on pointers: ++, --, +, and -. Example: (assuming 32-bit integers) int *p1; // assume: p1 == 2000 p1++; 1996 byte 1 byte 2 byte 3 byte 4 p1--; 2000 byte 1 byte 2 byte 3 byte byte 1 byte 2 byte 3 byte 4 First Back TOC 14 Notes Prev Next Last
15 Pointer Basics Chapter 11: Pointers Christian Jacob Integers can be added or subtracted from pointers: You can subtract pointers of the same type from one another. You can not add pointers! However, you can add int numbers to pointers: #include <iostream.h> int main() { int i[10], *i_ptr; double f[10], *f_ptr; int x; i_ptr = i; // i_ptr points to first element of i f_ptr = f; // f_ptr points to first element of f for(x=0; x<10; x++) cout << i_ptr+x << << f_ptr+x << \n ; return 0; First Back TOC 15 Notes Prev Next Last
16 Pointer Basics Chapter 11: Pointers Christian Jacob Output: The addresses of the array elements: 4 bytes int 8 bytes double 0xeffffd9c 0xeffffd48 0xeffffda0 0xeffffd50 0xeffffda4 0xeffffd58 0xeffffda8 0xeffffd60 0xeffffdac 0xeffffd68 0xeffffdb0 0xeffffd70 0xeffffdb4 0xeffffd78 0xeffffdb8 0xeffffd80 0xeffffdbc 0xeffffd88 0xeffffdc0 0xeffffd90 First Back TOC 16 Notes Prev Next Last
17 Pointer Basics Chapter 11: Pointers Christian Jacob Pointer Comparisons Pointers may be compared using relational operators, such as!=, ==, <, and >. int main() { int num[10]; int *start, *end; start = num; end = &num[9]; while(start!= end) { cout << Enter a number: ; cin >> *start; start++; return 0; First Back TOC 17 Notes Prev Next Last
18 Pointer Basics Chapter 11: Pointers Christian Jacob Pointer Comparisons (2): using subtraction of pointers Pointers may be compared using relational operators, such as!=, ==, <, and >. int main() { int num[10]; int *start, *end; start = num; end = &num[9]; while((end - start) > 0) { cout << Enter a number: ; cin >> *start; start++; return 0; First Back TOC 18 Notes Prev Next Last
19 Pointers and Arrays Chapter 11: Pointers Christian Jacob 2 Pointers and Arrays 2.1 Pointer Arithmetic And Array Indexing In C++, using the name of an array without an index generates a pointer to the first element in the array: char str[80]; char *ptr; ptr = str; The last statement assigns the address of str[0] to ptr. Now there are two ways to access the n-th element in the array: str[n-1] or *(ptr + (n-1)) First Back TOC 19 Notes Prev Next Last
20 Pointers and Arrays Chapter 11: Pointers Christian Jacob 2.2 Pointers Versus Arrays An Example Here are two versions of a tokenizing program, a (usually) more efficient version with pointers, and a version using arrays. Tokeninzing Program: Pointer Version int main() { char str[80]; char token[80]; char *str_ptr, *tk_ptr; cout << Enter a sentence: ; gets(str); str_ptr = str;... First Back TOC 20 Notes Prev Next Last
21 Pointers and Arrays Chapter 11: Pointers Christian Jacob // Read a token at a time from the string while(*str_ptr) { tk_ptr = token; // start of token // Read characters until either a space or // the null terminator is encountered. while( *str_ptr!= && *str_ptr ) { *tk_ptr = *str_ptr; tk_ptr++; str_ptr++; if(*str_ptr) str_ptr++;// advance past space *tk_ptr = \0 ; // null terminate the token cout << token << endl; return 0; First Back TOC 21 Notes Prev Next Last
22 Pointers and Arrays Chapter 11: Pointers Christian Jacob Tokeninzing Program: Array Version #include <iostream> #include <cstdio> int main() { char str[80]; char token[80]; int i, j; cout << Enter a sentence: ; gets(str);... First Back TOC 22 Notes Prev Next Last
23 Pointers and Arrays Chapter 11: Pointers Christian Jacob // Read a token at a time from the string for(i=0; ; i++) { // Read characters until either a space or // a null terminator is encountered. for(j=0; str[i]!= && str[i]; j++, i++) token[j] = str[i]; token[j] = \0 ; // null termination cout << token << \n ; if(!str[i]) break; return 0; First Back TOC 23 Notes Prev Next Last
24 Pointers and Arrays Chapter 11: Pointers Christian Jacob 2.3 Indexing of Pointers One can not only use pointers to access array elements, but a pointer may also be indexed as if it were an array. #include <iostream> #include <cctype> int main() { char str[20] = hello tom ; char *p; int i; p = str; for(i=0; p[i]; i++) p[i] = toupper(p[i]); cout << p; return 0; First Back TOC 24 Notes Prev Next Last
25 Pointers and Arrays Chapter 11: Pointers Christian Jacob 2.4 Interchangeability of Pointers and Arrays Pointers and arrays are are strongly related in many cases. However, pointers and arrays are not completely interchangeable: int num[10]; int i; for(i=0; i<10; i++) { *num = i; num++; // ERROR -- cannot modify num num is a constant that points to the beginning of the array. Generally, an array name without an index generates a pointer to the beginning of an array, but it cannot be changed. *(num+3) = 100; // OK because num not modified First Back TOC 25 Notes Prev Next Last
26 String Constants and Pointers Chapter 11: Pointers Christian Jacob 3 String Constants and Pointers String constants appearing in program text are stored in the program s string table, which is generated at runtime. For every entry in the string table, a pointer to the string is generated. #include <iostream> int main() { char *s; s = Pointers are fun to use.\n ; cout << s; return 0; First Back TOC 26 Notes Prev Next Last
27 Arrays of Pointers Chapter 11: Pointers Christian Jacob 4 Arrays of Pointers Pointers can be arrayed like any other data type in C++. For example, to declare an array pi of 10 integer pointers, one would write: int *pi[10]; To assign the address of an integer variable called var to the third element of the pointer array, one would write: int var; pi[2] = &var; The following expression gives the value of var: *pi[2] First Back TOC 27 Notes Prev Next Last
28 Problems with Pointers Chapter 11: Pointers Christian Jacob 5 Problems with Pointers 5.1 Uninitialized Pointers The classic example of a pointer error is the uninitialized pointer: // This program is wrong. int main() { int x, *p; x = 10; *p = x; // Where does p point? return 0; First Back TOC 28 Notes Prev Next Last
29 Problems with Pointers Chapter 11: Pointers Christian Jacob 5.2 The Null Pointer Convention After a pointer is declared, but before it has been assigned, it will contain an arbitrary value. By convention, if a pointer contains the null (zero) value, it is assumed to point to nothing. float *p = 0; // p is now a null pointer Any type of pointer can be initialized to null when it is declared, thus avoiding accidental misuse of uninitialized pointers. To check for a null pointer, use an if statement: if(p) // succeeds if p is not null if(!p) // succeeds if p is null First Back TOC 29 Notes Prev Next Last
30 Problems with Pointers Chapter 11: Pointers Christian Jacob 5.3 Invalid Pointer Comparisons Making any comparisons between pointers to two different objects, not belonging to the same array, may yield unexpected results. char s[80]; char y[80]; char *p1, *p2; p1 = s; p2 = y; if( p1 < p2)... This code makes invalid assumptions about C++ s strategy of placing variables in memory. First Back TOC 30 Notes Prev Next Last
31 Problems with Pointers Chapter 11: Pointers Christian Jacob One should also never assume that two back-to-back arrays can be indexed as one: int first[10]; int second[10]; int *p, t; p = first; for(t=0; t<20; ++t) { *p = t; p++; First Back TOC 31 Notes Prev Next Last
32 Problems with Pointers Chapter 11: Pointers Christian Jacob 5.4 Forgetting to Reset a Pointer The following program displays the ASCII codes of each character in a string entered from the keyboard. This program has a serious bug! int main() { char s[80], *p1; p1 = s; do{ cout << Enter a string: ; gets(p1); // read a string // print the ASCII values while(*p1) cout << (int) *p1++ << ; cout << \n ; while( strcmp(s, done ) ); return 0; First Back TOC 32 Notes Prev Next Last
33 Problems with Pointers Chapter 11: Pointers Christian Jacob Here is the corrected version, so that pointer p1 is reset to the beginning of the character array s at the beginning of each loop iteration: int main() { char s[80], *p1; do{ p1 = s; cout << Enter a string: ; gets(p1); // read a string // print the ASCII values while(*p1) cout << (int) *p1++ << ; cout << \n ; while( strcmp(s, done ) ); return 0; First Back TOC 33 Notes Prev Next Last
34 Multiple Indirection Pointers to Pointers Chapter 11: Pointers Christian Jacob 6 Multiple Indirection Pointers to Pointers A pointer to a pointer a chain of pointers is a form of multiple indirection. In the case of a normal pointer the value of the pointer is the address of a value. In the case of a pointer to a pointer the first pointer contains the address of the second, normal pointer. Single Indirection Pointer address Variable value Pointer Pointer Variable Multiple Indirection address address value First Back TOC 34 Notes Prev Next Last
35 Multiple Indirection Pointers to Pointers Chapter 11: Pointers Christian Jacob Example using multiple indirection: int main() { int x, *p, **q; x = 10; p = &x; q = &p; cout << **q; // prints the value of x return 0; First Back TOC 35 Notes Prev Next Last
Module 4 Arrays, Strings, and Pointers
Module 4 Arrays, Strings, and Pointers Table of Contents CRITICAL SKILL 4.1: Use one-dimensional arrays... 2 CRITICAL SKILL 4.2: Two-Dimensional Arrays... 6 CRITICAL SKILL 4.3: Multidimensional Arrays...
More informationHomework #3 CS2255 Fall 2012
Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation
More informationC++ for Java Programmers
Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even
More informationChapter 10. Arrays and Strings
Christian Jacob Chapter 10 Arrays and Strings 10.1 Arrays 10.2 One-Dimensional Arrays 10.2.1 Accessing Array Elements 10.2.2 Representation of Arrays in Memory 10.2.3 Example: Finding the Maximum 10.2.4
More informationCS2255 HOMEWORK #1 Fall 2012
CS55 HOMEWORK #1 Fall 01 1.What is assigned to the variable a given the statement below with the following assumptions: x = 10, y = 7, and z, a, and b are all int variables. a = x >= y; a. 10 b. 7 c. The
More informationFundamentals of Programming Session 20
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
More informationCS201- Introduction to Programming Current Quizzes
CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements
More informationComputer Programming
Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering Session: Character Strings Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, 1 Quick Recap
More informationCSC 211 Intermediate Programming. Arrays & Pointers
CSC 211 Intermediate Programming Arrays & Pointers 1 Definition An array a consecutive group of memory locations that all have the same name and the same type. To create an array we use a declaration statement.
More informationChapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:
Chapter-11 POINTERS Introduction: Pointers are a powerful concept in C++ and have the following advantages. i. It is possible to write efficient programs. ii. Memory is utilized properly. iii. Dynamically
More informationPOINTER & REFERENCE VARIABLES
Lecture 9 POINTER & REFERENCE VARIABLES Declaring data pointer variables Assignment operations with pointers Referring objects using pointer variables Generic pointers Operations with pointer variables
More informationObjectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program
Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationChapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationFORM 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):
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): 1. If a function has default arguments, they can be located anywhere
More informationChapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction
Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers
More informationFORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):
FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. The basic commands that a computer performs are input (get data), output (display result),
More information5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!
FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each): 1. Assume that all variables are properly declared. The following for loop executes 20 times.
More informationAssist. Prof. Dr. Caner ÖZCAN
Assist. Prof. Dr. Caner ÖZCAN Memory Structure When a variable defined it is stored somewhere in memory. Memory can be thought as block consist of cells. When a variable defined, required number of cell
More informationLecture 3 Tao Wang 1
Lecture 3 Tao Wang 1 Objectives In this chapter, you will learn about: Arithmetic operations Variables and declaration statements Program input using the cin object Common programming errors C++ for Engineers
More informationA First Program - Greeting.cpp
C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout
More informationcalling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!
Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns
More informationProgramming in C++ 5. Integral data types
Programming in C++ 5. Integral data types! Introduction! Type int! Integer multiplication & division! Increment & decrement operators! Associativity & precedence of operators! Some common operators! Long
More informationOutput of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8
Pointers Variables vs. Pointers: A variable in a program is something with a name and a value that can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within
More informationChapter 2: Introduction to C++
Chapter 2: Introduction to C++ Copyright 2010 Pearson Education, Inc. Copyright Publishing as 2010 Pearson Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 2.1 Parts of a C++
More informationReview: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements
Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja
More informationChapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen
Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Copyright 2009 Publishing Pearson as Pearson Education, Addison-Wesley Inc. Publishing as Pearson Addison-Wesley
More informationChapter 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.
Chapter 9: Pointers 9.1 Getting the Address of a Variable C++ Variables [ not in book ] A Variable has all of the following attributes: 1. name 2. type 3. size 4. value 5. storage class static or automatic
More information1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.
1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format
More information9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable
CS 1400 Chapter 9 9.1 Getting the Address of a Variable A variable has: Name Value Location in a memory Type The location in memory is an address Use address operator & to get address of a variable: int
More informationObjectives. In this chapter, you will:
Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about
More informationLecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?
Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static
More informationModule 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1
Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Topics 1. Expressions 2. Operator precedence 3. Shorthand operators 4. Data/Type Conversion 1/15/19 CSE 1321 MODULE 2 2 Expressions
More informationCS162 - POINTERS. Lecture: Pointers and Dynamic Memory
CS162 - POINTERS Lecture: Pointers and Dynamic Memory What are pointers Why dynamically allocate memory How to dynamically allocate memory What about deallocation? Walk thru pointer exercises 1 CS162 -
More informationChapter 2 (Dynamic variable (i.e. pointer), Static variable)
Chapter 2 (Dynamic variable (i.e. pointer), Static variable) August_04 A2. Identify and explain the error in the program below. [4] #include int *pptr; void fun1() { int num; num=25; pptr= #
More informationPointer Basics. Lecture 13 COP 3014 Spring March 28, 2018
Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory
More informationC++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.
EEE-117 COMPUTER PROGRAMMING Basic Elements of C++ Objectives General Questions Become familiar with the basic components of a C++ program functions, special symbols, and identifiers Data types Arithmetic
More informationCHAPTER 3 BASIC INSTRUCTION OF C++
CHAPTER 3 BASIC INSTRUCTION OF C++ MOHD HATTA BIN HJ MOHAMED ALI Computer programming (BFC 20802) Subtopics 2 Parts of a C++ Program Classes and Objects The #include Directive Variables and Literals Identifiers
More informationPointers. Variable Declaration. Chapter 10
Pointers Chapter 10 Variable Declaration When a variable is defined, three fundamental attributes are associated with it: Name Type Address The variable definition associates the name, the type, and the
More informationChapter 13. Functions and Parameter Passing (Part 2)
Christian Jacob Chapter 13 Functions and Parameter Passing (Part 2) 13.1 Passing Arguments to Functions 13.1.1 Passing Pointers 13.1.2 Passing Arrays 13.1.3 Passing Strings 13.2 Parameter Passing Mechanisms
More informationC++ Programming Chapter 7 Pointers
C++ Programming Chapter 7 Pointers Yih-Peng Chiou Room 617, BL Building (02) 3366-3603 ypchiou@cc.ee.ntu.edu.tw Photonic Modeling and Design Lab. Graduate Institute of Photonics and Optoelectronics & Department
More informationUNIT- 3 Introduction to C++
UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage
More informationCSC 211 Intermediate Programming. Pointers
CSC 211 Intermediate Programming Pointers 1 Purpose Pointers enable programs to simulate call-by-reference; to create and manipulate dynamic data structures, i.e. data structures that can grow and shrink,
More informationChapter 2 Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion
More informationo Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement
Last Time Let s all Repeat Together 10/3/05 CS150 Introduction to Computer Science 1 1 We covered o Counter and sentinel controlled loops o Formatting output Today we will o Type casting o Top-down, stepwise
More informationby Pearson Education, Inc. All Rights Reserved.
Let s improve the bubble sort program of Fig. 6.15 to use two functions bubblesort and swap. Function bubblesort sorts the array. It calls function swap (line 51) to exchange the array elements array[j]
More information3. Java - Language Constructs I
Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks
More informationPointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple
More informationComputer Programming : C++
The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program
More informationDELHI PUBLIC SCHOOL TAPI
Loops Chapter-1 There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed
More informationChapter 7. Additional Control Structures
Chapter 7 Additional Control Structures 1 Chapter 7 Topics Switch Statement for Multi-Way Branching Do-While Statement for Looping For Statement for Looping Using break and continue Statements 2 Chapter
More informationIntroduction to Computer Science Midterm 3 Fall, Points
Introduction to Computer Science Fall, 2001 100 Points Notes 1. Tear off this sheet and use it to keep your answers covered at all times. 2. Turn the exam over and write your name next to the staple. Do
More informationC Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations
55:017, Computers in Engineering C Pointers C Pointers Powerful C feature but challenging to understand Some uses of pointers include Call by reference parameter passage Dynamic data structures Data structures
More informationUEE1302 (1102) F10 Introduction to Computers and Programming (I)
Computational Intelligence on Automation Lab @ NCTU UEE1302 (1102) F10 Introduction to Computers and Programming (I) Programming Lecture 10 Pointers & Dynamic Arrays (I) Learning Objectives Pointers Data
More informationNon-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.
, ean, arithmetic s s on acters Comp Sci 1570 Introduction to C++ Outline s s on acters 1 2 3 4 s s on acters Outline s s on acters 1 2 3 4 s s on acters ASCII s s on acters ASCII s s on acters Type: acter
More informationProblem Solving and 'C' Programming
Problem Solving and 'C' Programming Targeted at: Entry Level Trainees Session 15: Files and Preprocessor Directives/Pointers 2007, Cognizant Technology Solutions. All Rights Reserved. The information contained
More informationWhat is Pointer? Pointer is a variable that holds a memory address, usually location of another variable.
CHAPTER 08 POINTERS What is Pointer? Pointer is a variable that holds a memory address, usually location of another variable. The Pointers are one of the C++ s most useful and powerful features. How Pointers
More information[0569] p 0318 garbage
A Pointer is a variable which contains the address of another variable. Declaration syntax: Pointer_type *pointer_name; This declaration will create a pointer of the pointer_name which will point to the
More information7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING
KEY CONCEPTS COMP 10 EXPLORING COMPUTER SCIENCE Lecture 2 Variables, Types, and Programs Problem Definition of task to be performed (by a computer) Algorithm A particular sequence of steps that will solve
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 2. Overview of C++ Prof. Amr Goneid, AUC 1 Overview of C++ Prof. Amr Goneid, AUC 2 Overview of C++ Historical C++ Basics Some Library
More informationArrays, Pointers and Memory Management
Arrays, Pointers and Memory Management EECS 2031 Summer 2014 Przemyslaw Pawluk May 20, 2014 Answer to the question from last week strct->field Returns the value of field in the structure pointed to by
More informationCS2141 Software Development using C/C++ C++ Basics
CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short
More informationBasic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable
Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is
More informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine
More informationPointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.
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 Powerful but difficult to master Used to simulate pass-by-reference
More information2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program
Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program
More informationADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1
NAGERCOIL COMPUTER SCIENCE Grade: IX C++ PROGRAMMING 1 C++ 1. Object Oriented Programming OOP is Object Oriented Programming. It was developed to overcome the flaws of the procedural approach to programming.
More informationPOINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory
More informationInput And Output of C++
Input And Output of C++ Input And Output of C++ Seperating Lines of Output New lines in output Recall: "\n" "newline" A second method: object endl Examples: cout
More informationLECTURE 02 INTRODUCTION TO C++
PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 02 INTRODUCTION
More informationReference operator (&)
Pointers Each cell can be easily located in the memory because it has a unique address and all the memory cells follow a successive pattern. For example, if we are looking for cell 1776 we know that it
More informationWhat is an algorithm?
Announcements CS 142 C++ Pointers Reminder Program 6 due Sunday, Nov. 9 th by 11:55pm 11/3/2014 2 Pointers and the Address Operator Pointer Variables Each variable in a program is stored at a unique address
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers
More informationC++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I
C++ ARRAYS POINTERS POINTER ARITHMETIC Problem Solving with Computers-I General model of memory Sequence of adjacent cells Each cell has 1-byte stored in it Each cell has an address (memory location) Memory
More informationJagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming
Jagannath Institute of Management Sciences Lajpat Nagar BCA II Sem C Programming UNIT I Pointers: Introduction to Pointers, Pointer Notation,Decalaration and Initialization, Accessing variable through
More informationArrays. Lecture 9 COP 3014 Fall October 16, 2017
Arrays Lecture 9 COP 3014 Fall 2017 October 16, 2017 Array Definition An array is an indexed collection of data elements of the same type. Indexed means that the array elements are numbered (starting at
More informationPointers, Arrays, and Strings. CS449 Spring 2016
Pointers, Arrays, and Strings CS449 Spring 2016 Pointers Pointers are important. Pointers are fun! Pointers Every variable in your program has a memory location. This location can be accessed using & operator.
More informationCOMP26120: Pointers in C (2018/19) Lucas Cordeiro
COMP26120: Pointers in C (2018/19) Lucas Cordeiro lucas.cordeiro@manchester.ac.uk Organisation Lucas Cordeiro (Senior Lecturer, FM Group) lucas.cordeiro@manchester.ac.uk Office: 2.44 Office hours: 10-11
More informationScheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C
Sample Test Paper-I Marks : 25 Time:1 Hrs. Q1. Attempt any THREE 09 Marks a) State four relational operators with meaning. b) State the use of break statement. c) What is constant? Give any two examples.
More informationFundamentals of Programming Session 20
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
More informationBy the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program
1 By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program variables. Apply C++ syntax rules to declare variables, initialize
More informationThe C++ Language. Arizona State University 1
The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University
More informationCHAPTER 3 Expressions, Functions, Output
CHAPTER 3 Expressions, Functions, Output More Data Types: Integral Number Types short, long, int (all represent integer values with no fractional part). Computer Representation of integer numbers - Number
More informationExam 3 Chapters 7 & 9
Exam 3 Chapters 7 & 9 CSC 2100-002/003 29 Mar 2017 Read through the entire test first BEFORE starting Put your name at the TOP of every page The test has 4 sections worth a total of 100 points o True/False
More information1. In C++, reserved words are the same as predefined identifiers. a. True
C++ Programming From Problem Analysis to Program Design 8th Edition Malik TEST BANK Full clear download (no formatting errors) at: https://testbankreal.com/download/c-programming-problem-analysis-program-design-8thedition-malik-test-bank/
More informationIntroduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C
Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #29 Arrays in C (Refer Slide Time: 00:08) This session will learn about arrays in C. Now, what is the word array
More informationCS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.
CS 31 Review Sheet Tau Beta Pi - Boelter 6266 Contents 1 Basics 2 2 Working with Decimals 2 3 Handling Strings and Numbers with stdin/stdout 3 4 Operators 3 5 Common Mistakes/Misconceptions 3 6 Constants
More informationName MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.
CMPSC11 Final (Study Guide) Fall 11 Prof Hartman Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) This is a collection of statements that performs
More informationDECLARAING AND INITIALIZING POINTERS
DECLARAING AND INITIALIZING POINTERS Passing arguments Call by Address Introduction to Pointers Within the computer s memory, every stored data item occupies one or more contiguous memory cells (i.e.,
More informationAlgorithms & Data Structures
GATE- 2016-17 Postal Correspondence 1 Algorithms & Data Structures Computer Science & Information Technology (CS) 20 Rank under AIR 100 Postal Correspondence Examination Oriented Theory, Practice Set Key
More informationFORM 2 (Please put your name and form # on the scantron!!!!)
CS 161 Exam 2: FORM 2 (Please put your name and form # on the scantron!!!!) True (A)/False(B) (2 pts each): 1. Recursive algorithms tend to be less efficient than iterative algorithms. 2. A recursive function
More informationIntroduction to Programming using C++
Introduction to Programming using C++ Lecture One: Getting Started Carl Gwilliam gwilliam@hep.ph.liv.ac.uk http://hep.ph.liv.ac.uk/~gwilliam/cppcourse Course Prerequisites What you should already know
More informationCS201 Spring2009 Solved Sunday, 09 May 2010 14:57 MIDTERM EXAMINATION Spring 2009 CS201- Introduction to Programming Question No: 1 ( Marks: 1 ) - Please choose one The function of cin is To display message
More informationCS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12
CS31 Discussion 1E Jie(Jay) Wang Week3 Oct.12 Outline Problems from Project 1 Review of lecture String, char, stream If-else statements Switch statements loops Programming challenge Problems from Project
More informationObject Oriented Pragramming (22316)
Chapter 1 Principles of Object Oriented Programming (14 Marks) Q1. Give Characteristics of object oriented programming? Or Give features of object oriented programming? Ans: 1. Emphasis (focus) is on data
More informationPointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables Pointer variable: content is a memory address There is no name associated with the pointer data type in C++. Declaring Pointer Variables Syntax: Data-type *identifier
More informationCS313D: ADVANCED PROGRAMMING LANGUAGE
CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting
More informationCS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010
CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011 Lectures 1-22 Moaaz Siddiq Asad Ali Latest Mcqs MIDTERM EXAMINATION Spring 2010 Question No: 1 ( Marks: 1 ) - Please
More informationCSI33 Data Structures
Outline Department of Mathematics and Computer Science Bronx Community College October 24, 2018 Outline Outline 1 Chapter 8: A C++ Introduction For Python Programmers Expressions and Operator Precedence
More informationPIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics
PIC 10A Pointers, Arrays, and Dynamic Memory Allocation Ernest Ryu UCLA Mathematics Pointers A variable is stored somewhere in memory. The address-of operator & returns the memory address of the variable.
More informationMA 511: Computer Programming Lecture 3: Partha Sarathi Mandal
MA 511: Computer Programming Lecture 3: http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html Partha Sarathi Mandal psm@iitg.ernet.ac.in Dept. of Mathematics, IIT Guwahati Semester 1, 2010-11 Last
More information