Lab 02 - Pointers
Lab 02 - Pointers
Outcomes:
1. See the definition of a pointer.
2. Observe the basic pointer operators.
3. See mechanism for simple pointer manipulation.
4. Pointer to arrays
1.1 Background:
There are things and there are pointers to things (Figure 1).
Figure 1
Things can come in any size; some may be big, some may be small. Pointers
come in only one size (relatively small).*
Figure 1 shows one thing: a variable named thing. The name of the variable is
written on the box that represents it. This variable contains the value 6. The
actual address of this variable is 0x1000. C++ automatically assigns an address
to each variable at compile time. The actual addresses differ from machine to
machine.
The pointer (thing_ptr) points to the variable thing. Pointers are also called
address variables since they contain the addresses of other variables. In this
case, the pointer contains the address 0x1000. Since this is the address of thing,
you say that thing_ptr points to thing.
For example:
This means countPtr is a pointer that points to a variable of type int. int * (i.e.,
a pointer to an int value) and is read (right to left), “countPtr is a pointer to
int.” Also, variable count is a simple just a variable, not a pointer to an int.
The * in the declaration applies only to countPtr. Each variable being declared
as a pointer must be preceded by an asterisk (*).
indicates that both xPtr and yPtr are pointers to double values. When * appears
in a declaration, it isn’t an operator; rather, it indicates that the variable being
declared is a pointer.
As ptr has an address of variable p, *ptr will give the value of variable p
(variable the pointer variable ptr is pointing to).
1.4 Initialization:
When you declare a pointer variable, its content is not initialized. In other words,
it contains an address of "somewhere", which is of course not a valid location.
This is dangerous! You need to initialize a pointer by assigning it a valid
address.
You can use the address-of operator to get the address of a variable, and assign
the address to a pointer variable. For example,
int number = 88; // An int variable with a value
int * pAnother = &number; // Declare another int pointer and init to address
of the variable number
Take note that pNumber stores a memory address location, whereas *pNumber
refers to the value stored in the address kept in the pointer variable, or the value
pointed to by the pointer.
The indirection operator (*) can be used in both the RHS (temp = *pNumber)
and the LHS (*pNumber = 99) of an assignment statement.
return (0);
}
1.6 Pointer has a Type Too:
A pointer is associated with a type (of the value it points to), which is specified
during declaration. A pointer can only hold an address of the declared type; it
cannot hold an address of a different type.
Example:
int i = 88;
double d = 55.66;
int * iPtr = &i; // int pointer pointing to an int value
double * dPtr = &d; // double pointer pointing to a double value
int j = 99;
iPtr = &j; // You can change the address stored in a pointer
int * iPtr;
*iPtr = 55;
cout << *iPtr << endl;
int main()
{
int myarray[5] = {1, 1, 2, 3, 5};
int* ptrvar;
ptrvar = myarray;
for(int i=0;i<5;i++)
{
cout<<*ptrvar<<"\t";
ptrvar++;
}
return 0;}
Output: 1 1 2 3 5
In the above program, we assign the array name to a pointer variable. As the
array name points to the first element in the array, we can print the contents of
the entire array by using a pointer variable and increment it using the ++
operator. This is shown in the output.
int main()
{
int myarray[5] = {2, 4,6, 8,10};
int* myptr;
myptr = myarray;
cout<<"First element in the array :"<<*myptr<<endl;
myptr ++;
cout<<"next element in the array :"<<*myptr<<endl;
myptr +=1;
cout<<"next element in the array :"<<*myptr<<endl;
myptr--;
cout<<"next element in the array :"<<*myptr<<endl;
myptr -= 1;
cout<<"next element in the array :"<<*myptr<<endl;
return 0;
}
Output:
We have seen the arithmetic operations carried out on pointers. Note that the
increment operator ++ increments the pointer and points to the next element in
the array. Similarly, the decrement operator decrements the pointer variable by
1 so that it points to the previous element in the array.
We also use + and – operators. First, we have added 1 to the pointer variable.
The result shows that it points to the next element in the array. Similarly, –
operator makes the pointer variable to point to the previous element in the array.
LAB TASKS:
int var1 = 3;
int var2 = 24;
int var3 = 17;
pointVar = &var;
int var = 5;
int* pointVar;
pointVar = &var;
cout<<ptr2<<endl;
cout<<&arr<<endl;
cout << ptr << endl;
cout << ptr-- << endl;
cout << --ptr << endl;
int main ()
{
int arr[5]={1,2,3,4,5};
display(arr);
display(arr);
}
Task # 2:
A weather station records the temperature every hour for a day, resulting in 24 temperature
readings. These readings are stored in an array. Your task is to write a C program that uses
pointers to analyze this data.
Store the 24 temperature readings in an array.
Use pointers to iterate through the array and calculate the average temperature for the
day.
Use pointers to find the highest and lowest temperatures recorded during the day.
Print the average, maximum, and minimum temperatures.
Task # 3:
You are tasked with developing a program to manage and analyze grades for students in
multiple courses. Each course has a different number of students, and you need to
dynamically allocate memory for storing grades. The program should perform several
operations, including calculating averages, finding the highest and lowest grades, and sorting
the grades.
Dynamically allocate memory to store grades for each course.
Allow the user to input grades for each course.
Calculate the average grade for each course.
Determine the highest and lowest grades in each course.
Sort the grades in ascending order for each course.
Print the average, highest, lowest, and sorted grades for each course.
Lab # 02 Marks distribution
ER1 ER6 ER8
Task 3 points 3 points 4 points