0% found this document useful (0 votes)
10 views4 pages

Programming For Problem Solving: Experiment 9 - 2

The document provides programming tasks focused on pointer operations in C++. It includes examples for incrementing and decrementing pointers, swapping values using pointers, using a pointer to a pointer, and handling strings with pointers. Each task is accompanied by code snippets demonstrating the respective concepts.
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)
10 views4 pages

Programming For Problem Solving: Experiment 9 - 2

The document provides programming tasks focused on pointer operations in C++. It includes examples for incrementing and decrementing pointers, swapping values using pointers, using a pointer to a pointer, and handling strings with pointers. Each task is accompanied by code snippets demonstrating the respective concepts.
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/ 4

PROGRAMMING FOR PROBLEM SOLVING

EXPERIMENT 9 - 2
Tasks:

1. WAP to demonstrate different arithmetic operations on pointers.

PROGRAM : For incrementing a pointer

#include <iostream>

using namespace std;

const int MAX = 3;

int main () {

int var[MAX] = {10, 100, 200};

int *ptr;

ptr = var;

for (int i = 0; i < MAX; i++) {

cout << "Address of var[" << i << "] = ";

cout << ptr << endl;

cout << "Value of var[" << i << "] = ";

cout << *ptr << endl;

ptr++;

return 0;

}
PROGRAM: For decrementing a pointer

#include <iostream>

using namespace std;

const int MAX = 3;

int main () {

int var[MAX] = {10, 100, 200};

int *ptr;

ptr = &var[MAX-1];

for (int i = MAX; i > 0; i--) {

cout << "Address of var[" << i << "] = ";

cout << ptr << endl;

cout << "Value of var[" << i << "] = ";

cout << *ptr << endl;

ptr--;

return 0;

2. WAP to swap values of two variables using pointers.

PROGRAM:

#include<iostream.h>

#include<conio.h>

void main() {

clrscr();
int *a,*b,*temp;

cout<<“Enter value of a and b:”;

cin>>*a>>*b;

temp=a;

a=b;

b=temp;

cout<<“nAfter swapingna=”<<*a<<“nb=”<<*b;

getch();

3. WAP to demonstrate use of a pointer to pointer.

PROGRAM:

#include<bits/stdc++.h>

using namespace std;

int main()

int age = 21;

int *ptr1 = &age;

int **ptr2 = &ptr1;

cout<<"Value stored in the age variable "<<age<<"\n";

cout<<"Value accessed using single pointer "<<*ptr1<<"\n";

cout<<"Value accessed using double pointer "<<**ptr2<<"\n";

return 0;

}
4. WAP to demonstrate string handling using pointer.

PROGRAM:

#include <iostream>

#include<conio.h>

using namespace std;

int main() {

char str[20], *pt;

cout << "Pointer Example C++ Program : Print String \n";

cout << "Enter Any string [below 20 chars] : ";

cin>>str;

pt = str;

while (*pt != '\0') {

cout << *pt;

pt++;

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy