Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Example 2 : Pointers. #include using namespace std; void increment _ all ( int * start, int * stop ) { int * current =

Example 2: Pointers.
#include
using namespace std;
void increment_all(int* start, int* stop){
int* current = start;
while (current != stop){
++(*current);
++current;
}
}
void print_all(const int* start, const int* stop){
const int* current = start;
while (current != stop){
cout <<*current <<'
';
++current;
}
}
int main(){
int numbers[]={10,20,30};
increment_all(numbers, numbers +3);
print_all(numbers, numbers +3);
return 0;
}
{//Assume that the address of the variable a =0015FAA0
int a;
int *aPtr;
a =7;
aPtr = &a;
cout << &a << endl;
cout << aPtr << endl;
cout << a << endl;
cout <<*aPtr << endl;
cout << &*aPtr << endl;
cout <<*&aPtr << endl;
return 0;}
2 #include
using namespace std;
int main ()
{ int num1, num2;
int *mypointer;
mypointer = &num1;
*mypointer =10;
mypointer = &num2;
*mypointer =20;
cout << "firstvalue is "<< num1<< endl;
cout << "secondvalue is "<< num2<< endl;
return 0;}
3 #include
using namespace std;
int main ()
{
int firstvalue =5, secondvalue =15;
int *p1,*p2;
p1= &firstvalue;
p2= &secondvalue;
*p1=10;
*p2=*p1;
p1= p2;
*p1=20;
cout << "firstvalue is "<< firstvalue << endl;
cout << "secondvalue is "<< secondvalue << endl;
return 0;
}
4 #include
using namespace std;
void double_it_1(int *p)
{
*p =*p *2;
}
void double_it_2(int n)
{
n = n*2;
}
int main(){
int var =10;
int *pvar = &var;
cout <<*pvar <
using namespace std;
int main(){
int *pc, c;
c =5;
cout << "Address of c (&c): "<< &c << endl;
cout << "Value of c (c): "<< c << endl << endl;
pc = &c; // Pointer pc holds the memory address of variable c
cout << "Address that pointer pc holds (pc): "<< pc << endl;
cout << "Content of the address pointer pc holds (*pc): "<<*pc << endl << endl;
c =11; // The content inside memory address &c is changed from 5 to 11.
cout << "Address pointer pc holds (pc): "<< pc << endl;
cout << "Content of the address pointer pc holds (*pc): "<<*pc << endl << endl;
*pc =2;
cout << "Address of c (&c): "<< &c << endl;
cout << "Value of c (c): "<< c << endl << endl;
return 0;
}
Example 2: Pointers.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// pointers as arguments:
#include
using namespace std;
void increment_all (int* start, int* stop)
{
int * current = start;
while (current != stop){
++(*current); // increment value pointed
++current; // increment pointer
}
}
void print_all (const int* start, const int* stop)
{
const int * current = start;
while (current != stop){
cout <<*current <<'
';
++current; // increment pointer
}
}
int main ()
{
int numbers[]={10,20,30};
increment_all (numbers,numbers+3);
print_all (numbers,numbers+3);
return 0;
}
Example 3: void Pointers
// increaser
#include
using namespace std;
void increase (void* data, int psize)
{
if ( psize == sizeof(char))
{ char* pchar; pchar=(char*)data; ++(*pchar); }
else if (psize == sizeof(int))
{ int* pint; pint=(int*)data; ++(*pint); }
}
int main ()
{
char a ='x';
int b =1602;
increase (&a,sizeof(a));
increase (&b,sizeof(b));
cout << a <<","<< b <<'
';
return 0;
}
Example 4: Pointers to functions
// pointer to functions
#include
using namespace std;
int addition (int a, int b)
{ return (a+b); }
int subtraction (int a, int b)
{ return (a-b); }
int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g =(*functocall)(x,y);
return (g);
}
int main ()
{
int m,n;
int (*minus)(int,int)= subtraction;
m = operation (7,5, addition);
n = operation (20, m, minus);
cout <
using namespace std;
const int MAX =3;
int main (){
int var[MAX]={10,100,200};
int *ptr[MAX];
for (int i =0; i < MAX; i++){
ptr[i]= &var[i]; // assign the address of integer.
}
for (int i =0; i < MAX; i++){
cout << "Value of var["<< i <<"]=";
cout <<*ptr[i]<< endl;
}
return 0;
}
Example 6: Pointer Array
#include
using namespace std;
const int MAX =4;
int main (){
char *names[MAX]={
"Zara Ali",
"Hina Ali",
"Nuha Ali",
"Sara Ali",
};
for (int i =0; i < MAX; i++){
cout << "Value of names["<< i <<"]=";
cout << names[i]<< endl;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions