0% found this document useful (0 votes)
37 views

Computer Programming Lab - Pointers LAB 8

This document contains 5 programming lab exercises that demonstrate the use of pointers in C++. The first program demonstrates passing a variable by reference and modifying it using a pointer. The second program uses a pointer to traverse a string. The third program demonstrates passing a variable by reference to functions that get and double its value. The fourth program dynamically allocates an array using a pointer and inputs values. The fifth program uses pointers to reverse a string in place.

Uploaded by

omer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Computer Programming Lab - Pointers LAB 8

This document contains 5 programming lab exercises that demonstrate the use of pointers in C++. The first program demonstrates passing a variable by reference and modifying it using a pointer. The second program uses a pointer to traverse a string. The third program demonstrates passing a variable by reference to functions that get and double its value. The fourth program dynamically allocates an array using a pointer and inputs values. The fifth program uses pointers to reverse a string in place.

Uploaded by

omer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Computer Programming Lab - Pointers LAB 8

1. Prog1 2. Prog2
int main() int main()
{ {
int a=10; char *name="Ahmed";
int *p=&a; while(*name !=NULL)
*p=*p*2; cout<<*name++<<endl;
cout<<a; }
return 0;
}
3. Prog3 4. Prog4
void getNumber(int *); int main()
void doubleValue(int *); {
int main()
{ int i,n;
int number; int * p;
getNumber(&number) cout << "How many numbers would you like to
doubleValue(&number); type? ";
cout << "That value doubled is " ; cin >> i;
cout << number << endl; p= new int[i];
return 0; if (p == 0)
} cout << "Error: memory could not be allocated";
else
void getNumber(int *input) {
{ for (n=0; n<i; n++)
cout << "Enter an integer : "; {
cin >> *input; cout << "Enter number: ";
} cin >> p[n];
}
void doubleValue(int *val) cout << "You have entered: ";
{ for (n=0; n<i; n++)
*val *= 2; cout << p[n] << ", ";
} delete[] p;
}
return 0;
}
5. Prog5 Reverse a string // pointers to the beginning and ending of the string
#include <iostream> char* pfront = buffer;
#include <cstring> char* pback = buffer + strlen( buffer ) - 1;
using namespace std;
void swap( char* front, char* back ) while ( pfront < pback ) {
{ swap( pfront, pback );
char temp = *front; ++pfront; // Move front pointer forward
*front = *back; --pback; // Move back pointer backward
*back = temp; }
}
int main( ) // Display the result and exit:
{ cout << buffer<<endl;
char buffer[50]="My name is Ahmed"; return 0;
cout<<buffer<<endl; }

You might also like