0% found this document useful (0 votes)
28 views5 pages

Dam 23603 - Lab 8 (Pointers)

Uploaded by

shakirr aimann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

Dam 23603 - Lab 8 (Pointers)

Uploaded by

shakirr aimann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Pusat Pengajian

No. Mukasurat 1/5


Diploma
DAM 23603 : Edisi 1
Pengaturcaraan
No. Semakan 1
Komputer
Tajuk Ujikaji: Tarikh Efektif 23/07/2012
Pointers Tarikh Pindaan 23/07/2012

CENTER FOR DIPLOMA


STUDIES

COMPUTER PROGRAMMING

LAB SHEET INSTRUCTION

Kod Mata Pelajaran DAM 23603

Tajuk Ujikaji POINTERS

Kod Kursus 2 DAM

No. Ujikaji 7
Pusat Pengajian
No. Mukasurat 2/5
Diploma
DAM 23603 : Edisi 1
Pengaturcaraan
No. Semakan 1
Komputer
Tajuk Ujikaji: Tarikh Efektif 23/07/2012
Pointers Tarikh Pindaan 23/07/2012

AIM:
Understanding Pointers.

1.0 OBJECTIVES

1. Understand the concept of Pointers in C programming


2. To write a C programming based on the above concept

2.0 THEORY

What are Pointers?


Different from other normal variables which can store values, pointers are special variables that can
hold the address of a variable. Since they store memory address of a variable, the pointers are very
commonly said to “point to variables”. Lets try to understand the concept.

As shown in the above diagram:

 A normal variable ‘var’ has a memory address of 1001 and holds a value 50.
 A pointer variable has its own address 2047 but stores 1001, which is the address of the
Pusat Pengajian
No. Mukasurat 3/5
Diploma
DAM 23603 : Edisi 1
Pengaturcaraan
No. Semakan 1
Komputer
Tajuk Ujikaji: Tarikh Efektif 23/07/2012
Pointers Tarikh Pindaan 23/07/2012
variable ‘var’

EXAMPLE 1

#include <stdio.h>

int main(void)
{
char ch = 'c';
char *chptr = &ch;

int i = 20;
int *intptr = &i;

float f = 1.20000;
float *fptr = &f;

char *ptr = "I am a string";

printf("\n [%c], [%d], [%f], [%c], [%s]\n", *chptr, *intptr, *fptr, *ptr, ptr);

return 0;
}

OUTPUT :

[c], [20], [1.200000], [I], [I am a string]

The above code covers all the common pointers. The first three of them are very trivial now to
understand so lets concentrate on the fourth one. In the fourth example, a character pointer points
Pusat Pengajian
No. Mukasurat 4/5
Diploma
DAM 23603 : Edisi 1
Pengaturcaraan
No. Semakan 1
Komputer
Tajuk Ujikaji: Tarikh Efektif 23/07/2012
Pointers Tarikh Pindaan 23/07/2012
to a string.

In C, a string is nothing but an array of characters. So we have no staring pointers in C. Its the
character pointers that are used in case of strings too.

Now, coming to the string, when we point a pointer to a string, by default it holds the address of the
first character of the string. Lets try to understand it better.

The string, ‘I am String’ in memory is placed as :

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

I a m S t r i n g \0

Since characters occupy one byte each, so they are placed like above in the memory. Note the last
character, its a null character which is placed at the end of every string by default in C. This null
character signifies the end of the string.

_____________________________________________________________________________

3.0 QUESTION

LAB EXERCISE QUESTIONS


Write a full C program that will produce output as per below. You are required to use pointer

val[0]: value is 11 and address is 88820


val[1]: value is 22 and address is 88824
val[2]: value is 33 and address is 88828
val[3]: value is 44 and address is 88832
val[4]: value is 55 and address is 88836
Pusat Pengajian
No. Mukasurat 5/5
Diploma
DAM 23603 : Edisi 1
Pengaturcaraan
No. Semakan 1
Komputer
Tajuk Ujikaji: Tarikh Efektif 23/07/2012
Pointers Tarikh Pindaan 23/07/2012
val[5]: value is 66 and address is 88840
val[6]: value is 77 and address is 88844

You might also like