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

Size: px
Start display at page:

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

Transcription

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

2 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 What is an array? Arrays are data structures consisting of related data items of the same type. Structures and classes are each capable of holding related data items of possibly different types. Arrays, structures and classes are "static" entities in that they remain the same size throughout program execution. struct data int id_n; int age; float salary; ; int main() data employee; employee.age = 22; employee.id_n = 1; employee.salary = ;

3 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 3 Arrays 12 elements. the name of the array followed by the position number of an element in square brackets [ ] The position number is called a subscript or index The first element in an array has subscript 0 (zero). The highest subscript in array c is 11, which is 1 less than 12 the number of elements in the array. Array names follow the same conventions as other variable names, i.e., they must be identifiers.

4 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 4 Arrays A subscript must be an integer or integer expression cout << c[ 0 ] + c[ 1 ] + c[ 2 ] << endl; If a program uses an expression as a subscript, then the program evaluates the expression to determine the subscript. int a=5; int b=4; c[ a + b ] = 2; c[9]=2; subscripted array name is an lvalue it can be used on the left side of an assignment, Arrays occupy space in memory. type of the elements and the number of elements required by an array: type arrayname [ arraysize ]; Then the compiler reserves the appropriate amount of memory. The arraysize must be an integer constant greater than zero.

5 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 5 Arrays int b[5]=0; cout<<&(b[0]) cout<<&(b[1]) cout<<&(b[2]) cout<<&(b[3]) cout<<&(b[4]) 0xbfd xbfd xbfd xbfd6658c 0xbfd66590 ( ) [ ] Parentheses, subscript static_cast<type>() Unary postfix ! Unary prefix * / % Multiplicative << >> Insertion/extraction < <= > >= Relational ==!= Equality?: Conditional && Logical and Logical or = += -= *= /= %= Assignment, Comma

6 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 6 Array initializers int array1[ 5 ]; int i; for ( i = 0; i < 5; i++ ) array1[ i ] = 0; //or int array1[ 10 ] = 0 ; //remaining array elements are initialized to zero int array2[ 5 ] = 32, 27, 64, 18, 95; //initializer list //or int array2[]= 32, 27, 64, 18, 95; //the compiler determines the number of elements in the array using the list // int array2[ 5 ] = 32, 27, 64, 18, 95, 14 ; gives an error. for ( int i = 0; i < 5; i++ ) cout << setw( 7 ) << i << setw( 13 ) << array2[ i ] << endl; int array3[ 5 ]=0; array3=array2; NOT OK // int *array3; array3=array2; OK

7 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 7 Notes on good use of arrays Referring to an element outside the array bounds is an execution-time logic error. It is not a syntax error. When looping through an array, the array subscript should never go below 0 and should always be less than the total number of elements in the array C++ Standard Library class template vector, which enables programmers to perform many operations that are not available for C++'s built-in arrays. For example, we will be able to compare vectors directly and assign one vector to another. This new array definition will enable us to input and output entire arrays with cin and cout. Const qualifier const int x; // Error: x must be initialized : a compilation error. x = 7; // Error: cannot modify a const variable : a compilation error. Const value as the array size vectors are more efficient Only constants can be used to declare the size of automatic and static arrays. Not using a constant for this purpose is a compilation error. compilation error. const int arraysize = 10; // constant variable indicating size of array 10 int a[ arraysize ] = 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 ;

8 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 8 Array example Top pseudo-code statement: Roll a four-sided dice 10,000 times. Track the number of occurrences of each side of a die and output these numbers First refinement Second refinement Initialize the variables Initialize array size to 4 Initialize number of rolls to 10,000 Initialize the array frequency with zeros Initialize the counter value for the rolls to zero Roll the dies a number of times, update the number of random occurrences of each side. While the counter value for the rolls is less than or equals to 10,000 roll the dice, use modulus operator with 4 update observed side s frequency Print the frequencies of the sides Initialize the side counter of the die to 1 While the side counter of the die is less than equals to the array size print frequency of the specific side of the die increment the side counter

9 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 9 Array example const int arraysize = 4; int frequency[ arraysize ] = 0 ; srand( time( 0 ) ); for ( int roll = 1; roll <= 10000; roll++ ) frequency[rand() % 4 ]++; cout << Side" << setw( 13 ) << "Frequency" << endl; for ( int sidecounter = 0; sidecounter < arraysize; sidecounter ++ ) cout << setw( 4 ) << sidecounter+1 << setw( 13 ) << frequency[sidecounter ] << endl;

