pointers, arrays and c-strings - ipn · i. hrivnacova @ data processing course 2019 16 c-strings...
TRANSCRIPT
I. Hrivnacova @ Data Processing Course 2021 1
Pointers, Arrays and C-Strings
Data Processing Course, I. Hrivnacova, IJCLab Orsay
● Variable in Memory● Pointers● Nullptr● Pointers vs References● C-Arrays● C-Strings● Problems with C-Arrays, C-Strings
I. Hrivnacova @ Data Processing Course 2021 2
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., 1-byte) of data.
Chua Hock-Chuan: Programming Notes
I. Hrivnacova @ Data Processing Course 2021 3
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
I. Hrivnacova @ Data Processing Course 2021 4
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
I. Hrivnacova @ Data Processing Course 2021 5
Pointers
number:255
pNumber:
● Pointer type: int* ● Address operator: &
– Gives a variable address
● Derefence operator: *– Indirectly give the variable
pointed by the pointer
int number = 88;int* pNumber = &number; cout << pNumber<< endl; cout << *pNumber << endl;
*pNumber = 99; cout << *pNumber << endl;cout << number << endl;
I. Hrivnacova @ Data Processing Course 2021 6
Using Pointers
number:255
pNumber:
int number = 88;int* pNumber = &number; cout << pNumber<< endl; cout << *pNumber << endl;
*pNumber = 99; cout << *pNumber << endl;cout << number << endl;
Program output:> ./testPointers0x7fff5a4b8c9c889999
I. Hrivnacova @ Data Processing Course 2021 7
Not Initialized Pointers (!)
● 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"!
int* pNumber ;*pNumber = 55;cout << *pNumber << endl;
Program output:> ./testPointersSegmentation fault: 11
I. Hrivnacova @ Data Processing Course 2021 8
nullptr (C++11)
● 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
int* pNumber = nullptr;if ( pNumber ) { *pNumber = 55; cout << *pNumber << endl;}
I. Hrivnacova @ Data Processing Course 2021 9
References
● A reference = is an alias, or an alternate name to an existing variable.
● Reference declaration: the type of object and &– Ex. int&, float&
sum:
refSum:
int sum = 255;int& refSum = sum;
● References are NOT pointers
● You need to initialize the reference during declaration.
int& ir; // !! error
int& ir = nullptr; // !! error
● Referencing and dereferencing are done on the references implicitly.– No explicit dereferencing operator
* and address-of operator & should be used as it is in case of pointers
● References are feature of C++
I. Hrivnacova @ Data Processing Course 2021 10
References vs. Pointers
● 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
#include <iostream>using namespace std;
int main() { int number = 88; int* ptrNumber = &number; int& refNumber = number; cout << "number: " << number << endl; // 88 cout << "number: " << *ptrNumber << endl; // 88 cout << "number: " << refNumber << endl; // 88 int number2 = 99; ptrNumber = &number2; cout << "number2: " << number2 << endl; // 99 cout << "number2: " << *ptrNumber << endl; // 99
refNumber = number2; // !!! will set the value // of 'number2' to 'number' cout << "refNumber: " << refNumber << endl; // 99 cout << "number: " << number << endl; // 99}
I. Hrivnacova @ Data Processing Course 2021 11
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 elementsint a[6] = {11, 22, 33, 44, 55, 66};
Chua Hock-Chuan: Programming Notes
I. Hrivnacova @ Data Processing Course 2021 12
a:
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 elementsint a[6] = {11, 22, 33, 44, 55, 66};int* ptrA = a;// Modify first element*ptrA = 77;
Chua Hock-Chuan: Programming Notes
I. Hrivnacova @ Data Processing Course 2021 13
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;}
I. Hrivnacova @ Data Processing Course 2021 14
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;}
I. Hrivnacova @ Data Processing Course 2021 15
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;
I. Hrivnacova @ Data Processing Course 2021 16
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
‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’
“hello”:
I. Hrivnacova @ Data Processing Course 2021 17
Problems with C-Strings
‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’“s”:
‘N’ ‘i’ ‘c’ ‘o’ ‘\0’“t”:
const char* s = "hello";const char* t = "Nico";
s = t;
‘h’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’“s”:
‘N’ ‘i’ ‘c’ ‘o’ ‘\0’“t”:
● Assigns pointers rather than characters
● Special functions have to be used for operations with C-Strings– to copy, compare, etc.
● The same danger of “out of bound” errors as for arrays
● You should prefer use of std::string