Pointers and Strings. Adhi Harmoko S, M.Komp
|
|
- Phyllis Stone
- 6 years ago
- Views:
Transcription
1 Pointers and Strings Adhi Harmoko S, M.Komp
2 Introduction Pointers Powerful, but difficult to master Simulate call-by-reference Close relationship with arrays and strings
3 Pointer Variable Declarations and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain the address of a variable that has a specific value (indirect reference) countptr count Indirection Referencing a pointer value Pointer declarations * indicates variable is a pointer int *myptr; declares a pointer to an int, a pointer of type int * Multiple pointers require multiple asterisks int *myptr1, *myptr2; 7 count 7
4 Pointer Variable Declarations and Initialization Can declare pointers to any data type Pointers initialization Initialized to 0, NULL, or an address 0 or NULL points to nothing
5 Pointer Operators & (address operator) Returns the address of its operand Example int y = 5; int *yptr; yptr = &y; // yptr gets address of y yptr points to y yptr y 5 yptr y address of y is value of yptr
6 Pointer Operators * (indirection/dereferencing operator) Returns the value of what its operand points to *yptr returns y (because yptr points to y). * can be used to assign a value to a location in memory *yptr = 7; // changes y to 7 Dereferenced pointer (operand of *) must be an lvalue (no constants) * and & are inverses Cancel each other out *&myvar == myvar and &*yptr == yptr
7 Fig. 5.4: fig05_04.cpp // Fig. 5.4: fig05_04.cpp // Using the & and * operators. #include <iostream> using std::cout; using std::endl; int main() int a; // a is an integer int *aptr; // aptr is a pointer to an integer a = 7; aptr = &a; // aptr assigned address of a cout << "The address of a is " << &a << "\nthe value of aptr is " << aptr; cout << "\n\nthe value of a is " << a << "\nthe value of *aptr is " << *aptr;
8 Fig. 5.4: fig05_04.cpp // Fig. 5.4: fig05_04.cpp // Using the & and * operators. #include <iostream> using std::cout; using std::endl; The address of a is the value of aptr int main() int a; // a is an integer int *aptr; // aptr is a pointer to an integer a = 7; aptr = &a; // aptr assigned address of a cout << "The address of a is " << &a << "\nthe value of aptr is " << aptr; cout << "\n\nthe value of a is " << a << "\nthe value of *aptr is " << *aptr;
9 Fig. 5.4: fig05_04.cpp // Fig. 5.4: fig05_04.cpp // Using the & and * operators. #include <iostream> using std::cout; using std::endl; int main() int a; // a is an integer int *aptr; // aptr is a pointer to an integer a = 7; aptr = &a; // aptr assigned address of a cout << "The address of a is " << &a << "\nthe value of aptr is " << aptr; cout << "\n\nthe value of a is " << a << "\nthe value of *aptr is " << *aptr; The * operator returns an alias to what its operand points to. aptr points to a, so *aptr returns a.
10 Fig. 5.4: fig05_04.cpp cout << "\n\nshowing that * and & are inverses of " << "each other.\n&*aptr = " << &*aptr << "\n*&aptr = " << *&aptr << endl; return 0; // indicates successful termination } // end main
11 Fig. 5.4: fig05_04.cpp cout << "\n\nshowing that * and & are inverses of " << "each other.\n&*aptr = " << &*aptr << "\n*&aptr = " << *&aptr << endl; return 0; // indicates successful termination } // end main Notice how * and & are inverses
12 Fig. 5.4: fig05_04.cpp cout << "\n\nshowing that * and & are inverses of " << "each other.\n&*aptr = " << &*aptr << "\n*&aptr = " << *&aptr << endl; return 0; // indicates successful termination } // end main The address of a is 006AFDF4 The value of aptr is 006AFDF4 The value of a is 7 The value of *aptr is 7 Showing that * and & are inverses of each other. &*aptr = 006AFDF4 *&aptr = 006AFDF4
13 Calling Functions by Reference Call by reference with pointer arguments Pass address of argument using & operator Allows you to change actual location in memory Arrays are not passed with & because the array name is already a pointer * operator used as alias/nickname for variable inside of function void doublenum( int *number ) } *number = 2 * ( *number ); *number used as nickname for the variable passed in When the function is called, must be passed an address doublenum( &mynum );
14 Fig. 5.4: fig05_07.cpp // Fig. 5.7: fig05_07.cpp // Cube a variable using pass-by-reference // with a pointer argument. #include <iostream> using std::cout; using std::endl; void cubebyreference( int * ); // prototype int main() int number = 5; cout << "The original value of number is " << number; // pass address of number to cubebyreference cubebyreference( &number ); cout << "\nthe new value of number is " << number << endl; return 0; // indicates successful termination } // end main
15 Fig. 5.4: fig05_07.cpp // Fig. 5.7: fig05_07.cpp // Cube a variable using pass-by-reference // with a pointer argument. #include <iostream> using std::cout; using std::endl; void cubebyreference( int * ); // prototype int main() int number = 5; cout << "The original value of number is " << number; // pass address of number to cubebyreference cubebyreference( &number ); cout << "\nthe new value of number is " << number << endl; return 0; // indicates successful termination } // end main Notice how the address of number is given - cubebyreference expects a pointer (an address of a variable).
16 Fig. 5.4: fig05_07.cpp // calculate cube of *nptr; modifies variable number in main void cubebyreference( int *nptr ) *nptr = *nptr * *nptr * *nptr; // cube *nptr } // end function cubebyreference
17 Fig. 5.4: fig05_07.cpp // calculate cube of *nptr; modifies variable number in main void cubebyreference( int *nptr ) *nptr = *nptr * *nptr * *nptr; // cube *nptr } // end function cubebyreference Inside cubebyreference, *nptr is used (*nptr is number).
18 Fig. 5.4: fig05_07.cpp // calculate cube of *nptr; modifies variable number in main void cubebyreference( int *nptr ) *nptr = *nptr * *nptr * *nptr; // cube *nptr } // end function cubebyreference The original value of number is 5 The new value of number is 125
19 Using the Const Qualifier with Pointers const qualifier Variable cannot be changed const used when function does not need to change a variable Attempting to change a const variable is a compiler error const pointers Point to same memory location Must be initialized when declared int *const myptr = &x; Constant pointer to a non-constant int const int *myptr = &x; Non-constant pointer to a constant int const int *const Ptr = &x; Constant pointer to a constant int
20 Fig. 5.4: fig05_13.cpp // Fig. 5.13: fig05_13.cpp // Attempting to modify a constant pointer to // non-constant data. int main() int x, y; // ptr is a constant pointer to an integer that can // be modified through ptr, but ptr always points to the // same memory location. int * const ptr = &x; *ptr = 7; // allowed: *ptr is not const ptr = &y; // error: ptr is const; cannot assign new address return 0; // indicates successful termination } // end main
21 Fig. 5.4: fig05_13.cpp // Fig. 5.13: fig05_13.cpp // Attempting to modify a constant pointer to // non-constant data. int main() int x, y; // ptr is a constant pointer to an integer that can // be modified through ptr, but ptr always points to the // same memory location. int * const ptr = &x; *ptr = 7; // allowed: *ptr is not const ptr = &y; // error: ptr is const; cannot assign new address return 0; // indicates successful termination } // end main Changing *ptr is allowed - x is not a constant.
22 Fig. 5.4: fig05_13.cpp // Fig. 5.13: fig05_13.cpp // Attempting to modify a constant pointer to // non-constant data. int main() int x, y; // ptr is a constant pointer to an integer that can // be modified through ptr, but ptr always points to the // same memory location. int * const ptr = &x; *ptr = 7; // allowed: *ptr is not const ptr = &y; // error: ptr is const; cannot assign new address return 0; // indicates successful termination } // end main Changing ptr is an error - ptr is a constant pointer. Error E2024 Fig05_13.cpp 15: Cannot modify a const object in function main()
23 Bubble Sort Using Call-by-reference Implement bubblesort using pointers swap function must receive the address (using &) of the array elements array elements have call-by-value default Using pointers and the * operator, swap is able to switch the values of the actual array elements Psuedocode Initialize array print data in original order Call function bubblesort print sorted array Define bubblesort
24 Bubble Sort Using Call-by-reference sizeof Returns size of operand in bytes For arrays, sizeof returns ( the size of 1 element ) * ( number of elements ) if sizeof( int ) = 4, then int myarray[10]; cout << sizeof(myarray); will print 40 sizeof can be used with Variable names Type names Constant values
25 Fig. 5.4: fig05_15.cpp // Fig. 5.15: fig05_15.cpp // This program puts values into an array, sorts the values into // ascending order, and prints the resulting array. #include <iostream> using std::cout; using std::endl; #include <iomanip> using std::setw; void bubblesort( int *, const int ); // prototype void swap( int * const, int * const ); // prototype int main() const int arraysize = 10; int a[ arraysize ] = 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; cout << "Data items in original order\n";
26 Fig. 5.4: fig05_15.cpp // Fig. 5.15: fig05_15.cpp // This program puts values into an array, sorts the values into // ascending order, and prints the resulting array. #include <iostream> using std::cout; using std::endl; #include <iomanip> using std::setw; void bubblesort( int *, const int ); // prototype void swap( int * const, int * const ); // prototype int main() const int arraysize = 10; int a[ arraysize ] = 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; cout << "Data items in original order\n"; Bubblesort gets passed the address of array elements (pointers). The name of an array is a pointer.
27 Fig. 5.4: fig05_15.cpp for ( int i = 0; i < arraysize; i++ ) cout << setw( 4 ) << a[ i ]; bubblesort( a, arraysize ); // sort the array cout << "\ndata items in ascending order\n"; for ( int j = 0; j < arraysize; j++ ) cout << setw( 4 ) << a[ j ]; cout << endl; return 0; // indicates successful termination } // end main
28 Fig. 5.4: fig05_15.cpp // sort an array of integers using bubble sort algorithm void bubblesort( int *array, const int size ) // loop to control passes for ( int pass = 0; pass < size - 1; pass++ ) // loop to control comparisons during each pass for ( int k = 0; k < size - 1; k++ ) // swap adjacent elements if they are out of order if ( array[ k ] > array[ k + 1 ] ) swap( &array[ k ], &array[ k + 1 ] ); } // end function bubblesort // swap values at memory locations to which // element1ptr and element2ptr point void swap( int * const element1ptr, int * const element2ptr ) int hold = *element1ptr; *element1ptr = *element2ptr; *element2ptr = hold; } // end function swap
29 Fig. 5.4: fig05_15.cpp // sort an array of integers using bubble sort algorithm void bubblesort( int *array, const int size ) // loop to control passes for ( int pass = 0; pass < size - 1; pass++ ) // loop to control comparisons during each pass for ( int k = 0; k < size - 1; k++ ) // swap adjacent elements if they are out of order if ( array[ k ] > array[ k + 1 ] ) swap( &array[ k ], &array[ k + 1 ] ); } // end function bubblesort // swap values at memory locations to which // element1ptr and element2ptr point void swap( int * const element1ptr, int * const element2ptr ) int hold = *element1ptr; *element1ptr = *element2ptr; *element2ptr = hold; } // end function swap swap takes pointers (addresses of array elements) and dereferences them to modify the original array elements.
30 Fig. 5.4: fig05_15.cpp Data items in original order Data items in ascending order
31
32
33
34
35
Pointers and Strings Prentice Hall, Inc. All rights reserved.
Pointers and Strings 1 Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using const with Pointers Selection Sort Using Pass-by-Reference 2
More informationOutline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.
Outline Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples. 1 Introduction A pointer is a variable that contains a memory address Pointers
More informationC Pointers. 7.2 Pointer Variable Definitions and Initialization
1 7 C Pointers 7.2 Pointer Variable Definitions and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain
More informationChapter 6 - Pointers
Chapter 6 - Pointers Outline 1 Introduction 2 Pointer Variable Declarations and Initialization 3 Pointer Operators 4 Calling Functions by Reference 5 Using the const Qualifier with Pointers 6 Bubble Sort
More informationChapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 Chapter 7 Pointers Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Chapter 7 - Pointers 7.1 Introduction 7.2 Pointer Variable Definitions and Initialization
More informationOutline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary
Pointers 1 2 Outline Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary 3 Computer Memory Revisited Computers store data in memory slots Each slot has an
More informationLecture 05 POINTERS 1
Lecture 05 POINTERS 1 Pointers Powerful, but difficult to master Simulate call-by-reference Close relationship with arrays and strings Pointer Variable vs. Normal Variable Normal variables contain a specific
More informationChapter 5 - Pointers and Strings
Chapter 5 - Pointers and Strings 1 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization 5.3 Pointer Operators 5.4 Calling Functions by Reference 5.5 Using const with Pointers 5.6 Bubble
More informationChapter 5 - Pointers and Strings
Chapter 5 - Pointers and Strings 1 5.1 Introduction 2 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization 5.3 Pointer Operators 5. Calling Functions by Reference 5.5 Using const with
More informationPointers. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan
Pointers Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline 7.1 Introduction 7.2 Pointer Variable Definitions and Initialization 7.3 Pointer
More informationC Pointers Pearson Education, Inc. All rights reserved.
1 7 C Pointers 2 Addresses are given to us to conceal our whereabouts. Saki (H. H. Munro) By indirection find direction out. William Shakespeare Many things, having full reference To one consent, may work
More informationIS 0020 Program Design and Software Tools
1 Program Components in C++ 2 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions and Arrays Jan 13, 200 Modules: functionsand classes Programs use new and prepackaged
More informationOperating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan
Operating Systems 2INC0 C course Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl) Containt Pointers Definition and Initilization Ponter Operators Pointer Arithmetic and Array Calling Functions by
More informationPOINTERS. Content. Pointers. Benefits of Pointers. In this chapter, you will learn:
Content POINTERS Erkut ERDEM Hacettepe University December 2010 In this chapter, you will learn: To be able to use pointers. To be able to use pointers to pass arguments to functions using call by reference.
More informationKingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS
Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 1 ARRAYS Arrays 2 Arrays Structures of related data items Static entity (same size
More informationPointers in C. Recap: Anatomy of a Typical C Program. declarations variables functions. int main (void){ declarations; statements; return value; }
Recap: Anatomy of a Typical C Program #preprocessor directives Pointers in C BBM 101 - Introduction to Programming I Hacettepe University Fall 2016 Fuat Akal, Aykut Erdem, Erkut Erdem declarations variables
More informationArrays. Week 4. Assylbek Jumagaliyev
Arrays Week 4 Assylbek Jumagaliyev a.jumagaliyev@iitu.kz Introduction Arrays Structures of related data items Static entity (same size throughout program) A few types Pointer-based arrays (C-like) Arrays
More informationPointers and Strings Prentice Hall, Inc. All rights reserved.
Pointers and Strings 1 sizeof operator Pointer Expressions and Pointer Arithmetic Relationship Between Pointers and Arrays Arrays of Pointers Case Study: Card Shuffling and Dealing Simulation sizeof operator
More informationObjectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips
Lab6 Array I Objectivities 1. Using rand to generate random numbers and using srand to seed the random-number generator. 2. Declaring, initializing and referencing arrays. 3. The follow-up questions and
More informationChapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types
Chapter 4 - Arrays 1 4.1 Introduction 4.2 Arrays 4.3 Declaring Arrays 4.4 Examples Using Arrays 4.5 Passing Arrays to Functions 4.6 Sorting Arrays 4.7 Case Study: Computing Mean, Median and Mode Using
More informationLecture 4. Default Arguments. Set defaults in function prototype. int myfunction( int x = 1, int y = 2, int z = 3 );
Lecture 4 More on functions Default arguments Recursive functions Sorting and Searching (e.g. bubble sort, linear search, binary search) Declaration file (specification), definition file (implementation)
More informationChapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)
Chapter - Arrays 1.1 Introduction 2.1 Introduction.2 Arrays.3 Declaring Arrays. Examples Using Arrays.5 Passing Arrays to Functions.6 Sorting Arrays. Case Study: Computing Mean, Median and Mode Using Arrays.8
More informationOutline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.
Outline Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples. 1 Arrays I Array One type of data structures. Consecutive group of memory locations
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 informationCHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad
CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad Outline 1. Introduction 2. Arrays 3. Declaring Arrays 4. Examples Using Arrays 5. Multidimensional Arrays 6. Multidimensional Arrays Examples 7. Examples Using
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 informationArrays. Outline. Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays Prentice Hall, Inc. All rights reserved.
Arrays 1 Multidimensional Arrays Case Study: Computing Mean, Median and Mode Using Arrays Multidimensional Arrays 2 Multiple subscripts a[ i ][ j ] Tables with rows and columns Specify row, then column
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 information0 vs NULL // BBM 101. Introduction to Programming I. Lecture #12 C Pointers & Strings
0 vs NULL // Twitter @raysato BBM 101 Introduction to Programming I Lecture #12 C Pointers & Strings Erkut Erdem, Aykut Erdem & Aydın Kaya // Fall 2017 Last time Functions, Loops and M- D Arrays in C Switch
More informationC Pointers. sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then
1 7 C Pointers 7.7 sizeof Operator 2 sizeof Returns size of operand in bytes For arrays: size of 1 element * number of elements if sizeof( int ) equals 4 bytes, then int myarray[ 10 ]; printf( "%d", sizeof(
More informationDeclaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:
1 Programming in C Pointer Variable A variable that stores a memory address Allows C programs to simulate call-by-reference Allows a programmer to create and manipulate dynamic data structures Must be
More informationC++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.
C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2013 Fall Outline 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.5
More informationMYcsvtu Notes LECTURE 34. POINTERS
LECTURE 34 POINTERS Pointer Variable Declarations and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain
More informationFunctions and Recursion
Functions and Recursion 1 Storage Classes Scope Rules Functions with Empty Parameter Lists Inline Functions References and Reference Parameters Default Arguments Unary Scope Resolution Operator Function
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 18 - C++ Operator Overloading
Chapter 18 - C++ Operator Overloading Outline 18.1 Introduction 18.2 Fundamentals of Operator Overloading 18.3 Restrictions on Operator Overloading 18.4 Operator Functions as Class Members vs. as friend
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 informationA pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.
Lecture 9 Pointers A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables. This variable may be of type int, char,
More informationC++ PROGRAMMING SKILLS Part 4: Arrays
C++ PROGRAMMING SKILLS Part 4: Arrays Outline Introduction to Arrays Declaring and Initializing Arrays Examples Using Arrays Sorting Arrays: Bubble Sort Passing Arrays to Functions Computing Mean, Median
More informationC Pointers. CS 2060 Week 6. Prof. Jonathan Ventura
CS 2060 Week 6 1 Pointer Variables 2 Pass-by-reference 3 const pointers 4 Pointer arithmetic 5 sizeof 6 Arrays of pointers 7 Next Time Pointers The pointer is one of C s most powerful and important features.
More informationKOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 ARRAYS ~ VECTORS KOM3191 Object-Oriented Computer Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 What is an array? Arrays
More informationChapter 15 - C++ As A "Better C"
Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference
More informationWeek 3: Pointers (Part 2)
Advanced Programming (BETC 1353) Week 3: Pointers (Part 2) Dr. Abdul Kadir abdulkadir@utem.edu.my Learning Outcomes: Able to describe the concept of pointer expression and pointer arithmetic Able to explain
More informationC Functions. Object created and destroyed within its block auto: default for local variables
1 5 C Functions 5.12 Storage Classes 2 Automatic storage Object created and destroyed within its block auto: default for local variables auto double x, y; Static storage Variables exist for entire program
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 informationOperator Overloading in C++ Systems Programming
Operator Overloading in C++ Systems Programming Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. Global Functions Overloading
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 informationIntroduction to C++ Systems Programming
Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions
More informationIntroduction. W8.2 Operator Overloading
W8.2 Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. as friend Functions Overloading Stream Insertion and Extraction
More informationCHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad Outline 1. Introduction 2. Program Components in C++ 3. Math Library Functions 4. Functions 5. Function Definitions 6. Function Prototypes 7. Header Files 8.
More informationExercise 1.1 Hello world
Exercise 1.1 Hello world The goal of this exercise is to verify that computer and compiler setup are functioning correctly. To verify that your setup runs fine, compile and run the hello world example
More informationW8.2 Operator Overloading
1 W8.2 Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. as friend Functions Overloading Stream Insertion and Extraction
More informationIntroduction to C++ Introduction to C++ 1
1 What Is C++? (Mostly) an extension of C to include: Classes Templates Inheritance and Multiple Inheritance Function and Operator Overloading New (and better) Standard Library References and Reference
More informationFunctions. Function Prototypes. Function prototype is needed if the function call comes before the function definition in the program.
Functions Functions are defined as follows: return-value-type function-name( parameter-list ) { local declarations and statements Example: int square( int y ) { return y * y; 1 Function Prototypes Function
More informationComputer Programming with C++ (21)
Computer Programming with C++ (21) Zhang, Xinyu Department of Computer Science and Engineering, Ewha Womans University, Seoul, Korea zhangxy@ewha.ac.kr Classes (III) Chapter 9.7 Chapter 10 Outline Destructors
More informationOperator Overloading
Operator Overloading Introduction Operator overloading Enabling C++ s operators to work with class objects Using traditional operators with user-defined objects Requires great care; when overloading is
More informationChapter 7 C Pointers
Chapter 7 C Pointers Objectives of This Chapter Definition and Operations with Pointers Using Pointers to pass arguments as call by reference call. Using Pointers to deal with arrays and strings. Character
More informationPointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology
Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a
More informationPointers. Memory. void foo() { }//return
Pointers Pointers Every location in memory has a unique number assigned to it called it s address A pointer is a variable that holds a memory address A pointer can be used to store an object or variable
More informationChapter 3 - Functions
Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number
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 informationCSCE 206: Structured Programming in C++
CSCE 206: Structured Programming in C++ 2017 Spring Exam 3 Monday, April 17, 2017 Total - 100 Points B Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,
More informationCSCE 206: Structured Programming in C++
CSCE 206: Structured Programming in C++ 2017 Spring Exam 3 Monday, April 17, 2017 Total - 100 Points A Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam,
More informationCHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad Outline 1. Introduction 2. Program Components in C++ 3. Math Library Functions 4. Functions 5. Function Definitions 6. Function Prototypes 7. Header Files 8.
More informationProgramming for Engineers Pointers
Programming for Engineers Pointers ICEN 200 Spring 2018 Prof. Dola Saha 1 Pointers Pointers are variables whose values are memory addresses. A variable name directly references a value, and a pointer indirectly
More informationBITG 1113: POINTER LECTURE 12
BITG 1113: POINTER LECTURE 12 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the concept of pointer. 2. Write declaration and initialization of a pointer. 3. Do arithmetic
More information엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED
엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED Outline - Function Definitions - Function Prototypes - Data
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 informationChapter 3 - Functions
Chapter 3 - Functions 1 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number Generation
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 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++ Programming Lecture 11 Functions Part I
C++ Programming Lecture 11 Functions Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department Introduction Till now we have learned the basic concepts of C++. All the programs
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 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++
Chapter 3 - Functions 1 Chapter 3 - Functions 2 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3. Functions 3.5 Function Definitions 3.6 Function Prototypes 3. Header Files 3.8
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 informationPointers. 10/5/07 Pointers 1
Pointers 10/5/07 Pointers 1 10/5/07 Pointers 2 Variables Essentially, the computer's memory is made up of bytes. Each byte has an address, associated with it. 10/5/07 Pointers 3 Variable For example 1:#include
More informationC++, How to Program. Spring 2016 CISC1600 Yanjun Li 1
Chapter 6 Function C++, How to Program Deitel & Deitel Spring 2016 CISC1600 Yanjun Li 1 Function A function is a collection of statements that performs a specific task - a single, well-defined task. Divide
More information12. Pointers Address-of operator (&)
12. Pointers In earlier chapters, variables have been explained as locations in the computer's memory which can be accessed by their identifer (their name). This way, the program does not need to care
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 information1 // Fig. 6.13: time2.cpp 2 // Member-function definitions for class Time. 3 #include <iostream> Outline. 4 5 using std::cout; 6 7 #include <iomanip>
CISC11 Introduction to Computer Science Dr. McCoy Lecture 20 November, 2009. Using Default Arguments with Constructors Constructors Can specify default arguments Default constructors Defaults all arguments
More informationChapter 3 - Functions
Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Progra m Components in C++ 3.3 Ma th Libra ry Func tions 3.4 Func tions 3.5 Func tion De finitions 3.6 Func tion Prototypes 3.7 He a de r File s 3.8
More informationpointers + memory double x; string a; int x; main overhead int y; main overhead
pointers + memory computer have memory to store data. every program gets a piece of it to use as we create and use more variables, more space is allocated to a program memory int x; double x; string a;
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 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 informationClasses and Data Abstraction. Topic 5
Classes and Data Abstraction Topic 5 Introduction Object-oriented programming (OOP) Encapsulates data (attributes) and functions (behavior) into packages called classes The data and functions of a class
More information6.5 Function Prototypes and Argument Coercion
6.5 Function Prototypes and Argument Coercion 32 Function prototype Also called a function declaration Indicates to the compiler: Name of the function Type of data returned by the function Parameters the
More informationA Deeper Look at Classes
A Deeper Look at Classes Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by
More informationBITG 1113: Function (Part 2) LECTURE 5
BITG 1113: Function (Part 2) LECTURE 5 1 Learning Outcomes At the end of this lecture, you should be able to: explain parameter passing in programs using: Pass by Value and Pass by Reference. use reference
More informationC Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables
1 6 C Arrays 6.2 Arrays 2 Array Group of consecutive memory locations Same name and type To refer to an element, specify Array name + position number arrayname[ position number ] First element at position
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 informationIS 0020 Program Design and Software Tools
1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Operator Overloading, Inheritance Lecture 6 February 10, 2005 Fundamentals of Operator Overloading 2 Use operators with objects
More informationWhat have we learned about when we learned about function parameters? 1-1
What have we learned about when we learned about function parameters? 1-1 What have we learned about when we learned about function parameters? Call-by-Value also known as scalars (eg. int, double, char,
More informationIS 0020 Program Design and Software Tools
1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Multiple Inheritance July 26, 2004 22.9 Multiple Inheritance 2 Multiple inheritance Derived class has several base classes Powerful,
More informationLecture on pointers, references, and arrays and vectors
Lecture on pointers, references, and arrays and vectors pointers for example, check out: http://www.programiz.com/cpp-programming/pointers [the following text is an excerpt of this website] #include
More informationIntroduction to C++ Programming. Adhi Harmoko S, M.Komp
Introduction to C++ Programming Adhi Harmoko S, M.Komp Machine Languages, Assembly Languages, and High-level Languages Three types of programming languages Machine languages Strings of numbers giving machine
More informationMore C++ Classes. Systems Programming
More C++ Classes Systems Programming C++ Classes Preprocessor Wrapper Time Class Case Study Class Scope and Assessing Class Members Using handles to access class members Access and Utility Functions Destructors
More informationPointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island
Pointers and Structure Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Pointer Variables Each variable in a C program occupies space in
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 informationPointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015
Pointers Lecture 1 Sections 10.1-10.2 Robb T. Koether Hampden-Sydney College Wed, Jan 14, 2015 Robb T. Koether (Hampden-Sydney College) Pointers Wed, Jan 14, 2015 1 / 23 1 Pointers 2 Pointer Initialization
More informationC Pointers. 6th April 2017 Giulio Picierro
C Pointers 6th April 07 Giulio Picierro Functions Return type Function name Arguments list Function body int sum(int a, int b) { return a + b; } Return statement (return keyword
More information