10 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 10 char type arrays char string1[ ] = "first"; char string1[] = 'f', 'i', 'r', 's', 't', '\0' ; not providing a terminating null character for a string can cause logic errors. char string2[20]; cin >> string2; reads a string from the keyboard into string2 and appends the null character to the end of the string input by the user. It is the programmer's responsibility to ensure that the array into which the string is read is capable of holding any string the user types at the keyboard. By default, cin reads characters from the keyboard until the first white-space character is encountered. Thus, inputting data with cin and >> can insert data beyond the end of the array A character array representing a null-terminated string can be output with cout and <<. cout << string2 cout <<, like cin >>, does not care how large the character array is. char myarray2[5]; cin>>myarray2; // welcome cout<<myarray2; //stack smash detected./test terminated

11 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 11 Passing Arrays to Functions int hourlytemperatures[ 24 ]; the function call modifyarray( hourlytemperatures, 24 ); When passing an array to a function, the array size is normally passed as well, so the function can process the specific number of elements in the array. that the size of a vector is built in. every vector object "knows" its own size, which can be obtained by invoking the vector object's size member function. Thus, when we pass a vector object into a function, we will not have to pass the size of the vector as an argument vectors are more efficient

12 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 12 Passing Arrays to Functions Prototypes can be: void modifyarray( int [], int ); void modifyarray( int anyarrayname[], int anyvariablename ); int main() int hourlytemperatures[ 24 ]; modifyarray( hourlytemperatures, 24 ); Function definition is: void modifyarray( int b[], int sizeofarray ) for ( int k = 0; k < sizeofarray; k++ ) b[ k ] *= 2; //In main() cout<< hourlytemperatures <<endl; //In modifyarray cout<< b<<endl; 0xbf90095c 0xbf90095c indicating that modifyarray expects to receive the address of an array of integers in parameter b and the number of array elements in parameter arraysize. Because C++ passes arrays to functions by reference, when the called function uses the array name b, it will in fact be referring to the actual array in the caller

13 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 13 Passing Arrays to Functions Applying the const type qualifier to an array parameter in a function definition to prevent the original array from being modified in the function body is another example of the principle of least privilege. Functions should not be given the capability to modify an array unless it is absolutely necessary. void displayarray( const int b[], int sizeofarray ) for ( int k = 0; k < sizeofarray; k++ ) // b[ k ] *= 2; NOT ALLOWED cout<<b[k]<<endl; //OK

14 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 14 Class ~ member function example //in GradeBook.h class GradeBook public: const static int students = 10; // note public data... double getaverage(); // determine the average grade for the test... private: string coursename; int grades[ students ]; ; There are variables for which each object of a class does not have a separate copy. That is the case with static data members, which are also known as class variables. When objects of a class containing static data members are created, all the objects of that class share one copy of the class's static data members. A static data member can be accessed within the class definition and the memberfunction definitions just like any other data member. A public static data member can also be accessed outside of the class, even when no objects of the class exist, using the class name followed by the binary scope resolution operator (::) and the name of the data member.

15 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 15 Class ~ member function example //in GradeBook.cpp double GradeBook::getAverage() int total = 0; double average; for ( int grade = 0; grade < students; grade++ ) total += grades[ grade ]; average=static_cast <double >( total ) / students; cout<<average; return average; //in main() int gradesarray[ GradeBook::students ] = 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 ; GradeBook mygradebook Kom3191 OOP, gradesarray; mygradebook.getaverage(); //84.9

16 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 16 Insertion sort, selection sort Insertion sort Example: The following table shows the steps for sorting the sequence 3, 7, 4, 9, 5, 2, 6, 1. In each step, the item under consideration is underlined. The item that was moved (or left in place because it was biggest yet considered) in the previous step is shown in bold // find the exact location (amongst the previous ones) of 6 and insert it there selection sort Here is an example of this sort algorithm sorting five elements: find the smallest of and place as the 1 st number find the smallest of and place as 2 nd number

