chapter 8-pointer

11
1 COMPUTER PROGRAMMING CHAPTER 8 POINTER NOORDIANA KASSIM

Upload: jay-leong

Post on 28-Jan-2015

107 views

Category:

Education


0 download

DESCRIPTION

CHAPTER 8, COMPUTER PROGRAMMING (DAE 20102)

TRANSCRIPT

Page 1: Chapter 8-Pointer

1

COMPUTER PROGRAMMING

CHAPTER 8POINTER

NOORDIANA KASSIM

Page 2: Chapter 8-Pointer

2

BASIC OF POINTER• Every byte in a computer

memory has an address• Every time a variable is

declared in our program, a certain amount of storage is allocated to the variable

• Each variable is associated with an address

• The address of the variable and a variable that holds as address is called pointer variable

Character constants

10

Pointerconstants

20

B

A

G

int a

int b

char a

char b

char c

145600

address

Variable name

MEMORY

Page 3: Chapter 8-Pointer

3

POINTER DECLARATION AND INITIALIZATION

• A symbol “*” (asterisk) is used with pointer variables to indicate that it is a pointer variable

• Example:– int *myPtr, *myPtr2;

• Two steps in initialization pointer variables

int *p=&x;

int *p;

p=&x;

int x;Step1:Define variable

Step2:Initialize variable

Page 4: Chapter 8-Pointer

4

POINTER INITIALIZATION

• int var1 = 10;

• int *pointer1;

• pointer1 = &var1;

Define the value of integer var1

Declare the pointer

Initialize the pointer 1 to the value of integer var1

You cannot simply write pointer1=var1 since pointer1 if referred to memory address while var1 referred to a value of 10

Page 5: Chapter 8-Pointer

5

Example 1#include <stdio.h> int main() {

int i,j; int *p;

/* a pointer to an integer */ p = &i;*p=5; j=i; printf("%d %d %d\n", i, j, *p); return 0;

}

*p is only point to the value of 5 but doesn’t change the memory address value

Page 6: Chapter 8-Pointer

6

/*Fun with pointers*/#include<stdio.h>#include<conio.h>int main(void){

clrscr();int a,b,c;int *p,*q,*r;a=6;b=2;p=&b;

q=p;r=&c;

p=&a;*q=8;

*r=*p;

*r=a+*q+*&c;

printf("%d\t%d\t%d\n",a,b,c);printf("%d\t%d\t%d",*p,*q,*r);return 0;

}

Example 2? ? ?

a b c

? ? ?

p q r

6 2 ?

a b c

? ?

p q r

6 2 ?

a b c

?

p q r

6 8 6

a b c

p q r

6 8 20

a b c

p q r

Page 7: Chapter 8-Pointer

7

/*Add two numbers*/#include<stdio.h>#include<conio.h>int main (void){

int a,b,r;int *pa=&a;int *pb=&b;int *pr=&r;

printf("Enter the first number:");scanf("%d",pa);printf("Enter the second number:");scanf("%d",pb);*pr=*pa+*pb;printf("\n%d+%d is %d",*pa,*pb,*pr);return 0;

}

Example 3: Add Two Numbers

Page 8: Chapter 8-Pointer

8

Example 4:Multiple pointers/*This progran shows how we can use different pointers to point to the same

data variable*/

#include<stdio.h>int main(void){

int a;int *p=&a;int *q=&a;int *r=&a;

printf("Enter a number:");scanf("%d",&a);

printf("\n%d",*p);printf("\n%d",*q);printf("\n%d",*r);

return 0;}

Page 9: Chapter 8-Pointer

9

POINTER MANIPULATIONS

Page 10: Chapter 8-Pointer

10

Pointers and arrays

• An array is actually a pointer to the 0th element of the array

• There are some differences between arrays and pointers

• We can modify the values in the array, but not the array itself

• Statements like arr ++ are illegal, but arr[n] ++ is legal

Array Access Pointer Equivalent

arr[0] *arr

arr[2] *(arr + 2)

arr[n] *(arr + n)

Page 11: Chapter 8-Pointer

11

Pointer arithmetic • You can perform a limited number of arithmetic

operations on pointers. These operations are:   – Increment and decrement – Addition and subtraction – Comparison – Assignment

• The increment (++) operator increases the value of a pointer by the size of the data object the pointer refers to

• For example, if the pointer refers to the second element in an array, the ++ makes the pointer refer to the third element in the array.

• The decrement (--) operator decreases the value of a pointer by the size of the data object the pointer refers to

• For example, if the pointer refers to the second element in an array, the -- makes the pointer refer to the first element in the array.