-->





Translate

Showing posts with label pointers. Show all posts
Showing posts with label pointers. Show all posts

pointer multiplication c++ example source code

Leave a Comment
Write a program in c++ programming to multiply two numbers using pointers.
Make a function for multiplication that takes input from the user. 

There should be three functions and each should return a different data type int, float and double.

Prototypes for functions are
simple pointer multiplication example c++ code



This program has been tested on c++ code blocks IDE
C++ source code:
see also: pointer addition example source code

#include <iostream>
using namespace std;

int Mult(int *, int *);
float Mult1(int *, int *);
double Mult2(int *, int *);

int main(){

    int a,b;

 cout<<"Enter 1st Value:";
 cin>>a;
 cout<<"Enter 2nd Value:";
 cin>>b;
 cout<<"...............Multiplication.........." <<endl <<endl;

 cout<<"The integer Multiplication of these numbers is:"<<Mult(&a,&b)<<endl;
 cout<<"The float Multiplication of these numbers is:"<<Mult1(&a,&b)<<endl;
 cout<<"The double Multiplication of these numbers is:"<<Mult2(&a,&b)<<endl;

 cout<<endl <<endl;
 return 0;
}

int Mult(int *x, int *y){
 int Prod;
 Prod=(*x * *y);
 return Prod;
}

float Mult1(int *x, int *y){
 float Prod;
 Prod=(*x * *y);
 return Prod;
}

double Mult2(int *x, int *y){
 double Prod;
 Prod=(*x * *y);
 return Prod;
}

Program input-output
c++ simple pointer multiplication example source code

If you don't want to write prototypes then just add three methods above the main method. 

Suggestion number 1

I suggest first remove the prototypes then examine the errors the c++ compiler (IDE) will show. This will help to understand errors also. 

Suggestion number 2

We have 3 methods with all methods take two integers as arguments. Try by changing argument data types and return types and examine the different cases in which your code is working and not working. 

Always keep doing experiments with the code for fast learning.
Each and every time you face an error you learn.
Read More...

c++ pointer addition example

Leave a Comment
Write a c++ program that calculates the addition of two numbers using pointers
Program has three functions receiving two-pointers reference. 
Three functions return int, float and double sum of numbers. 

This c++ tutorial uses the following concepts.

  • pointers in c++
  • functions
  • passing reference to a function

This code has been tested on code blocks c++ compiler

C++ source code
#include <iostream>
using namespace std;

int Sum(int *, int *);
float Sum1(int *,int *);
double Sum2(int *, int *);


int main()

{
    int a,b;


 cout<<"Enter 1st Value:";
 cin>>a;

 cout<<"Enter 2nd Value:";
 cin>>b;

    cout<<"...............ADDITION.............." <<endl <<endl;

 cout<<"The integer sum of these numbers is:"<<Sum(&a,&b)<<endl;
 cout<<"The float sum of these numbers is:"<<Sum1(&a,&b)<<endl;
 cout<<"The double sum of these numbers is:"<<Sum2(&a,&b)<<endl;

 cout<<endl <<endl;

 cout<<endl <<endl;


 return 0;

}


int Sum(int *x, int *y )

{
 int sum;
 sum=(*x + *y);
 return sum;
}

float Sum1(int *x,int *y )

{
 float sum;
 sum=(*x + *y);
 return sum;
}

double Sum2(int *x, int *y )
{
 double sum;
 sum=(*x + *y);
 return sum;

}



Input-output of code
c++ pointer example to add two numbers

addition functions
two number additions using pointers in c++ programming
Find more here: c++ examples with output
github source
Read More...

c++ program to count spaces in a string using pointers

Leave a Comment
Write a c++ program that calculates a number of spaces in a string using pointers

Make a separate function that takes a pointer then using for loop it calculates all spaces.
This example covers the concept of 
C++ source code

#include <iostream>
using namespace std;
void Calculate_Spaces_alpha(char *);
int main()
{
 char string [] = "This string contains four spaces";
 cout <<"String is "<<"\n%----------------------%\n" << string << endl;
 char *str = string;
 Calculate_Spaces_alpha(str);
 return 0;
}

void Calculate_Spaces_alpha(char *ptr)
{
 int spaces = 0;
 int characters = 0;
 for ( ; ; ptr++)
 {
  if (*ptr == ' ')
   spaces++;

  if (*ptr != '\0')
   characters++;
  else
   break;
 }
 cout << "\nIn the String: "<< endl << "Characters:  "<< characters << endl <<"Spaces:  " << spaces << endl;
}