17 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 17 Bubble sort First Pass: ( ) ( ), Here, algorithm compares the first two elements, and swaps since 5 > 1. ( ) ( ), Swap since 5 > 4 ( ) ( ), Swap since 5 > 2 ( ) ( ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. Second Pass: ( ) ( ) ( ) ( ), Swap since 4 > 2 ( ) ( ) ( ) ( ) Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted. Third Pass: ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )

18 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 18 Multidimensional arrays By convention, the first identifies the element's row and the second identifies the element's column. Arrays that require two subscripts to identify a particular element are called two-dimensional arrays or 2-D arrays. Note that multidimensional arrays can have more than two dimensions a[ i ][ j ] s

19 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 19 Multidimensional arrays int b[ 2 ][ 2 ] = 1, 3, 4 initializes b[ 0 ][ 0 ] to 1, b[ 0 ][ 1 ] to 0, b[ 1 ][ 0 ] to 3 and b[ 1 ][ 1 ] to 4 int array1[ 2 ][ 3 ] = 1, 2, 3, 4, 5, 6 ; int array2[ 2 ][ 3 ] = 1, 2, 3, 4, 5 ; int array3[ 2 ][ 3 ] = 1, 2, 4 ; array1 array2 array for ( column = 0; column < 4; column++ ) a[ 2 ][ column ] = 0; total = 0; for ( row = 0; row < 3; row++ ) for ( column = 0; column < 4; column++ ) total += a[ row ][ column ];

20 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 20 C++ Standard Library Class Template vector #include <vector> void inputvector( vector< int >& array ) for ( size_t i = 0; i < array.size(); i++ ) cin >> array[ i ]; int main() vector< int > integers1( 10 ); vector< int > integers2( 10 ); cout<<integers1.size(); if ( integers1!= integers2 ) cout << "integers1 and integers2 are not equal" << endl; vector< int > integers3( integers1 ); // copy constructor integers1 = integers2; cout << "\nintegers1[5] is " << integers1[ 5 ]; integers1.at( 15 ) = 1000; // ERROR: out of range

21 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 21 POINTERS KOM3191 Object-Oriented Computer Programming

22 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 22 Pointers Pointer variables contain memory addresses as their values. aptr has the address information of the variable int *aptr; // declares the variable aptr to be type of int // it is a pointer to an int variable-value // an asterisk * must precede the pointer variable // in the declaration * is not an operator aptr addr_a addr_aptr addr_a 5 a int a=5; aptr=&a; //address operator (&) is a unary operator //it returns the memory address of its operand. //use of the & in a reference variable declaration was different Normally, a variable directly contains a specific value. However, a pointer contains the memory address of a variable that, in turn, contains a specific value. A pointer indirectly references a value Referencing a value through a pointer is often called indirection.

23 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 23 Pointers cout << *aptr << endl; *aptr=7; // The dereferenced pointer is an lvalue. cin >> *aptr; * here is indirection or dereferencing operator Dereferencing a pointer that has not been properly initialized could cause a fatal execution-time error An attempt to dereference a variable that is not a pointer is a compilation error. The address of a is : &a = The value of aptr = aptr; The value of a = *aptr The information &*aptr =*&aptr = aptr

24 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 24 Pointers ( ) [ ] Parentheses, subscript static_cast<type>() Unary postfix ! & * Unary prefix * / % Multiplicative << >> Insertion/extraction < <= > >= Relational ==!= Equality?: Conditional && Logical and Logical or = += -= *= /= %= Assignment, Comma

25 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 25 Passing arguments to functions pass-by-value return can be used to return one value from a called function to a caller pass-by-reference arguments can be passed to a function using reference arguments Reference arguments also enable programs to pass large data objects to a function and avoid the overhead of passing the objects by value pass-by-value int incrementbyref (int num) num++; return num; pass-by-reference with a pointer argument void incrementbyref2 (int *numptr) (*numptr)++; pass-by-reference with a reference argument void incrementbyref1 (int &num) num++; numptr is a pointer to an int num is a reference to an int

