Creating simple pointers
int* ptr = nullptr; // creating a null pointer
int val = 0;
int* ptr = &val;// creating a pointer referencing to a variable
*ptr = 90; // dereferencing pointer
int* ptr2 = new int; // allocation for 1 uninitialized int variable
*ptr2 = 100; // assign a value to address stored in ptr2
ptr2[0] = 100; // valid syntax but never do it this way :)
int* arptr = new int; // allocating memory for 1 uninitialized int variable
Pointers and arrays
For 1d array, the name itself is a pointer pointing to the first element of the array:
int v[]{10,200,3000};
std::cout << (v == &v[0]); // output: 1
int* ptr2 = new int[10]; // allocating memory for array of 10 int's
From the code above we consider that for allocating array we need to have pointer first.
What about 2d array. The one is actually 1d array of 1d arrays, so we need to get array of... pointers for each array.
Since we need array of pointer then we need pointer to ... pointer!
int** pPtr = new int*[10]; // array of 10 pointers to int;
for (int i = 0; i < 10; ++i)
{
*(pPtr + i) = new int[10]; // for each of pointers allocate an array of int
}
We can increment/decrement pointer:
int v[]{10,200,3000};
int* ptr = v;
++ptr;
In this case pointer refers the next item rather than next address.
Assume
v == 0x4, then
++ptr is 0x8, in other words:
newSize = oldSize + sizeof(int)*1
ptr += 2; // newSize = oldSize + sizeof(int)*2
We can subtract pointers:
int v[]{10,200,3000};
int* ptr = v;
int* ptr2 = &v[2];
int diff = ptr2 - ptr;
To get an array element you can use subscription syntax or "pointer plus offset" one:
int v[]{10,200,3000};
int* ptr = v;
*(ptr + 1) = 100; // ptr[1] = 100; which is the same as v[1] = 100
// for 2d arrays, let's create array 10x10
int** pPtr = new int*[10];
for (int i = 0; i < 10; ++i)
{
*(pPtr + i) = new int[10];
}
*(*(pPtr + 2) + 3) = 100;
std::cout << pPtr[2][3];
output: 100
the same for 3d, 4d, ...
int (*arrPtr)[10]; // pointer to an array of 10 elements
compare:
int *arrPtr[10]; // array of 10 poiters to int
Pointers and constants
const int cVar = 20;
int* const constPtr = new int(12); // const pointer to an integer variable
int const* ptrToIntConst = &cVar; // pointer to an integer constant
const int* ptrToIntConst2 = &cVar; // pointer to an integer constant
const int *const constPtrToIntConst = new int(12); // const pointer to an integer const (don't forget to assign an initial value)
const int *const constPtrToIntConst2 = &cVar; // const pointer to an integer const
int const *const constPtrToIntConst3 = new int(12);// const pointer to an integer const (don't forget to assign an initial value)
int const *const constPtrToIntConst4 = &cVar;// const pointer to an integer const
Pointers to functions
int add( int a, int b)
{
return a + b;
}
int main(int argc, char* argv[])
{
int (*fPtr)(int x, int y) = add; // or int (*fPtr)(int x, int y) = &add;
fPtr(10,20);
return 0;
}
We can create a "pointer to function" using
typedef
typedef int(*addFPtr)(int p1, int p2);
int add(int a, int b)
{
return a + b;
}
int main()
{
addFPtr afp = add;
int result = afp(1, 2);
return 0;
}
Pointers to functions can be passed to or return from a function like other types:
int getIntResult(addFPtr f, int a, int b)
{
return f(a, b);
}
void main()
{
addFPtr afp = add;
int result = getIntResult(afp, 7, 8);
return 0;
}
Pointers to member functions
class TestPointers
{
public:
int foo(int a){ return a + 2; }
int foo(int a) const { return a + 2; }
};
typedef int (TestPointers::*fooptr)(int);
typedef int (TestPointers::*constfooptr)(int) const;
int main()
{
TestPointers testPtrs;
fooptr ptr1 = &TestPointers::foo; // '&' here is necessary
int result = (testPtrs.*ptr1)(12);
constfooptr constPtr1 = &TestPointers::foo;
const TestPointers constTestPtrs;
result = (constTestPtrs.*constPtr1)(12);
return 0;
}
Комментариев нет :
Отправить комментарий