BERC 1313 Lab 5 POINTER - 2022 - 2023 - Sem2
BERC 1313 Lab 5 POINTER - 2022 - 2023 - Sem2
PROGRAMMING FUNDAMENTAL
LAB 5: POINTER
2.
3.
1BERT S1/1 1BERT S1/2 1BERT S2/1 1BERT S2/2 1BERT S3/1
PROGRAMME/
SECTION
1BERE (Intake Sem2)
DATE 29.5.24
2. MA TIEN CHOON
NAME OF
INSTRUCTOR(S)
3. TS. IMRAN BIN HINDUSTAN
1 /6
Students' Names 2 /6
3 /6
Total Student 1 /6
Total Student 2 /6
Total Student 3 /6
1
0.0 PRE-LAB SURVEY
Tick the relevant response that describe you for each statement below:
Strongly Strongly
Agree Neutral Disagree
Agree Disagree
I able to familiarize with C pointer.
I aware the application of pointer.
I understand the relationship between
pointer and arrays.
1.0 OBJECTIVES
2.0 EQUIPMENT
Like other data values, memory addresses, or pointer values, can be stored in variables of the
appropriate type. A variable that stores an address is called a pointer variable, but is often
simply referred to as just a pointer. The definition of a pointer variable, say ptr, must specify
the type of data that ptr will point to. The asterisk before the variable name indicates that
ptr is a pointer variable, and the int data type indicates that ptr can only be used to point
1
to, or hold addresses of, integer variables. This definition is read as “ptr is a pointer to int.” It
is also useful to think of *ptr as the “variable that ptr points to.” With this view, the
definition of ptr just given can be read as “the variable that ptr points to has type int.”
Because the asterisk (*) allows you to pass from a pointer to the variable being pointed to, it is
called the indirection operator.
An array name, without brackets and a subscript, actually represents the starting
address of the array. This means that an array name is really a pointer. Refer the following
code Example 2:
2
Example 2: Relationship between array and pointer.
From code in Example 2, if we compare the statement in line 9 and 10, the output for line 9
will display the value of the first element in array numbers. The output from line 10 will print
the address of first element of the array. The output for line 14 will display the value of the
arrays using pointer and the output for line 20 will print the address of each element in the
array.
3
4.0 PROCEDURE
Section 1: Pointer Operator & Running C Program in Command Prompt
1. Open your preferred C++ IDE, and create new file called “pointer.c”. Save your project at
the Desktop.
2. Type the following program code:
3. Compile and run your program and observe the output of the program.
4. Explain briefly, what happen in the program at line 8 – 17.
5. Modify the program so that it displays the address of the variable “num_c” and “num_d”.
6. Copy and paste your program code and submit it with your lab report.
7. Now, open your Command Prompt.
1
Section 3: Pointer Application
#include<stdio.h>
2
/* takes one integer by reference and increments the value pointed to by x */
int inc(int *x)
{
// Add Your Code Here
}
int main( )
{
int num1, num2;
int result1;
int result2;
printf("Enter two numbers separated by a space\n");
scanf("%d %d",&num1, &num2 );
printf("num1 is %d, num2 is %d\n", num1, num2);
// Add Your Code Here
}
3. Alter and complete the rest of the code to test the add, swap, decrement, and increment
functions and display each result before and after each function call to validate the
code. Then compile and run the code.
3
5.0 RESULTS
Section A
a. Explaination of line 8-17 of the program
ptr_a=&num_c; - This assigns the address of num_c to the pointer variable ptr_a.
ptr_b=ptr_a; - ptr_b in same location as ptr_a, which is the address of num_c.
printf("%d %d \n", *ptr_a, *ptr_b); - This prints the values stored at the memory
locations pointed to by ptr_a and ptr_b. Now, *ptr_a still points to num_c, while *ptr_b
points to num_d.
a. Output windows
1
Section B
a. Executable C source code
#include <stdio.h>
int main()
{
int fno;
int sno;
int *ptr1=&fno;
int *ptr2=&sno;
printf ("Pointer: Find the maximum number between two number: \n");
printf ("-------------------------------------------------------");
printf ("\n Input the first number:");
scanf ("%d", &fno);
if (*ptr1>*ptr2)
{
printf("%d is the maximum number", *ptr1);
}
else
{
printf("\n%d is the maximum number", *ptr2);
}
return 0;
2
}
b. Output window
Section C
a. Executable C source code
#include <stdio.h>
3
(*x)++;
}
int main()
{
int num1, num2;
int result1, result2;
dec(&num1);
printf("After decrementing num1: %d\n", num1);
inc(&num2);
printf("After incrementing num2:%d \n", num2);
return 0;
b. Output window
4
5
6.0 POST-LAB SURVEY
Tick the relevant response that describe you for each statement below:
S
tr
o
n D
gl is
Strongly
y Agree Neutral a
Disagree
A gr
g ee
r
e
e
I able to familiarize with C pointer.
I aware the application of pointer.
I understand the relationship between
pointer and arrays.
Note for lab invigilator: Please fill the following table if student being asked
additional question(s) or tak(s) during the laboratory session.
N Question / Task Comment & Mark ( 0 Signature
o to 5 )
1
2