26 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 26 Passing arguments to functions In C++, programmers can use pointers and the indirection operator (*) to accomplish pass-by-reference When calling a function with an argument that should be modified, the address of the argument is needed. This can be normally accomplished by applying the address operator (&) to the name of the variable whose value will be modified. Arrays are not passed using operator &, because the name of the array is the starting location in memory of the array (i.e., an array name is already a pointer). The name of an array, arrayname, is equivalent to &arrayname[ 0 ]. When the address of a variable is passed to a function, the indirection operator (*) can be used in the function to form a synonym for the name of the variable this in turn can be used to modify the value of the variable at that location in the caller's memory.

27 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 27 Passing arguments to function by-reference with pointers int main() int a=5; int *aptr=&a; cout<<incrementbyvalue(a)<<endl; //6 cout<<a<<endl; // a is 5 incrementbyref1(a); cout<<a<<endl; // a is 6 incrementbyref2(aptr); cout<<a<<endl; // a is 7 incrementbyref2(&a); cout<<a<<endl; // a is 8 return 0; A function receiving an address as an argument must define a pointer parameter to receive the address int incrementbyvalue(int b) return ++b; void incrementbyref1(int &b) b++; void incrementbyref2(int* bptr) (*bptr)++;

28 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 28 Passing arguments to function by-reference with pointers In the function header and in the prototype for a function that expects a onedimensional array as an argument, the pointer notation may be used. The compiler does not differentiate between a function that receives a pointer and a function that receives a one-dimensional array. When the compiler encounters the form int b[], the compiler converts the parameter to the pointer notation int *b (pronounced "b is a pointer to an integer"). void modifyarray( int [], int ); // or void modifyarray( int *, int ); int main() int hourlytemperatures[ 24 ]; modifyarray( hourlytemperatures, 24 ); Function definition is: void modifyarray( int b[], int sizeofarray ) // or void modifyarray( int *b, int sizeofarray ) for ( int k = 0; k < sizeofarray; k++ ) b[ k ] *= 2;

29 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 29 Using const with Pointers In pass-by-value a copy of the argument (or arguments) in the function call is made and passed to the function. If the copy is modified in the function, the original value is maintained in the caller without change In pass-by-reference the value of the passed argument is modified so the function can accomplish its task. However, in some instances, the value should not be altered in the called function, called function should manipulate only a copy of the original value. void printcharacters( const char *sptr ) for ( ; *sptr!= '\0'; sptr++ ) cout << *sptr; void copyarray( const int *src, int *dest, int SIZE ) for ( int k = 0; k < sizeofarray; k++ ) dest[k] =src[k]; //src[k]=0; // NOT ALLOWED : // error: cannot modify a const object

30 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 30 (non)constant pointer to (non)constant data int x, y; int * const ptr=&x; *ptr=7; ptr=&y; // not allowed ptr is const int x=5, y; const int * const ptr=&x; *ptr=7; // not allowed *ptr is const ptr=&y; // not allowed ptr is const The const qualifier enables the programmer to inform the compiler that the value of a particular variable should not be modified. int * ptr - pointer to int const int * ptr - pointer to const int int * const ptr - const pointer to int const int * const ptr - const pointer to const int

31 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 31 sizeof operator int array[ 20 ]; int *ptr = array; cout << "\nsizeof array = " << sizeof(array); cout << "\nsizeof ptr = " << sizeof(ptr); sizeof array = 80 sizeof ptr = 4 C++ provides the unary operator sizeof to determine the size of an array (or of any other data type, variable or constant) in bytes When applied to the name of an array the sizeof operator returns the total number of bytes in the array as a value of type size_t (an alias for unsigned int on most compilers). the sizeof operator returns the size of the pointer in bytes (4), not the size of the array. sizeof(array)/sizeof(int) = 20 When writing programs that depend on data type sizes, and that will run on several computer systems, use sizeof to determine the number of bytes used to store the data types.

32 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 32 sizeof operator int a[3]=0; print(a); cout<<sizeof(a)<<endl; // void print(int a[]) cout<<sizeof(a)<<endl; // 4?? cout<<"ptr : "<<ptr<<endl; Because sizeof is a compile-time unary operator, not an run-time operator, using sizeof does not negatively impact execution performance. sizeof() gives you the size of the data type, not the size of a particular instance of that type in memory. For example, if you had a string data object that allocated a variable size character array at runtime, sizeof() could not be used to determine the size of that character array. It would only give you the size of the pointer string mystring; cin>>mystring; cout<<sizeof(mystring)<<endl; //4??

