const dataType* var;
=> pointer variable point to a const valuedataType* const var;
=> const pointer variable point to the value, Memory address of pointer can't be changedconst dataType* const var;
=> const pointer pointing to a const variable int I = 65;
char* p = (char*)&I;
dataType *ptr
=> Declare, Bad practicedataType *ptr = &var
=> Initialize, ptr is a pointer to dataTypeptr = &var
=> Assigning memory address of variable in pointer, Referencingptr
=> Gives memory value stored*ptr
=> Gives value in that memory address, Dereferencing(*ptr)++
=> Value increases by 1ptr++
=> Value increases by the size of its dataType*var = ptr
=> Copying a pointervoid *ptr
=> Void Pointerint var[N];
int *ptr = var
=> Initializing a pointer and pointing it to the first element of an arrayvar = &var[0]
=> Returns address of first memory block*var = *ptr
=> It is equal to var[0]
*(var + n) = *(ptr + n)
=> It is equal to var[n]
n[arr] = *(n + var)
*var + 1
=> Increase value of first element by 1(var + n) = (ptr + n)
=> It is the address of the nth element of the arrayptr + 1 = ptr++
=> Points to the next element of the arrayvar + 1
=> Point to the next Index of the memory address, It is an Indexing pointervar = var + 1
=> Error, var is a constant pointer so its value can not be modifiedsizeof(var) / sizeof(var[0])
=> Gives the length of the arraych var[4] = "val";
char *ptr = &ch[0]
cout << var;
=> Prints value and not the address as in case of integer arraycout << c;
=> Starts prints and stops at null pointerchar *ptr = "val"
Passing a Pinter to a Function
void func(int *p) {
// Makes a copy pointer, CHanging its address won't affect original pointer
// But changing its value will also affect the original
cout << *p << endl;
}
int main() {
int value = 5;
int *p = &value;
func(p);
}
Passing Array
void func(int arr[], int n) {
// A pointer to the array is passed to "arr" here
// Its size is 8 instead of 4 * number of elements
}
void func(int arr*, int n) {
// Same as before
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
func(arr, 5);
// Can also send a part of array
func(arr + 3, 2);
}
Return by Reference, Bad Practice, Local variable is being sent
int* functionName(int var) {
int* ptr = var;
return ptr;
}
int var = 5;
int *ptr = &var;
int **ptr2 = &ptr;
*ptr2
=> Gives the memory address that ptr is pointing at**ptr2
=> Gives value at the memory that ptr is pointing at