Beruflich Dokumente
Kultur Dokumente
LAB 8: Pointer
Student Name: Student ID: Section:
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.
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 variables 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
Figure 1
Value of a: Value of b:
Memory address of a: Memory address of b:
Page 5 of 11
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
main()
{
int num = 3, *numptr1, *numptr2;
numptr1 = #
numptr2 = #
printf("%d\n%d.\n",*numptr1,*numptr2);
Figure 2
Page 6 of 11
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
main()
{
int num = 3, *numptr1, *numptr2;
numptr1 = #
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
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.
#include <stdio.h>
main()
{
int x = 10,y = 20,z = 30,*xptr, *yptr, *zptr;
xptr = &x;
yptr = &y;
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);
yptr = &z;
printf("The value of yptr is %d.\n",*yptr);
}
Figure 5
2. Print screen the output.
Page 8 of 11
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
#include <stdio.h>
main()
{
int num = 89212020;
FuncByReference(&num);
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
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;
Figure 7
Page 10 of 11
Prepared By Sarveswaren May 2015
Collage of Engineering (COE) EEEB114: Programming For Engineers
Page 11 of 11
Prepared By Sarveswaren May 2015