Pointers in C++
We can say the pointer as a variable which holds the memory address of another variable. If one variable contains the address of another variable, the first variable is said to point to the second.
Declaring a Pointer
If a variable is holding the address of another variable then we can declare it as
var_type *varName;
Here var_type is the valid C++ Language data type and varName is the name of the pointer variable. The type of a variable defines that which variable type a pointer can hold.
There are two types of pointer operators; * and &. the & is a unary operator that returns the memory address of its operand. e.g.
vvarName = &newVar;
The * operator is the compliment of the & operator. It returns the value of the variable located at the address thta follows. e.g.
varName = *newVar;
Pointer Example
Examine the program named POINTERS.CPP for a simple example of the use of pointers.
// pointers.cpp
#include <iostream.h>
main()
{
int *pt_int;
float *pt_float;
int pig = 7, dog = 27;
float x = 1.2345, y = 32.14;
void *general;
pt_int = &pig;
*pt_int += dog;
cout << "Pig now has the value of " << *pt_int << "\n";
general = pt_int;
pt_float = &x;
y += 5 * (*pt_float);
cout << "y now has the value of " << y << "\n";
general = pt_float;
const char *name1 = "John"; // Value cannot be changed
char *const name2 = "John"; // Pointer cannot be changed
}
// Result of execution
//
// Pig now has the value of 34
// y now has the value of 38.3125
This is a pointer review and if you are comfortable with the use of pointers, you can skip this example program completely. A pointer in either ANSI-C or C++ is declared with an asterisk preceding the variable name. The pointer is then a pointer to a variable of that one specific type and should not be used with variables of other types. Thus pt_int is a pointer to an integer type variable and should not be used with any other type. Of course, an experienced C programmer knows that it is simple to coerce the pointer to be used with some other type by using a cast, but he must assume the responsibility for its correct usage.
In line 12 the pointer named pt_int is assigned the address of the variable named pig and line 13 uses the pointer named pt_int to add the value of dog to the value of pig because the asterisk dereferences the pointer in exactly the same manner as standard C. The address is used to print out the value of the variable pig in line 14 illustrating the use of a pointer with the stream output operator cout. Likewise, the pointer to float named pt_float is assigned the address of x, then used in a trivial calculation in line 18.
If you are not completely comfortable with this trivial program using pointers, you should review the use of pointers in any good C programming book or Coronado Enterprises C tutorial before proceeding on because we will assume that you have a thorough knowledge of pointers throughout the remainder of this tutorial. It is not possible to write a C program of any significant size or complexity without the use of pointers.
Pages: [Page - 1] [Page - 2] [Page - 3] [Page - 4]
Tags: C++ Programming, Memory Allocation, Pointer, Pointers
Like What you See?
Become one of the regulars by subscribing! You'll be the first to know when we add more great posts just like this. Join up by either RSS Feeds or Email Updates today!
There are 10 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.



































please discuss the function by using example. program to write quadratic equation using functions
You can find a quadratic equation solver here in this website at
http://www.mycplus.com/out.asp?CID=2&SCID=192
——– Original Message ——–
please discuss the function by using example. program to write quadratic equation using functions
This helped a lot but i want to know about inhertence and common errors using inheritance while programming in c .
please inform me as early as possible.
I knows this but plz help me by giving examples on “pointers and 2d array”and on pointer and “3d array”
——– Original Message ——–
I knows this but plz help me by giving examples on “pointers and 2d array”and on pointer and “3d array”
eg..
we see example CALL BY REFERENCE(POWER OF POINTER)
#include<stdio.h> void main() { int a,b; printf("Enter a 2value") scanf("%d",&a,&b)/*store a value in a,b*/ swap(&a,&b);/*swap the 2 value we call a function name is swap ok & send memory address hera a & b memory address send ok */ printf("%d,%d",a,b); } void swap(int *a,int*b) { int t; t=*a;/* ponter mean VALUE THAT ADDRESS */ *a=*b; *b=t; }please run the code & pl do dry run u have any problem releted pointer pl mail me
my email id-shakti_147@yahoo.co.in
It is a wonderful explaination of the concept in demand. It is one of the prime and important concepts to understand from the children point of view. My congratulations to the Author. KUDOS and Best ahead.- Dheeraj Mehrotra, National Awardee, Lucknow, INDIA
plz give some more examples. and use simple words.
Please create a program that will accept an integer and it will convert it to its word form.Note that it will accept all the integer to be enter.
Sample Output:
Enter a number:5
The Word form is: FIVE
——– Original Message ——–
Please create a program that will accept an integer and it will convert it to its word form.Note that it will accept all the integer to be enter.
Sample Output:
Enter a number:5
The Word form is: FIVE
Nice example of default params. But I was wondering. In your example of one manditory and two defaults how would you skip default 1 and pass a param to default 2.