Its an intermediate guide on C++(Pointers)
MIT License
Its an intermediate guide series on C++
<data_type> *<ptrvar_name>
Function Declaration:
<return_data_type> function_name (data_type_1 * , data_type_2 * ,......, data_type_n *);
Function Definition:
<return_data_type> function_name (data_type * formal_argument_1,data_type * formal_argument_2,...., data_type * formal_argument_n)
{
///Function Body
}
Function Call:
function_name (&actual_argument_1, &actual_argument_2,...., &actual_argument_n)
Function Definition:
return_type function_name(datatype pointer_variable){
//function body
}
such as :
int lengthOfDigits(int *number){
//function body
}
Then , in main() function either we call :
function_name(address_operator variable_name);
Such as:
lengthOfDigits(&i);
Or We assign :
data_type pointer_variable = address_operator variable_name;
function_name(pointer_variable);
Such as:
int *ptr = &i;
lengthOfDigits(ptr);
i.e. *ptr holds address of variable i and it fetch the value when it is called. Note Both the way, it satisfies function declararion :
Function Declaration:
<return_data_type> function_name (data_type_1 * , data_type_2 * ,......, data_type_n *);
Recursion:
<return_data_type> function_name (data_type *ptr_var){
data_type var = *ptr_var;
return function_name (&var);
};
Such as:
int display(int *i){
int j = *i;
display(&j);
}
int *ptr_var;
int var;
char var_1;
ptr_var = &var;
But It cannot be:(Invalid)
ptr_var = &var_1; //Invalid
(As ptr_var is int type pointer variable cannot store address address of character variable's address(Conversion Error:
Cannot convert char* to int *)
void *vd_ptr;
int var;
char var_1;
vd_var = &var; //valid
vd_var = &var_1; //valid
Invalid and Illegal Use of Pointers Examples:
-----------------------------------------------
int a , b , *p, *q;
p = -q; //Illegal use of pointers [uniary minus]
p = p << 1; //Illegal use of pointers [int* to int conversion]
p = p - q ; // Invalid :Nonportable pointer conversion
p = p - q - a; // Invalid :Nonportable pointer conversion
p = p + a; //Invalid Pointer Addition
p = p + q;//Invalid Pointer Addition
p = p * q; // Illegal use of Pointer
p = p * a; //Illegal use of Pointer
p = p / q ; //Illegal use of Pointer
p = p / b ; //Illegal use of Pointer
p = a / p ; //Illegal use of Pointer
--------
ReturnType(*PtrToFn)(arguments_if_any)
Invoking a Function using Pointers:
----------------------------------
(*PtrToFn)(arguments_if_any)
or
PtrToFn(arguments_if_any)
Syntax: const <dataType>* <var>;
is same as :
<dataType> const * <var>
Such as:
const float *ptrpi;
float pi = 3.14f;
float area = 0.14f;
ptrpi = π //Allowed
ptrpi = &area; //Allowed
ptrpi = ptrpi+1;// i.e. Pointer Arithmetic is Allowed
*ptrpi = 10.15 ; // Trying to change value using address as a reference is not allowed as Object (ptrpi) is constant.
Similarly,
const float *ptrpi; is same as float const *ptrpi;
Syntax: <dataType>* const <var>; Constant Pointer To An Object which store address of a <dataType> Variable.
Such as : - int * const pi ; Constant Pointer To An Object which store address of Integer Variable(Stored Address is Constant)
but Object(pi) is not Constant.
Hence we can change values like :
int * const pi;
int i = 10;
int j = 20;
pi = &i;
*pi = 30;
*pi = 40;
Output will be 40.
But if we assign another address to the pointer i.e.
pi = &j;
, it will refuse and generate error as it(pointer) is constant
i.e. it's assigned address is fixed and constant.
Also,
pi= pi+1 ; // Arithmetic Operation on Pointer Address is not Allowed and Possible here as Address is Fixed and Constant
,hence it will again refuse and generate error.
Syntax: const <dataType> const * <var>; Constant Pointer To A Constant Object.
Here we cannot change values as Object is constant .
Such as :
const int* const pi; Constant Pointer To An Object(pi) which store address of Integer Variable(Stored Address is Constant) and the Object(pi) is also Constant.
int i = 10;
*pi = &i;
Then *pi = 20 is not possible. It Output 10 only, as pointers to const cannot be used to change the value of variable as Object (pi) is constant.
pi = &j; It refuses again(not possible) and generate error, as Address assigned at first to Pointer Variable is fixed and Constant.
pi = pi + 1; It refuses again and generate error, as Arithmetic Operation on Pointer Address is not Allowed and Possible here as
Stored Address under Pointer Variable(Which is a constant object) is Fixed and Constant.