ตัวแปรประเภทตัวชี้ (pointer)

Post on 06-Feb-2016

55 views

Category:

Documents

Embed Size (px)

DESCRIPTION

(Pointer). http://www.renvi.src.ku.ac.th. . . 4. 6. Today Agenda. pointer?. . 1. 5. 2. . , . 3. Pointer ?. - PowerPoint PPT Presentation

TRANSCRIPT

(Structure)

(Pointer)http://www.renvi.src.ku.ac.th

Today Agendapointer?12, 34256

Pointer ? C (Pointer)3:int counter = 100;char sex = M;float gpa = 3.21; counter1001100sex1003Mgpa10043.21

Pointer

int *pt_int; __________char *pt_char ________float *pt_float ________

4 *;

____________________________________5counter1001100pt_countersex1003Mpt_sexgpa10043.21pt_gpa

()int counter = 100;int *pt_counter;char sex = M;char *pt_sex;float gpa = 3.21;float *pt_gpa;

6counter1001100pt_countersex1003Mpt_sexgpa10043.21pt_gpa

()int counter = 100;int *pt_counter;pt_counter = &counter;7counter1001001pt_counter1003_____

()8

%p

= &;

()int counter = 100;int *pt_counter;pt_counter = &counter;printf(address of counter is: %p\n, pt_counter);char sex = M;char *pt_sex;pt_sex = &sexprintf(address of sex is: %p\n, pt_sex);float gpa = 3.21;float *pt_gpa;pt_gpa = &gpa;printf(address of gpa is: %p, pt_gpa);

9

int x = 17, y;int *pt_int;pt_int = &x;y = *pt_num

10*x170100pt_int0370y0876_______

()#include "stdio.h"#include "conio.h"main(){ int a = 2, b = 3; int *p = &a; printf("Pointer p is points to a at address: %p\n", p); printf("Value of a is %d and value of p is %d too\n\n", a, *p); p = &b; printf("Now pointer p is point to b at address %p\n", p); printf("Value of b is %d and now value of p is %d too\n", b, *p); getch();}

11

12char a[4] = com;char *p;p = a; p = &a[0];

p1001a[0]a[1]a[2]a[3]comnull101102103104____

() +, -, ++, -- 1 int 2 bytesfloat 4 bytes13char a[4] = com;char *p;p = a; p = &a[0];p = p+2;--p;

p1001a[0]a[1]a[2]a[3]comnull101102103104101103102

()main() { char data[9] = "Computer"; char *p = data; printf("First element of data at address %p\n", &data[0]); printf("and value of first element is: %c\n\n", *p); ++p; printf("p point to address %p: value %c\n", p, *p); --p; printf("p point to address %p: value %c\n", p, *p); p = p+5; printf("p point to address %p: value %c\n", p, *p); getch();}14

1 15int a = 1, b = 2;int *p[2];p[0] = &a;p[1] = &b;a1101b2103P[0]P[1]105106______

()

int *p[3]; p 3 Char *p[10]; p 10

16 *[];

()main(){ int a[5] = {1, 2, 3, 4, 5}; int *p_a[5]; int i; for(i=0; i