33 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 33 Pointer arithmetic int v[5]=0; int *vptr = v; // or int *vptr = &v[ 0 ]; vptr += 2; // = += allowed Pointers are valid operands in arithmetic expressions, assignment expressions and comparison expressions vptr has been initialized to point to v[ 0 ] In conventional arithmetic, the addition yields the value This is normally not the case with pointer arithmetic ( * 4) Pointer arithmetic is meaningless unless performed on a pointer that points to an array Subtracting or comparing two pointers that do not refer to elements of the same array is a logic error Using pointer arithmetic to increment or decrement a pointer such that the pointer refers to an element past the end of the array or before the beginning of the array is normally a logic error. Pointers can be compared using equality and relational operators. Comparisons using relational operators are meaningless unless the pointers point to members of the same array.

34 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 34 Pointer vs array int b[ 5 ]; // create 5-element int array b int *bptr; // create int pointer bptr Because the array name (without a subscript) is a (constant) pointer to the first element of the array, we can set bptr to the address of the first element in array b with the statement bptr = b; // assign address of array b to bptr This is equivalent to taking the address of the first element of the array as follows: bptr = &b[ 0 ]; // also assigns address of array b to bptr Array element b[ 3 ] can alternatively be referenced with the pointer expression *( bptr + 3 )

35 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 35 Pointer vs array The array name can be treated as a pointer and used in pointer arithmetic. For example, the expression *( b + 3 ) is b[ 3 ]. In general, all subscripted array expressions can be written with a pointer and an offset For example, the expression bptr[ 1 ] refers to the array element b[ 1 ]; this expression uses pointer/subscript notation. Remember that an array name is a constant pointer; it always points to the beginning of the array. Thus, the expression b += 3 // NOT ALLOWED array names are pointers to the beginning of the array and pointers can be modified in arithmetic expressions, array names cannot be modified in arithmetic expressions, because array names are constant pointers.

36 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 36 Array of pointers Arrays may contain pointers. A common use of such a data structure is to form an array of pointer-based strings, referred to simply as a string array. Each entry in the array is a string, but in C++ a string is essentially a pointer to its first character, so each entry in an array of strings is simply a pointer to the first character of a string. const char *days[ 4 ] = monday", tuesday", wednesday", thursday" ; The const char * portion of the declaration indicates that each element of array suit is of type "pointer tochar constant data Each is stored in memory as a null-terminated character string that is one character longer than the number of characters between quotes. The strings could be placed into a two-dimensional array, in which each row represents one suit and each column represents one of the letters of a suit name. Such a data structure must have a fixed number of columns per row, and that number must be as large as the largest string.

37 KOM3191 Object Oriented Programming Dr Muharrem Mercimek 37 void pointers The void pointer can be point at objects of any data type! A void pointer is declared like a normal pointer, using the void keyword as the pointer s type: void *ptrvoid; // ptrvoid is a void pointer However, because the void pointer does not know what type of object it is pointing to, it can not be dereferenced! Rather, the void pointer must first be explicitly cast to another pointer type before it is dereferenced. they effectively allow you to avoid type checking int nvalue = 5; void *pvoid = &nvalue; // can not dereference pvoid because it is a void pointer int *pint = static_cast<int*>(pvoid); // cast from void* to int* cout << *pint << endl; // can dereference pint

C++ Programming Chapter 7 Pointers

C++ 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 information

by Pearson Education, Inc. All Rights Reserved.

by 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 information

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

COMP26120: 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 information

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management

More information

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

calling 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 information

Lecture 05 POINTERS 1

Lecture 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 information

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

Chapter 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 information

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

Pointers 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 information

C++ PROGRAMMING SKILLS Part 4: Arrays

C++ 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 information

UNIT- 3 Introduction to C++

UNIT- 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 information

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

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 information

CSC 211 Intermediate Programming. Arrays & Pointers

CSC 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 information

C Pointers. 7.2 Pointer Variable Definitions and Initialization

C 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 information

Homework #3 CS2255 Fall 2012

Homework #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 information

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

Chapter 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 information

C Pointers Pearson Education, Inc. All rights reserved.

C 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 information

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Chapter 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 information

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

Output 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 information

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

C 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 information

Chapter 5 - Pointers and Strings

Chapter 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 information

