Pointers, Arrays and C-Strings Data Processing Course, I. Hrivnacova, IPN Orsay Variable in Memory Pointers Nullptr Pointers vs References C-Arrays C-Strings Problems with C-Arrays, C-Strings 1
Variables in Memory A computer memory location has an address and holds a content. The address is a numerical number (often expressed in hexadecimal), which is hard for programmers to use directly. Typically, each address location holds 8-bit (i.e., 1byte) of data. Chua Hock-Chuan: Programming Notes 2
Variables in Memory A variable is a named location that can store a value of a particular type. Names (or identifiers) are attached to certain addresses. Types (such as int, double, char) are associated with the contents for ease of interpretation of data. int sum = 255; sum: 255 Chua Hock-Chuan: Programming Notes 3
Pointers Pointer = an object that refers to another object The pointer value = the address of the object pointed The pointer type - formed by the type of the pointed object and * Ex. int*, float * int sum = 255; sum: 255 ptrsum: Chua Hock-Chuan: Programming Notes 4
Pointers int number = 88; int* pnumber = &number; cout << pnumber<< endl; cout << *pnumber << endl; Pointer type: int* Address operator: & *pnumber = 99; cout << *pnumber << endl; cout << number << endl; number: 255 Gives a variable address Derefence operator: * Indirectly give the variable pointed by the pointer pnumber: 5
Using Pointers int number = 88; int* pnumber = &number; cout << pnumber<< endl; cout << *pnumber << endl; *pnumber = 99; cout << *pnumber << endl; cout << number << endl; number: 255 pnumber: Program output: >./testpointers 0x7fff5a4b8c9c 88 99 99 6
Not Initialized Pointers (!) int* pnumber ; *pnumber = 55; cout << *pnumber << endl; Program output: >./testpointers Segmentation fault: 11 The pointer pnumber was declared without initialization i.e., it is pointing to "somewhere" which is of course an invalid memory location. The *pnumber = 55 corrupts the value of "somewhere"! 7
nullptr int* pnumber = nullptr; if ( pnumber ) { *pnumber = 55; cout << *pnumber << endl; } The special constant nullptr denotes a pointer which points nowhere It corresponds to boolean value false,and all other values to true We can test pointers logically 8
References A reference = is an alias, or an alternate name to an existing variable. Reference declaration: the type of object and & Ex. int&, float& int sum = 255; int& refsum = sum; You need to initialize the reference during declaration. int& ir; //!! error int& ir = nullptr; //!! error Referencing and dereferencing are done on the references implicitly. sum: refsum: References are NOT pointers No explicit dereferencing operator * and address-of operator & should be used as it is in case of pointers References are feature of C++ 9
#include <iostream> using namespace std; int main() { int number = 88; int* ptrnumber = int& refnumber = cout << "number: cout << "number: cout << "number: References vs. Pointers &number; number; " << number << endl; " << *ptrnumber << endl; " << refnumber << endl; // 88 // 88 // 88 int number2 = 99; ptrnumber = &number2; cout << "number2: " << number2 << endl; // 99 cout << "number2: " << *ptrnumber << endl; // 99 The reference acts as another name of the object, it can not be changed to point to another object, it does not occupy memory space refnumber = number2; } //!!! will set the value // of 'number2' to 'number' cout << "refnumber: " << refnumber << endl; // 99 cout << "number: " << number << endl; // 99 10
Arrays An array is a collection of several elements of the same type, arranged in a sequence Arrays are created (declared) with brackets Element access via operator [] Their index ranges from 0 to size -1 // Declare and initialize an int array of 6 elements int a[6] = {11, 22, 33, 44, 55, 66}; Chua Hock-Chuan: Programming Notes 11
Arrays & Pointers Arrays can be accessed using pointers. An array variable, a, is actually a pointer to the first element of the array // Declare and initialize an int array of 6 elements int a[6] = {11, 22, 33, 44, 55, 66}; int* ptra = a; // Modify first element a: *ptra = 77; Chua Hock-Chuan: Programming Notes 12
Pointer Arithmetics Pointers can move around all elements of an array: If an integer n is added to the pointer, the pointer is increased by n elements. The ++ operator increases by one element If we sum the pointer value and an integer n, the result points tp the nth value after the pointer If we subtract two pointers, we get the distance between the elements to which they point int numbers[6] = {11, 22, 33, 44, 55, 66}; for ( int* ptr = numbers; ptr < numbers + 6; ++ptr ) { cout << *ptr << endl; } 13
Range-based for loop Can be also used in the context of an array int numbers[6] = {11, 22, 33, 44, 55, 66}; for ( int number : numbers ) { cout << number << endl; } 14
Problems with Arrays Pitfall of C(C++): C/C++ does not perform array index-bound check. In other words, if the index is beyond the array's bounds, it does not issue a warning/error. Handling of the arrays can be dangerous, you should prefer use of vectors or std::arrays (since C++11) int numbers[6] = {11, 22, 33, 44, 55, 66}; // Index out of bound! // Can compiled and run, but could pose very serious side effect! numbers[88] = 999; cout << numbers[77] << endl; 15
C-Strings The standard string types of C++ encapsulate the details of the low-level string processing In C, strings are managed as arrays of characters with the '\0' character at the end. The length of a C-string is the number of characters until '\0' The string literals ("hello") have this low-level format, their type is const char* const means that characters cannot be changed hello : h e l l o \0 16
Problems with C-Strings const char* s = "hello"; const char* t = "Nico"; s : h e l l o \0 t : N i c o \0 h e l l o \0 t : N i c o \0 Assigns pointers rather than characters Special functions have to be used for operations with C-Strings s = t; s : to copy, compare, etc. The same danger of out of bound errors as for arrays You should prefer use of std::string 17