Switch to full style
C++ code examples
Post a reply

Passing Pointers to function example

Sat Feb 02, 2013 2:18 pm

Passing Pointers to function example :

Without Pointers :
cpp code
// This program is not intended to work correctly.  It is used
// demonstrate a common pitfall. See article for more details.

#include <stdio.h>

void AddFive(int Number)
{
Number = Number + 5;
}

void main()
{
int nMyNumber = 18;

printf("My original number is %d\n", nMyNumber);
AddFive(nMyNumber);
printf("My new number is %d\n", nMyNumber);
}


After adding pointers:
cpp code
#include <stdio.h>
void AddFive(int *Number)
{
*Number = *Number + 5;
}

void main()
{
int nMyNumber = 18;

printf("My original number is %d\n", nMyNumber);
AddFive(&nMyNumber);
printf("My new number is %d\n", nMyNumber);
}




Post a reply
  Related Posts  to : Passing Pointers to function example
 Passing an Argument to a Function by Value     -  
 Passing arrays as function parameter in java     -  
 Array Passing     -  
 Passing a Reference Variable     -  
 JSP Passing Arrays to Methods     -  
 Basic Pointers     -  
 Swap Using Pointers     -  
 Arrays using Pointers     -  
 Types of Pointers in C++     -  
 How to use pointers and what it means     -  

Topic Tags

C++ Variables