5 pointer
This presentation is the property of its rightful owner.
Sponsored Links
1 / 33

บทที่ 5 พอยน์เตอร์ ( Pointer) PowerPoint PPT Presentation


  • 106 Views
  • Uploaded on
  • Presentation posted in: General

บทที่ 5 พอยน์เตอร์ ( Pointer). พอยน์เตอร์ เป็นชนิดข้อมูลชนิดหนึ่งของภาษาซี มีความเร็วในการทำงานสูง ช่วยให้ การเขียน ภาษาซี มีความยืดหยุ่น การใช้งานพอยน์เตอร์ ค่อนข้างซับ ซ้อน พอยน์เตอร์เป็นจุดเด่นอย่างหนึ่งในการเขียนภาษาซี. 5.1 พอยน์เตอร์กับแอดเดรส (Pointers and Addresses).

Download Presentation

บทที่ 5 พอยน์เตอร์ ( Pointer)

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


5 pointer

5 (Pointer)


5 pointer


5 1 pointers and addresses

5.1 (Pointers and Addresses)

int i;

(Declaration) i int


5 pointer

int i;

i

400

402

404

i = 10;

10


5 pointer


5 pointer

p

p

i

360

400

400

10

i

10

10

i int

p

p i

( p i)


5 pointer

5.2

(Unary Operator) *

Indirection

Dereferencing Operator

Type *Variable-name;

Type

*

Variable-name


5 pointer

prt

chr

ip temp

int

char *prt;

int *ip , *temp;

double *dp;

ip temp


5 pointer

5.3

  • (Unary Operator) &

  • & (address)

  • *


1 int x 10 y y x 2

int count , val ,* ptr;

count =100;

ptr = &count;

val = *ptr;

1int x=10,*y;

y = &x;

2


3 int a prt b c d a 25 prt a b a c prt d prt

3

int a , *prt , b , c ,*d;

a = 25;

prt = &a;

b = a;

c = *prt;

d= prt;


Int x 1 y 2 int ip iq ip x y ip ip 0 y 5 ip y ip 3 iq ip

int x = 1, y = 2;

int *ip, *iq;

ip = &x;

y = *ip;

*ip = 0;

y = 5;

ip = &y;

*ip = 3;

iq = ip;


5 pointer

int x =1, y =2;

1

1

x

y

400

402

500

502

int *ip, *iq;

2

2

ip

iq

x int 1 2

ip iq int


5 pointer

ip = &x;

3

1

x

y

400

402

500

502

2

400

ip

iq

ip x

ip address ip

address ip 400


5 pointer

4

y = *ip;

1

1

x

y

400

402

500

502

2

400

ip

iq

y = addrerss point

ip 400 400 1


5 pointer

5

*ip = 0;

0

1

x

y

400

402

500

502

1

400

ip

iq

addrerss point 0

ip 400 400 1 0


5 pointer

6

y = 5;

5

0

x

y

400

402

500

502

1

400

ip

iq


5 pointer

7

ip = &y;

0

x

y

400

402

500

502

5

402

400

ip

iq

ip y

ip address y 402


5 pointer

*ip = 3;

3

0

x

y

400

402

500

502

5

402

ip

iq

addrerss point 3

ip 402 402 5 3

8


5 pointer

iq = ip;

0

x

y

400

402

500

502

3

402

ip

iq

402

address ip iq iq 402

iq y

9


5 4 pointers and function arguments

5.4 (Pointers and Function Arguments)

By Value (return)

2 2


5 pointer

void swap (int *px, int *py) {

int temp;

temp = *px;/* Keep x value to temp */

*px = *py;/* Assign y value to x */

*py = temp;/* Assign old x value to y */

}

5.1 2

#include <stdio.h>

void swap (int *, int *);

void main ( ) {

int x = 5, y = 10;

printf(Before swap : x = %d y = %d\n, x, y);

swap ( &x, &y); /* Pass address of x and y to swap( ) */

printf(After swap : x = %d y = %d\n, x, y);

}


5 pointer

5.4

3

input-process-output

3 - 3

-

-


5 pointer

#include <stdio.h>

void readData(int, double *);

void calAverage(double, double, double, double *);

void printData(double, double, double, double);


5 pointer

void main( ) {

double x1, x2, x3, average;

readData(1, &x1);

readData(2, &x2);

readData(3, &x3);

calAverage(x1, x2, x3, &average);

printData(x1, x2, x3, average);

}


5 pointer

void readData(int seq, double *px) {

printf("Enter value %d : ", seq);

scanf("%lf", px);

}

void calAverage(double a1, double a2, double a3, double *pAvg) {

*pAvg = (a1 + a2 + a3)/3.0;

}

void printData(double b1, double b2, double b3, double avg) {

printf("Average of %.2lf, %.2lf, %.2lf is %.2lf", b1, b2, b3, avg);

}


5 pointer

5.5 5.4 3

#include <stdio.h>

void readData(double *, double *, double *);

void calAverage(double *, double *, double *, double *);

void printData(double *, double *, double *, double *);


5 pointer

void main( ) {

double x1, x2, x3, average;

readData(&x1, &x2, &x3);

calAverage(&x1, &x2, &x3, &average);

printData(&x1, &x2, &x3, &average);

}


5 pointer

void readData(double *px1, double *px2, double *px3) {

printf("Enter value 1 : ");

scanf("%lf", px1);

printf("Enter value 2 : ");

scanf("%lf", px2);

printf("Enter value 3 : ");

scanf("%lf", px3);

}


5 pointer

void calAverage(double *pa1, double *pa2, double *pa3,

double *pAvg) {

*pAvg = (*pa1 + *pa2 + *pa3)/3.0;

}

void printData(double *pb1, double *pb2, double *pb3,

double *pAvg) {

printf("Average of %.2lf, %.2lf, %.2lf is %.2lf",

*pb1, *pb2, *pb3, *pAvg);

}


5 pointer

5.6

void calRecArea(float, float, float *);

2

3


5 pointer

void calRecArea(float w, float l, float *pArea) {

*pArea = w * l;

}

#include <stdio.h>

void main( ) {

float width, length, area;

printf("Enter width : ");

scanf("%f", &width);

printf("Enter length : ");

scanf("%f", &length);

calRecArea(width, length, &area);

printf("Rectangle area is %.2f", area);

}


5 pointer


  • Login