Members BookmarksPolls Fresher Jobs Funny Photos B.Tech Projects New Member FAQ  



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

Advertisements


website counter



C ++ Concept-Pointers Arithmetic


Posted Date: 12 Jun 2008    Resource Type: Articles/Knowledge Sharing    Category: Computer & Technology

Posted By: Girish Patil       Member Level: Diamond
Rating:     Points: 3



Pointers Arithmetic
In C pointer arithmetic following operations are possible
1. A pointer variable can be assigned to an ordinary variable.
2. A pointer variable can be assigned to another pointer variable.
3. A pointer variable can be assigned ‘NULL’ [ i.e. 0 ].
4. Integer quantity can be added or subtracted from a pointer variable.
5. Two pointer variables can be compared.
6. Two pointer variables can not be added or subtracted from a pointer variable.

This is very confusing subject, so let us illustrate it with an example. The following function change takes two arguments; x is an int while y is a pointer to int. It changes both values.

Change(int x, int *y)
{
cout<<”starting change ; x = “ << x << ”,y = “ << *y;
x++;
(*y)++;
cout<< “\n Finishing change : x = “ << x <<” ,y = “ << *y;
}

Since y is a pointer, we must de-reference it before incrementing its value.
A very simple program to call this function might be as follows

main()
{
int I = 0, j = 0;
cout<< “ starting main : I = “ << x <<“ ,j = “ << *y;
cout<<”\n Calling change now “;
change(I , &j);
cout<<”\n Returned from change “;
cout<<”\n Finishing main : I = “ << I << ” , j = ” << *y;
}
note here how a pointer to int is created using the & operator within the call change(I , &j);
the result of running the program will look like this

Starting main : I = 0, j = 0
Calling change now
Starting change : x = 0,y = 0
Finishing change : x =1,y = 1
Returned from change
Finishing main : I = 0, j = 1

After the return from change the value of I is unchanged while j, which was passed as a pointer, has changed. To summarize, if you wish to use arguments to modify the value of variable from function, these arguments must be passed as pointers, and de-referenced within the function.
Where the value of an argument isn’t modified, the value can be passed without any worries about pointers.

Common use of pointers
1. Accessing array elements
2. Passing elements to a function when function need to change the original values
3. Passing arrays & strings
4. Obtaining memory from system
5. Creating data structures like LINK LIST, STACKS

Pointers & Arrays
There is a close relation between pointer & arrays can be find out from example
#include
void main()
{
int a[5] = { 5, 2, 5, 4, 3 };
c
}
The program displays the content of array a. the same program can be written using pointer as

#include
void main()
{
int a[5] = { 5, 2, 5, 4, 3 };
for(int j = 0; j < 5; cout<< *(a + j++] << “\n”;
}

Passing array to function
This is very simple process can be understand by example below

#include
const m = 5;

void centimeter(double *in)
{
for ( int x = 0; x < m ; x++)
*in++ *= 2.54;
}

void main()
{
double a[5] = { 5.0, 2.8, 5.9, 4.25, 3.36 };
for(int j = 0; j < 5; j++)
cout<<” a[ “ << j+1 << ” ] = ” a[j] << ” inch. \n ”;
centimeter(a);
cout<<” a[ “ << j+1 << ” ] = ” a[j] << ” centimeters. \n ”;
}

In the above example the function accepts array of values in inches and converts them to centimeter.
This process can be used to sort the array in the required manner. [The program sorts the number in ascending order]


#include
const m = 10;
void sort(double *in)
{
for(int x = 0; x < m; x++)
for( int y = x+1; y < m; y++)
if( *(in+x) > *(in+y))
{
int = *(in+x);
*(in+x) = *(in+y);
*(in+y) = z;
}
}

void main()
{
double a[m];
int j;
cout<<” Enter 10 numbers to be sorted “;
for(j = 0; j < m; j++)
cin>>a[j];
sort(a);
cout<<” The sorted Numbers are : \n “;
for(j = 0; j < m; j++)
cout<<”\na[ “<< j+1 << “]= “ << a[j];
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: C ++ Concept-Pointers
Previous Resource: C ++ Concept-Memory Management Using New and Delete
Return to Discussion Resource Index
Post New Resource
Category: Computer & Technology


Post resources and earn money!
 
Related Resources


Contact Us    Privacy Policy    Terms Of Use   

SpiderWorks Technologies Pvt Ltd. 2006 - 2007 All Rights Reserved.