Pointers
Objective: of this lab Manual is to get hands-on experience of Pointers
in C++. Get familiar with the syntax, Errors, and implementations.
Background: When declaring a variable, it is located at a specific
location in memory, the memory address. The task of locating variables is
automatically performed by the operating system during runtime. In some cases, we need to know the address where the variable is being stored during runtime.
A variable that stores a reference
to another variable is called a pointer. We can directly access the value
stored in the variable using a pointer that points to it.
Syntax:
1. Pointer Declaration:
Syntax: Pointer_type *Pointer_name;
Example: int *Ptr1; double *Ptr2;
2. Pointer initialization:
Syntax: Pointer_name=NULL;
Pointer_name=&variable_name;
Example: int *Ptr1, var;
Ptr1=&var;
Pre-Lab: Student must
have the knowledge about memory allocation and variable storage in the memory
how to use data types and their bytes.
Assessment tools:
The assessment is according to the
student participation in the lab if the student solves the exercise, he will
get the participation mark for this lab.
Lab Assignments:
Q1: Write a C++ program
to print the address of the declared variables of any type?
Note: you can use &
special character for this task
Q2: Write a C++ program
to swap the value of two variables using pointers only?
Q3: Write a program in
C++ to change a value in an integer variable using pointers your program should
access the address of integer variable and change it by changing in the pointer
variable?
Q4: Write a C++ program
to declare integer pointer and initialize integer variable which will be accessed
by the integer variable print address of pointer and address of the integer
variable and see the difference or similarity?
Q5: Write a program in
C to add two numbers using pointers. Go to the editor
Test Data:
Input the first number:
5
Input the second
number: 6
Expected Output:
The sum of the entered
numbers are: 11
Q6: Write a program in
C to find the maximum number between two numbers using a pointer.
Test Data:
Input the first number:
5
Input the second
number: 6
Expected Output:
6 is the maximum
number.
Q7. Write a program in
C to store n elements in an array and print the elements using pointer?
element - 0 : 5
element - 1 : 7
element - 2 : 2
element - 3 : 9
element - 4 : 8
Expected Output :
The elements you
entered are :
element - 0 : 5
element - 1 : 7
element - 2 : 2
element - 3 : 9
element - 4 : 8
Q8: Write
a program in C to find the largest element in the array using pointers you are
advised to take an array of any data type and size?
Q9: Write a program in
C to calculate the length of the string using a pointer?
Input a string:
w3resource
Expected Output:
The length of the given
string :w3resource
is: 10
Q10: write a program in
C++ to print all the addresses of the elements of the array using pointers you can have an array of any size and datatype for this question?
Q11: write a program in
C++ to sum all the values of the arrays elements using pointers you can have
array of any size and datatype for this question?
#include<iostream>
#include<conio.h>
#include<string>
#include<stdio.h>
using namespace std;
int main()
{
int a[5] = {1,2,3,4,5};
int sum = 0;
int *b;
b = a;
for(int i = 0;i <=4; i++)
{
sum = *b + sum;
*b++;
}
cout<<"the summ is"<<sum<<endl;
system("pause");
return 0;
}
Q12: write a program in
C++ to update all the values of the arrays elements using pointers you can have
array of any size and datatype for this question you can take updated values
from the user using only pointers?
#include<iostream>
#include<conio.h>
#include<string>
#include<stdio.h>
using namespace std;
int main()
{
int a[5] = {1,2,3,4,5};
int *b;
b = a;
for(int i = 0;i <=4; i++)
{
*b = a[i]+1;
cout<<"the updated is"<<*b<<endl;
*b++;
}
system("pause");
return 0;
}
for Tasks Submission:
Post a Comment