IS 0020 Program Design and Software Tools

IS 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 information

Chapter 5 - Pointers and Strings

Chapter 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 information

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 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 information

MYcsvtu Notes LECTURE 34. POINTERS

MYcsvtu 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 information

Chapter 7 Array. Array. C++, How to Program

Chapter 7 Array. Array. C++, How to Program Chapter 7 Array C++, How to Program Deitel & Deitel Spring 2016 CISC 1600 Yanjun Li 1 Array Arrays are data structures containing related data items of same type. An array is a consecutive group of memory

More information

Kingdom 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 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 information

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 7/e This chapter serves as an introduction to data structures. Arrays are data structures consisting of related data items of the same type. In Chapter 10, we discuss C s notion of

More information

Chapter 6 - Pointers

Chapter 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 information

Fundamentals of Programming Session 20

Fundamentals 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 information

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2

More information

What is an algorithm?

What 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 information

Cpt S 122 Data Structures. Introduction to C++ Part II

Cpt S 122 Data Structures. Introduction to C++ Part II Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function

More information

Functions. Introduction :

Functions. Introduction : Functions Introduction : To develop a large program effectively, it is divided into smaller pieces or modules called as functions. A function is defined by one or more statements to perform a task. In

More information

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

C 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 information

Objectives. 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++ 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 information

Pointers and Strings. Adhi Harmoko S, M.Komp

Pointers and Strings. Adhi Harmoko S, M.Komp Pointers and Strings Adhi Harmoko S, M.Komp Introduction Pointers Powerful, but difficult to master Simulate call-by-reference Close relationship with arrays and strings Pointer Variable Declarations and

More information

Chapter 2: Basic Elements of C++

Chapter 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 information

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 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 information

C: How to Program. Week /Apr/23

C: How to Program. Week /Apr/23 C: How to Program Week 9 2007/Apr/23 1 Review of Chapters 1~5 Chapter 1: Basic Concepts on Computer and Programming Chapter 2: printf and scanf (Relational Operators) keywords Chapter 3: if (if else )

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ 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 information

UNIT-2 Introduction to C++

UNIT-2 Introduction to C++ UNIT-2 Introduction to C++ C++ CHARACTER SET Character set is asset of valid characters that a language can recognize. A character can represents any letter, digit, or any other sign. Following are some

More information

Arrays. Week 4. Assylbek Jumagaliyev

Arrays. 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 information

Introduction to Computer Science Midterm 3 Fall, Points

Introduction 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 information

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

Declaring 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 information

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

Pointers. 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 information

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

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

More information

Review of the C Programming Language for Principles of Operating Systems

Review of the C Programming Language for Principles of Operating Systems Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights

More information

CHAPTER 3 ARRAYS. Dr. Shady Yehia Elmashad

CHAPTER 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 information

CS201 Some Important Definitions

CS201 Some Important Definitions CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data

More information

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers C++ for Engineers and Scientists Third Edition Chapter 12 Pointers CSc 10200! Introduction to Computing Lecture 24 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this chapter you will

More information

Programming for Engineers Arrays

Programming for Engineers Arrays Programming for Engineers Arrays ICEN 200 Spring 2018 Prof. Dola Saha 1 Array Ø Arrays are data structures consisting of related data items of the same type. Ø A group of contiguous memory locations that

More information

Pointers. 10/5/07 Pointers 1

Pointers. 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 information

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana POINTERS Programación de Computadores Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana 2018-01 Pointers A pointer is a reference to

More information

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections C++ Programming Chapter 6 Arrays and Vectors Yih-Peng Chiou Room 617, BL Building (02) 3366-3603 3603 ypchiou@cc.ee.ntu.edu.tw Photonic Modeling and Design Lab. Graduate Institute of Photonics and Optoelectronics

More information

Chapter 7 C Pointers

Chapter 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 information

Exam 3 Chapters 7 & 9

Exam 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 information

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

C 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 information

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

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor Pointer in C Presented By: Pushpendra K. Rajput Assistant Professor 1 Introduction The Pointer is a Variable which holds the Address of the other Variable in same memory. Such as Arrays, structures, and

More information

Pointers in C/C++ 1 Memory Addresses 2

