Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.

13

description

3 Learning Outcomes At the end of this lecture, students are capable of: Using pointer in C Language through an understanding of microprocessor architecture

Transcript of Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.

Page 1: Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
Page 2: Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.

PointerLecture 2

Course Name: High Level Programming LanguageYear : 2010

Page 3: Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.

3

Learning Outcomes

At the end of this lecture, students are capable of:

• Using pointer in C Language through an understanding of microprocessor architecture

Page 4: Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.

4

Outline Material• Pointer and Architecture

Computer• Usage Of Pointer• Variable Pointer

Page 5: Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.

5

Pointer and Arsitektur Komputer• Pointer is an address!!!

33

k

?

Uninitialized integer pointer (pointing to a random location)

Null pointer (pointing to a NULL location or to a grounding location “0”)

integer pointer (pointing to a variable k)

Page 6: Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.

6

Pointer and Architecture Computer• Function of pointer:

– It can refer to one object now and a different object later

• Generally, a pointer in C program functions to:– Return two or more value from a function– Operate on data type string– Operate on arrays and struct– Data structure that changes size (next week)

Page 7: Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.

7

Usage Of Pointer

int x = 10;int *p;

p = &x;

*p = 20;

Pointer declaration to data type integer

& is an address operator that takes the address of x

* dereference operator takes value from p

Page 8: Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.

8

Usage Of Pointer

int x = 10;int *p;

p = &x;

p takes the address of the variable x

p

x10

Page 9: Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.

9

Usage Of Pointer

int x = 10;int *p;

p = &x;

*p = 20;

*p is the value inside the address p

p

x20

Page 10: Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.

Conclusions• Pointer variable contains address, not data value,

always remember that!• Pointer refers only to one particular object at one

time, though later, it can be altered to different object.

• Dereference Pointer is used to access a content of a particular memory allocation.

10

Page 11: Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.

11

Topic For Next Week• Variable Pointer

– Assignment: Read book of “CppEssentials.pdf” chapter 5 pages 65 until 79.

– Do the following exercises (taken from “exercises” page 80 of book “CppEssentials.pdf”):

1. Define a function to input a list of names and store them as dynamically-allocated strings in an array, and a function to output them:

void ReadNames (char *names[], const int size);void WriteNames (char *names[], const int size);

Page 12: Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.

12

Topic For Next Week

Continued.

Write another function which sorts the list using bubble sort:

void BubbleSort(char *names[], const int size);

Bubble sort involves repeated scans of the list, where during each scan adjacent items are compared and swapped if out of order. A scan that involves no swapping indicates that the list is sorted.

Page 13: Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.

13

Topic For Next Week2. Rewrite the following function using pointer

arithmetic:

char* ReverseString (char *str){

int len = strlen(str);char *result = new char[len + 1];

for (register I = 0; I < len; ++i)result[i] = str[len – i – 1];result[len] = ‘\0’;

return result;}