This code has been tested on the c++ Code blocks compiler
code view
count spaces in a string c++ program source code


also check more examples here: C++ Example Source Codes
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...

Learn c++ Pointers with Examples

Leave a Comment
This C++ Tutorial Covers


  • Pointers Syntax, declaration, initialization 
  • Pointers with address (&) and asterisk (*) operator
  • Errors may found while working with pointers
  • Basic example pointers C++ program
  • Relation of Asterisk and address operator

What is a Pointer in C++ Programming?
A pointer is a variable which stores or points to memory address of a variable. Pointer can access indirectly the value of a variable using its address. Both pointer and variable will be of same data type. 

Declaration of a Pointer

Like other variables pointer declares in the same way. First write the data type then its name with asterisk before pointer's variable name.
Declaration Syntax
int *ptr;
where ptr is a pointer of integer and will point to an integer address variable normally we read it from right to left as ptr is a pointer to an integer value
Its is recommend that while declaring a pointer use the short word _ptr after variable name which will increase the readability of program.

Initialization of a Pointer

Like other variables pointers can be initialize at the time of declaration or after it using assignment method.
It initialize with value 0 or NULL and corresponding data type.
For example
int *ptr, a=5; 
ptr=NULL; and ptr=0;   both have same meaning

A pointer which points to a NULL value called as a NULL Pointer.  The point to be noted here 0 is an interger value but pointer stores address only if we do int *ptr=7; or any other integer value then error occurred.  

Students should Remember that 0 is the only integer value by which  
Pointer with Unary Operator & 
To get the address the unary operator & known as address operator is used.
for example
int *ptr;
int x=5;
ptr=&x; //  assigns the address of variable x to pointer ptr
we said that ptr is a pointer pointing to memory address of variable x and indirectly pointer can access the value of variable x. 

Asterisk * Operator With Pointer

The * operator is usually know as de-refrence operator used to get the value of variable using pointer.
For Example
To get Value of a variable
int *ptr; // Don't confuse here it is pointer declaration 
 x=10;
ptr=&x;
cout<<ptr; // will print the address of x
cout<<*ptr; // will print the value of x = 10;

Using on left side

*ptr=15;
cout<<*ptr; //  now will print 15;
 now the memory address of x value has changed replaced by 15

To take input 

cin>> *ptr; // it will take input 
now the memory address of x value has changed by what value is entered

Relation between * and address operator &

Both operators cancel the each other value and the value of variable is provided as result.
For example
int *ptr, a=5;
ptr=&a;
Let address of a= 0X100; so ptr=0X100;
    cout<<*ptr<<endl;      // 5
    cout<<ptr;                    // 0X100
    cout<<*&ptr<<endl;   // 0X100
    cout<<&*ptr<<endl;   // 0X100

    cout<<*(&ptr)<<endl; // 0X100
    cout<<&(*ptr)<<endl; // 0X100

Errors while working with pointers 
  • De-referencing  a NULL Pointer is an error
           int   *nPtr=NULL;
    cout<<  *nPtr;  // Error
  • Assigning a pointer to different data type
    int n;  double *dPtr;
             dPtr=&n;  // error: cannot convert int to double
  •  Trying to de-refrence a non pointer variable
Pointers Example program which displays different ways to deal with pointers
Compiler Used CodeBlocks C++ Compiler

#include<iostream>
using namespace std;

int main()
{
    int *ptr; double a=222;
    int x=10;
    // ptr=&a; error
    ptr=&x;
    cout<<"Address of ptr =   "<<ptr<<"\n\n\n";
    cout<<"Defrefrecned to get Value of s  *ptr=   "        <<*ptr<<"\n\n\n";
    *ptr=50;

    cout<<"Changing value using *ptr=50 "<<"\n\n\n";
    cout<<"Now Defrefrecned to get Value of x  *ptr=   "<<*ptr<<"\n\n\n";

    int *nPtr=NULL;
    //error
    // cout<<*nPtr; 

    cout<<"NULL Pointer "<<&nPtr<<"\n\n\n";

    cout<<"Taking Input in pointer Using cin>> *ptr :  ";
    cin>>*ptr;

    cout<<"Input Value =  "<< *ptr<<"\n\n\n";
    return 0;
}

read also: C++ Pointer example with pass by reference

Output 
pointers in c++ programming



See also: Difference b/w pass by value and pass by reference with C++ example

Recommendation: Copy the above code, edit it and examine the output of program. It will be very helpful for good learning.

See more Simple examples here of C++ programs
Read More...