Show pageOld revisionsBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== Pointer in C++ ====== ===== Basic concepts ===== A pointer contains the memory address of a variable. <code cpp> // Declare a pointer to a int variable. int *pnPtr; </code> ==== Address-of operator: & ==== Store the address of a variable in a pointer, by using the address of operator: <code cpp> // From value to pointer int nValue = 42; int *pnPtr = &nValue; </code> ==== Dereference operator: * ==== Given a pointer, retrieve the value. <code cpp> cout << *pnPtr; </code> ==== Null-Pointer ==== <code cpp> int *pnPtr = 0; // also possible, but this is C int *pnPtr = NULL; </code> Used in an if condition: <code cpp> if (pnPtr) cout << "pointer not null"; else cout << "null-pointer"; </code> Size of a pointer depends on the underlying OS/system (32/64 bit => 4 byte/8 byte). ===== Pointer and Arrays ===== An array is a pointer which refers to the first element of an array. <code cpp> int anArray[3] = {3, 5, 7}; cout << *anArray; // Gibt erstes Element "3" zurück </code> ==== Pointer arithmetic ==== If pnPtr points to an integer, pnPtr + 1 is the address of the next integer. pnPtr + 1 does not return the address + 1, instead it returns the address of the object of the type pnPtr points to. Example.: pnPtr points to an integer (4 bytes) pnPtr + 2 means 2 integers after pnPtr, which results in 8 adresses after pnPtr. The data type size can be returned with ''sizeof()''. //Scaling//: The compiler multiplies the integer operand with the size of the object which is pointed to. === Array example: === Given the following example: <code cpp> int anArray[3] = {3, 5, 7}; </code> anArray is a pointer to anArray[0] (it is &anArray[0]). anArray + 1 is a pointer to anArray[i]. Access to the second element of the above array: <code cpp> int anArray[3] = {3, 5, 7}; cout << *(anArray + 1); // Returns element "5" </code> ===== Dynamic memory allocation ===== ==== Einzelne Variablen ==== <code cpp> int *pnValue = new int; </code> //new//-Operator allocates space on the heap and returns the address of the variable which was allocated. Release allocated memory <code cpp> delete pnValue; // Delete memory on which the pointer points pnValue = 0; // Set null pointer </code> ==== Arrays ==== <code cpp> // Speicher allokieren int *pnArray = new int[10]; // Freigeben delete[] pnArray; pnArray = 0; </code> ==== Multidimensional arrays ==== Since an array is a pointer, a array of array can be seen as a pointer to pointers. A 2dimensional array can declared as follows: <code cpp> int x = 5; int y = 5; // int* arr would declare a pointer to a array of ints int** arr = new int*[x]; // declares a pointer to an array of pointers for(int i = 0; i < x; ++i) arr[i] = new int[y]; </code> http://stackoverflow.com/questions/8767166/passing-a-2d-array-to-a-c-function ===== const ===== Konstanter Pointer: <code cpp> int nValue = 1; int *const pnPtr = &nValue; </code> nValue ist nicht konstant, und Wert von nValue kann mit *pnPtr = 2 geändert werden. ===== Referenzen ===== Konstante, die als Alias für eine andere Variable dient <code cpp> int nValue = 5; int &rnRef = nValue; nValue++; cout << rnRef; // 6 </code> Konstante (Const) Referenzen verbieten Änderung des Wertes der Referenz <code cpp> const int &rnRef = nValue; </code> ===== Member Selection ===== <code cpp> (*psSomething).nValue = 5; psSomething->nValue = 5; </code> ===== Function pointer ===== <code cpp> // Pointer f to a function, which returns void and has no parameters void (*f) (void) // Pointer f to a function, which returns a void pointer and has a void pointer as parameter void *(*f)(void *) // Function can be called like this: f(); // Pointer func2, which returns a pointer to a char and takes a pointer to a const char and an int as a parameter. char * (*func2)(const char *, int) = strchr; </code> programming_languages/cpp/pointer.txt Last modified: 2017/10/05 00:24by phreazer