-->




const pointer in c++ with examples

Leave a Comment
What in const in C++?
Const which is short form of constant uses where we don't want to change or modify data. In programming const allow to set limit or control the code as you want. For example we pass some data to a function and want to ensure that function should not have rights to change it then const is used.

Lets discuss a scenario we have an array of size 10 and passes to a function which traverse to whole array and display its value. So to traverse completely, array size should be known in the function and it should not be modified so for good programming array size should be declare as const.


Different scenario with const pointers 

Making non constant pointer to non constant data
Its allows the full modified mode means both pointers and data can be changed or modified to each other by deference.
for example
int *iPtr, i=9;
iPtr=&9;


Making constant pointer to non constant data
int number=10;
const int *ciPtr=&number;

*ciPtr=20; allowed because number is not a const data

int number2=10;
ciPtr=&number2;  ERROR not allowed because ciPtr is constant 


Making constant pointer to constant data
const int number=10;
const *ciPtr=&number;

*ciPtr=20; ERROR ciPtr is pointing to a const value can't be changed

const int number2=10;
const *ciPtr=&number2;  ERROR ciPtr is const can't point to another address

If you have any confiusion about pointer basics read Learn pointer in c++ programming
Read More...

sizeof operator c++ tutorial with examples

Leave a Comment
Sizeof of operator is a unary operator which is used to calculate the size of a data type in bytes during a program compilation process. Pass a data type name and it will return the size in bytes.


List of Basic Data types their sizes and value ranges

Data TypeBytesMin ValueMax Value
char    1   -128127
short    2-3276832767
int    2-3276832767
long    4-21474836482147483647
double    8  10^-30810^308
float    410^-3810^38
unsigned short    2        065535
unsigned    int    2        065535
unsigned    long    4        04294967295
  bool    1True/FalseTrue/False


To understand more deeply let discuss a scenario
we have array of type integer of size 10
int array[10];
we want to calculate its size in two ways.
  1. sizeof operator in main function
  2. sizeof operator in another function in which array will be passed

Let see if the both methods returns same result or different. If different then why?
Cpp code example

#include<iostream>
using namespace std;

void calculate_size(int *);
int main()
{
    int array[10];

    cout<<"Size of array in bytes->
          main function "<<sizeof(array);
    cout<<"\n\n\n";

    calculate_size(array);

    return 0;

}

void calculate_size(int *iPtr)
{
   cout<<"Size of array in bytes-> 
     another function: "<<sizeof(iPtr)<<endl;
}


Example input/ouput of c++ program



As we can see both results are different buy why?

Lets explain it:
We know Integer takes 4 bytes in memory
we have array[10]->  10X4= 40 bytes in memory
so in main function body compiler knows that array is declared and it's size is -> sizeof(array)= 40 correct

In second result we have function with integer pointer argument. Array name is a constant pointer it will take 4 bytes that is why the result is different because we passed the array name(constant pointer) in function and within the scope of this function we have a single pointer of type integer.
address of array stored in iPtr->  iPtr=&array;

For pointers read here: Learn pointers in C++ Tutorial 
Sizeof Operator Example:
calculate number of elements in array using sizeof operator operations
We can also calculate number of elements in an array simple using two sizeof operators.
Lets discuss a Cpp code 


#include<iostream>
using namespace std;

int main()
{
    double array[10];
    int elements=sizeof(array)/sizeof(array[0]);

    cout<<"Total Elements "<<elements;
    cout<<"\n\n\n";

    return 0;

}


we have double array[10];
sizeof(array)=40
sizeof(array[0])=4
No.of Elements =sizeof(array)/sizeof(array[0])= 10


Note:
Size of data type varies from system to system specifications for 32-bit and 64 bit sizes may differ it is recommend to use sizeof operator for portability purpose.

Read more: Cpp Programming Tutorials

Read More...

c++ pointer simple example pass by reference

Leave a Comment
Write a program which passes a reference to a function and function has a pointer as argument. Keep the return type void and display result in main body. Apply any maths operation find square of a number
Use any c++ compiler: Codeblocks recommended.

If you have any confusion read this difference b/w pass by value and pass by reference 


#include<iostream>
using namespace std;

void pass_by_reference(int *);
int main()
{
    int number;

    cout<<"Enter Number to Calculate Square: ";cin>>number;
    cout<<"\n\n\n";

    pass_by_reference(&number);
    cout<<"\t\tResult: "<<number<<endl;

    return 0;

}

void pass_by_reference(int *iPtr)
{
    *iPtr=*iPtr * *iPtr;
}



Sample input Output of program
pointer argument example pass by reference


In the above example there is no need to return from function because changes will be made permanently on reference of number.
Find more examples here: C++ Simple Examples 

Read More...

Differ pass by value and reference c++ example

Leave a Comment
Pass by value in C++ Programming
In this method we call a function by passing a value. A new copy of value is made and send to functions body. Where operations are applied on new copied value.




Advantages
  • New copy is made of value after passing so if changes go wrong the original data will be save
  • It increase the security of data
Disadvantage
  • If the data is huge making too much copies may cause system over head.

Pass by reference in C++ Programming
In this method we call a function by its reference using address operator. It don't make a copy of data and pass the address of value to the function. Where operations are applied on the reference of value and changes made permanently to data.

Advantages
  • When data is large it reduce the copies of data and creates less system overhead.
Disadvantage
  • Security of data can be weak because the real information is accessible and can be changed 


C++ function example of both ways pass by value and pass by reference
In this example there are two functions one calculates a cube of number with value and other calculates cube of number with reference. With value returns result and with reference not return result.

#include<iostream>
using namespace std;

int pass_Value(int);
void pass_reference(int &);
int main()
{
    int num1,num2;

    cout<<"Enter First Number: ";cin>>num1;
    cout<<"Enter Second Number: ";cin>>num2;

    cout<<"\n\n\n";

    int result=pass_Value(num1);
    cout<<"\t\tPass By Value Result: "<<result<<endl;

    pass_reference(num2);
    cout<<"\t\tPass By Reference Result: "<<num2<<endl;

    return 0;

}

int pass_Value(int number)
{
    return (number*number*number);
}

void pass_reference(int &number)
{
    number=number*number*number;
}


Input Output Example:

see also: Learn Pointers in C++ Programming with Examples


Explanation of functions

  • First function is simple we received a value calculate its cube and return simply the new result.
  • In second function we applies changes using reference of number we sent so no need to return here. As changes are made permanently.



See More Examples Here: C++ Simple Examples
Read More...