Anda di halaman 1dari 11

Collage of Engineering (COE)

EEEB114: Programming For Engineers LAB 8: Pointer

Student Name:

8.1) LEARNING OBJECTIVES

Student ID:

Section:

By the end of this lab session, you should be able to:

• Explain the concept of pointers, pointer declaration and control.

• Understand pointer operator (& and *) and parameter passing by pointers.

• Demonstrate pointer variable declarations and the use of it, including for parameter passing by reference (CO8).

8.2) PRE LAB ASSIGNMENT

8.3) BACKGROUND A pointer variable does not store a value but store the address of the memory space which contain the value i.e. it directly points to a specific memory address.

Why would we want to use pointers?

• a) To call a function by reference so that the data passed to the function can be changed inside the function.

• b) To create a dynamic data structure which can grow larger or smaller as necessary.

A pointer declaration such as:

Page 1 of 11

Prepared By Sarveswaren © May 2015

Collage of Engineering (COE)

EEEB114: Programming For Engineers int *numberPtr; which declares numberptr as a variable that points to an integer variable. Its content is a memory address. The * indicates that the variable being declared is a pointer variable instead of a normal variable.

When a pointer is created, it is not pointing to any valid memory address. Therefore, we need to assign it to a variable’s address by using the & operator. This operator is called a reference operator. For e.g.:

#include <stdio.h>

main() { int number = 20; int *numberPtr; numberPtr = &number; printf("\nnumber = %d\n", *numberPtr); } Page 2 of 11

Prepared By Sarveswaren © May 2015

Collage of Engineering (COE)

EEEB114: Programming For Engineers  The statement numberPtr = &number assigns the address of the variable number to a pointer variable numberPtr. Variable numberPtr is then said as to “point to” variable number. Observe the value in numberPtr is the address of number.

After a pointer is assigned to a particular address, the value in the pointed address can be accessed/modified using the * operator. This operator is commonly called as the indirection operator or dereferencing operator.

A function may return multiple values by declaring their formal parameters (passing value) as pointers variables. This way of passing the argument is known as call by reference. When the value referenced by the pointer is changed inside the function, the value in the actual variable will also change. Therefore, we can pass the result of the function through the function argument without having to use the return statement. When a pointer is passed to a function, we are actually passing the address of a variable to the function. Since we have the address, we can

Page 3 of 11

Prepared By Sarveswaren © May 2015

Collage of Engineering (COE)

EEEB114: Programming For Engineers directly manipulate the data in the address. In the case where a non-pointer variable is passed, the function will create another space in memory to hold the value locally while the program is inside the function. Therefore, any change to the variable inside the function will not change the actual value of the variable.

Page 4 of 11

Prepared By Sarveswaren © May 2015

Collage of Engineering (COE)

EEEB114: Programming For Engineers 8.4) IN LAB ACTIVITIES Activity A: Pointer Operations

• 1. Type and execute the program shown in Figure 1. Figure 1

• 2. Print screen the output of the program.

• 3. Based on output in step 2, complete the following:

Value of ‘a’:

Memory address of ‘a’: Value of ‘b’:

Memory address of ‘b’: • 4. Describe the function of *in line 9. ___________________________________________________________________________ ___________________________________________________________________________

Page 5 of 11

Prepared By Sarveswaren © May 2015

Collage of Engineering (COE)

EEEB114: Programming For Engineers • 5. Describe the function of ‘&’ in line 11. ___________________________________________________________________________ _________________________________________________________________________

Activity B: Pointers Operation

• 1. Copy and paste program shown in Figure 2.

#include <stdio.h>

main() { int num = 3, *numptr1, *numptr2;

numptr1 = &num; numptr2 = &num;

printf("%d\n%d.\n",*numptr1,*numptr2);

}

Figure 2

• 2. Execute the program and print screen the output obtained.

• 3. Recalling Activity A, determine the following: Value of num: Value of numptr1: Value of numptr2: Memory address of num: Memory address of numptr1: Memory address of numptr2:

________________

________________

________________

________________

________________

________________

Page 6 of 11

Prepared By Sarveswaren © May 2015

Collage of Engineering (COE)

EEEB114: Programming For Engineers Activity C: Pointers Operation

• 1. Copy and paste program shown in Figure 3.

#include <stdio.h>

main() { int num = 3, *numptr1, *numptr2;

numptr1 = &num; numptr2 = NULL;

printf("%d\n",num);

printf("%d\n",&num);

printf("%d\n",*&num);

printf("%d\n",numptr1);

printf("%d\n",&numptr1);

printf("%d\n",&*numptr1);

printf("%d\n",numptr2);

printf("%d\n",&numptr2);

printf("%d\n",&*numptr2);

}

Figure 3

• 2. Execute the program and print screen the output obtained.

• 3. Delete ‘&’ in the last statement, refer to line 20 in Figure 4. Figure 4

Page 7 of 11

Prepared By Sarveswaren © May 2015

Collage of Engineering (COE)

EEEB114: Programming For Engineers • 4. Print screen the output and explain what is the function of ‘&’ in line 20.

Activity D: Pointer Arithmetic

• 1. Copy and paste program shown in Figure 5.

#include <stdio.h>

main() { int x = 10,y = 20,z = 30,*xptr, *yptr, *zptr;

xptr = &x; yptr = &y;

printf("The value of xptr is %d.\n",*xptr); printf("The value of yptr is %d.\n",*yptr);

zptr = yptr; printf("The value of zptr is %d.\n",*zptr);

*zptr = 40; printf("The value of zptr is %d.\n",*zptr);

y = *zptr; printf("The value of y is %d.\n",y);

*zptr = *yptr + 10; printf("The value of zptr is %d.\n",*zptr);

yptr = &z; printf("The value of yptr is %d.\n",*yptr); }

• 2. Print screen the output.

Figure 5

Page 8 of 11

Prepared By Sarveswaren © May 2015

Collage of Engineering (COE)

EEEB114: Programming For Engineers Activity E: Call by Reference

• 1. Copy and paste program shown in Figure 6.

#include <stdio.h>

void FuncByReference (int *);

main() { int num = 89212020;

FuncByReference(&num);

printf("\nNumber after call: %d\n",num); printf("Address after call: %x\n",&num); }

void FuncByReference (int *numptr) { printf("\nNumber from main function:%d\n", *numptr); *numptr = 1300882525; }

• 2. Print screen the output.

Figure 6

• 3. Explain why there is a new value is returned from FuncByReference although there is no ‘return’.

Page 9 of 11

Prepared By Sarveswaren © May 2015

Collage of Engineering (COE)

EEEB114: Programming For Engineers Activity F: Function Passed by Value and Address

• 1. Copy and paste program shown in Figure 7. Execute the program and enter any values for length, width and depth (integer only).

#include <stdio.h>

main() { int length, width, depth, volume;

FuncGetInput (&length, &width, &depth); FuncCalcVolume (&length, &width, &depth, &volume); FuncDisplayVolume (&volume); }

FuncGetInput (int *length, int *width, int *depth) { printf("\nEnter the length:"); scanf("%d", &*length); printf("\nEnter the width:"); scanf("%d", &*width); printf("\nEnter the depth:"); scanf("%d", &*depth); return;

}

FuncCalcVolume (int *length, int *width, int *depth, int *volume) {

*volume = (*length)*(*width)*(*depth); return;

}

FuncDisplayVolume (int *volume) {

printf("\nThe volume is %d.\n", *volume); return;

}

Figure 7

Page 10 of 11

Prepared By Sarveswaren © May 2015

Collage of Engineering (COE)

EEEB114: Programming For Engineers • 2. Print screen the output.

8.5) STATE YOUR LEARNING CURVE Note: conclude what you’ve learned from this lab activity.

Page 11 of 11

Prepared By Sarveswaren © May 2015