Pointers in C/C++ 1 Memory Addresses 2 Pointers in C/C++ Contents 1 Memory Addresses 2 2 Pointers and Indirection 3 2.1 The & and * Operators.............................................. 4 2.2 A Comment on Types - Muy Importante!...................................

More information

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

Outline. 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 information

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays) In this lecture, you will: Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of

More information

Relationship between Pointers and Arrays

Relationship between Pointers and Arrays Relationship between Pointers and Arrays Arrays and pointers are intimately related in C and often may be used interchangeably. An array name can be thought of as a constant pointer. Pointers can be used

More information

CSC 211 Intermediate Programming. Pointers

CSC 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 information

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Chapter-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 information

Object Oriented Design

Object Oriented Design Object Oriented Design Lecture 3: Introduction to C++ (Continue) Examples using declarations that eliminate the need to repeat the std:: prefix 1 Examples using namespace std; enables a program to use

More information

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

o 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 information

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CprE 288 Introduction to Embedded Systems Exam 1 Review.  1 CprE 288 Introduction to Embedded Systems Exam 1 Review http://class.ece.iastate.edu/cpre288 1 Overview of Today s Lecture Announcements Exam 1 Review http://class.ece.iastate.edu/cpre288 2 Announcements

More information

Review of the C Programming Language

Review of the C Programming Language Review of the C Programming Language Prof. James L. Frankel Harvard University Version of 11:55 AM 22-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Reference Manual for the

More information

POINTERS - 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 information

Pointers, Dynamic Data, and Reference Types

Pointers, 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 information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

BITG 1113: POINTER LECTURE 12

BITG 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

Introduction to C Final Review Chapters 1-6 & 13

Introduction to C Final Review Chapters 1-6 & 13 Introduction to C Final Review Chapters 1-6 & 13 Variables (Lecture Notes 2) Identifiers You must always define an identifier for a variable Declare and define variables before they are called in an expression

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Content. In this chapter, you will learn:

Content. In this chapter, you will learn: ARRAYS & HEAP Content In this chapter, you will learn: To introduce the array data structure To understand the use of arrays To understand how to define an array, initialize an array and refer to individual

More information

Functions and Recursion

Functions and Recursion Functions and Recursion 1 Outline Introduction Program Components in C++ Math Library Functions Functions Function Definitions Function Prototypes Header Files Random Number Generation Example: A Game

More information

Reference operator (&)

Reference 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 information

Review of Important Topics in CS1600. Functions Arrays C-strings

Review of Important Topics in CS1600. Functions Arrays C-strings Review of Important Topics in CS1600 Functions Arrays C-strings Array Basics Arrays An array is used to process a collection of data of the same type Examples: A list of names A list of temperatures Why

More information

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

C 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 information

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

Pointer 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 information

The C++ Language. Arizona State University 1

The 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 information

Objectives. In this chapter, you will:

Objectives. 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 information

What 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? 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 information

Introduction to Scientific Computing and Problem Solving

Introduction to Scientific Computing and Problem Solving Introduction to Scientific Computing and Problem Solving Lecture #22 Pointers CS4 - Introduction to Scientific Computing and Problem Solving 2010-22.0 Announcements HW8 due tomorrow at 2:30pm What s left:

More information

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

C++ 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 information

C++ for Java Programmers

C++ 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 information

CS2255 HOMEWORK #1 Fall 2012

CS2255 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 information

CS201- Introduction to Programming Current Quizzes

CS201- 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 information

Fundamentals of Programming Session 20

Fundamentals 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 information

Fundamentals of Programming Session 23

Fundamentals of Programming Session 23 Fundamentals of Programming Session 23 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 information

Operator Overloading in C++ Systems Programming

Operator 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 information

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

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 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 information

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Review: 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 information

CS2141 Software Development using C/C++ C++ Basics

CS2141 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 information

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

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below: QUIZ 1. Explain the meaning of the angle brackets in the declaration of v below: This is a template, used for generic programming! QUIZ 2. Why is the vector class called a container? 3. Explain how the

More information

Programming in C++ 5. Integral data types

Programming 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 information

CHAPTER 3 Expressions, Functions, Output

CHAPTER 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 information

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location

More information

Introduction to C++ Systems Programming

Introduction 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 information

Pointers! Arizona State University 1

Pointers! Arizona State University 1 Pointers! CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 10 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information