0% found this document useful (0 votes)
12 views

BERC 1313 Lab 5 POINTER - 2022 - 2023 - Sem2

The document discusses a laboratory experiment on pointers in the C programming language. It includes objectives to familiarize students with pointers, their applications, and the relationship between pointers and arrays. It also provides examples of pointer syntax and declarations, as well as how pointers can be used to access array elements and print their addresses.

Uploaded by

Aina Balqis
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

BERC 1313 Lab 5 POINTER - 2022 - 2023 - Sem2

The document discusses a laboratory experiment on pointers in the C programming language. It includes objectives to familiarize students with pointers, their applications, and the relationship between pointers and arrays. It also provides examples of pointer syntax and declarations, as well as how pointers can be used to access array elements and print their addresses.

Uploaded by

Aina Balqis
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

FAKULTI TEKNOLOGI DAN KEJURUTERAAN

ELEKTRONIK DAN KOMPUTER


UNIVERSITI TEKNIKAL MALAYSIA MELAKA

PROGRAMMING FUNDAMENTAL

BERC 1313 SEMESTER 2 SESI 2023/2024

LAB 5: POINTER

NO. STUDENTS' NAME MATRIC. NO.

1. AINA BALQIS BINTI MUHAMMAD SYAFIQ B122310078

2.

3.

1BERT S1/1 1BERT S1/2 1BERT S2/1 1BERT S2/2 1BERT S3/1
PROGRAMME/
SECTION
1BERE (Intake Sem2)

GROUP LAB G1 G2 G3 G4 G5 G6 G7 G8 G9 G10

DATE 29.5.24

1. DAYANASARI BINTI ABDUL HADI

2. MA TIEN CHOON
NAME OF
INSTRUCTOR(S)
3. TS. IMRAN BIN HINDUSTAN

4. TS. DR. HASRUL 'NISHAM BIN ROSLY

EXAMINER’S COMMENT(S) TOTAL MARKS


Laboratory Assessment Rubrics
BERC1313: Fundamental Programming
2023/2024 SEM II
ACTUAL
Lab Title LAB 5: POINTER Matrix ID COURSE / SECTION
MARK

1 /6

Students' Names 2 /6

3 /6

Assessment Level Assessment Criteria STUDENT 0 1 2 3 4 Weightage MARK


1 /1
P2 Ability to shows proper standard
in C program. 2 0.25 /1
(Readiness)
3 /1
1 /2
Laboratory Construct program codes that free
P3
from syntax/logic or runtime 2 0.50 /2
Experiment (Attempt) errors.
3 /2
1 /3
P4
Display the output/result of
(Basic experiment. 2 0.75 /3
Proficiency)
3 /3

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

1. To familiarize students with C pointers;


2. To expose student with the application of pointers;
3. To understand the relationship between pointer and arrays;

2.0 EQUIPMENT

1. Personal Computer / desktop


2. Software: CodeBlocks

3.0 SYNOPSIS& THEORY

Every variable in an executing program is allocated a section of memory large enough


to hold a value of that variable’s type. Current C compilers that run on PCs usually allocate a
single byte to variables of type char, two bytes to variables of type short, four bytes to
variables of type float and long, and 8 bytes to variables of type double. Each byte of memory
has a unique address. C has an address operator & that can be used to retrieve the address of
any variable. To use it, place it before the variable whose address you want. Refer Example 1

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.

Example 1: Declare pointer variable and display address of variable

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.

Section 2: Dynamic Data


1. Write a program to find the maximum number between two numbers using pointer You
can do this in three steps:
a) Declare a pointer and variable:
int fno,sno,*ptr1=&fno,*ptr2=&sno;

b) Input your numbers


scanf("%d", &fno);
c) Finally, use pointer to do the comparison and display the output
Sample output:

1
Section 3: Pointer Application

1. Open your preferred C IDE, and create new console project.


2. Type the following program:

/* demonstrate calling functions by value, by pointers, and by references */

#include<stdio.h>

/* add two numbers together and return the result */


int add(int x, int y)
{
// Add Your Code Here
}

/* take two pointer references to integers and swap their values


i.e. if *x = 5 and *y = 3 before the call to swap then after
*x = 3 and *y = 5 */
void swap(int *x, int *y)
{
int temp;
// Add Code Here
}

/* takes one integer by reference and decrements the value pointed to by x */


int dec(int *x)
{
// Add Your Code Here
}

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.

b. Modified C source code


2. #include <stdio.h>
3. int main()
4. {
5. int *ptr_a, *ptr_b;
6. int num_c=4, num_d=7;
7.
8. ptr_a=&num_c;
9. ptr_b=ptr_a;
10. printf("%d %d\n", *ptr_a, *ptr_b);
11.
12. ptr_b=&num_d;
13. printf("%d %d \n", *ptr_a, *ptr_b);
14.
15. *ptr_a=*ptr_b;
16. printf("%d %d\n", *ptr_a, *ptr_b);
17. printf("%d %d\n", num_c, *&*&*&num_d);
18.
19. printf("%d %d\n", &num_c, &num_d);
20.
21. return 0;
22. }
23.

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);

printf ("\n Input the second number:");


scanf ("%d", &sno);

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>

int add(int x, int y)


{
return x+y;
}
void swap(int *x, int *y)
{
int temp = *x;
*x =*y;
*y = temp;
}
void dec(int *x)
{
(*x)--;
}
void inc(int *x)
{

3
(*x)++;
}

int main()
{
int num1, num2;
int result1, result2;

printf("Enter two numbers separated by a space:");


scanf("%d %d", &num1, &num2);
printf("num1 is %d, num2 is %d\n", num1, num2);

result1 =add(num1, num2);


printf("Result of adding num1 and num2: %d\n", result1);

printf("Before swap: num1 =%d, num2= %d\n", num1, num2);


swap(&num1, &num2);
printf("After swap: num1 =%d, num2 =%d\n", num1, num2);

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.

7.0 ADDITIONAL (IMPROVISED) ASSESSMENT

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

You might also like