Computer Programming Lab - Pointers LAB 8

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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