include stdio h void main char c A

  • Slides: 82
Download presentation

포인터 #include <stdio. h> void main( ) { char c = 'A'; int n=7;

포인터 #include <stdio. h> void main( ) { char c = 'A'; int n=7; double d=3. 75; printf("%x t %xn", &c, &n, &d); } 2022 -01 -07 6

char형 포인터 #include <stdio. h> void main(void) { char c; char *p=NULL; // 포인터변수

char형 포인터 #include <stdio. h> void main(void) { char c; char *p=NULL; // 포인터변수 p 선언 c= ‘A’; 왜? char *p 로 선언하여 1 byte p= &c; // 포인터변수에 c의참조 주소값대입 char형 변수 c를 참조하고 있기때문 printf(“%c %cn”, c, *p); printf(“%d %dn”, sizeof(c), sizeof(p)); printf(“%d %dn”, sizeof(&c), sizeof(*p)); } 2022 -01 -07 return; 13

int형 포인터 #include <stdio. h> void main(void) { int a; int *p=NULL; a= 2011;

int형 포인터 #include <stdio. h> void main(void) { int a; int *p=NULL; a= 2011; p= &a; } 2022 -01 -07 4 byte 참조 왜? int *p 로 선언하여 int형 변수 a를 참조하고 있기때문 printf(“%d %dn”, a, *p); printf(“%d %dn”, sizeof(a), sizeof(p)); return; 14

char형 주소의 연산 void main(void) { char c='A'; char *p= &c; 그럼 &p의 값은?

char형 주소의 연산 void main(void) { char c='A'; char *p= &c; 그럼 &p의 값은? ? ? &c printf("c=%c *p=%cnn", c, *p); printf("&c= %xn", &c); printf("p = %xn", p); p+1 printf("&c+1 = %xn", &c+1); printf("p+1 = %xn", p+1); } 2022 -01 -07 return; 15

int형 주소의 연산 void main(void) { int a=100; int *p= &a; 그럼 &p의 값은?

int형 주소의 연산 void main(void) { int a=100; int *p= &a; 그럼 &p의 값은? ? ? printf("a = %d, *p = %dnn", a, *p); printf("&a = %xn", &a); printf("p = %xnn", p); printf("&a+1 = %xn", &a+1); printf("p+1 = %xnn", p+1); } 2022 -01 -07 p+1 return; 17

예제(p 204) 2022 -01 -07 19

예제(p 204) 2022 -01 -07 19

Review 예제 1 #include <stdio. h> void main() { ; ; } 2022 -01

Review 예제 1 #include <stdio. h> void main() { ; ; } 2022 -01 -07 x= (); p= ; printf("입력된 문자 = printf(" *p = n", ); ); 25

Review 예제 2 #include <stdio. h> void main() { ; ; printf("두개의 정수를 입력하시오:

Review 예제 2 #include <stdio. h> void main() { ; ; printf("두개의 정수를 입력하시오: "); scanf("%d, %d", , ); sum = ; printf("a = , b= , sum = printf("a + b = n", ); n", , , ); } 2022 -01 -07 27

8. 3 포인터와 배열 (P 206) • 배열의 이름 – ( – a+1=? )을

8. 3 포인터와 배열 (P 206) • 배열의 이름 – ( – a+1=? )을 나타낸다. a &a[0] 0 x 1000 &a[0] &a[1] 0 x 1004 int a[5]={ 1, 2, 3, 4, 5 }; a[1] &a[2] 0 x 1008 a[2] &a[3] 0 x 100 c a[3] &a[4] 0 x 1010 . . . 2022 -01 -07 a[4] 28

예제(P 206) 2022 -01 -07 32

예제(P 206) 2022 -01 -07 32

8. 4 포인터와 함수(P 212) 2022 -01 -07 41

8. 4 포인터와 함수(P 212) 2022 -01 -07 41

8. 4 포인터와 함수(P 213) 2022 -01 -07 43

8. 4 포인터와 함수(P 213) 2022 -01 -07 43

8. 4 포인터와 함수 • Scanf 함수 호출(Call-By-Reference) – Case 1 – Case 2

8. 4 포인터와 함수 • Scanf 함수 호출(Call-By-Reference) – Case 1 – Case 2 2022 -01 -07 44

예제(p 214) 2022 -01 -07 47

예제(p 214) 2022 -01 -07 47

8. 6 void 포인터-P 221 #include <stdio. h> int main( ){ int n=10; void

8. 6 void 포인터-P 221 #include <stdio. h> int main( ){ int n=10; void *vptr = &n; int *iptr = &n; *((int *)vptr) =20; } 2022 -01 -07 // void형 포인터 선언 & 초기화 // int형 포인터 선언 & 초기화 (형변환필요x) // 20을 대입하기 위해 int형으로 형변환 printf("n=%dn", n); (*((int *)vptr))++; // 증감 연산자 사용하기 복잡함 printf("*vptr = %dn", *((int *)vptr)); (*iptr)++; printf("*iptr = %dn", *iptr); return 0; 51

8. 7 다중 포인터 • 포인터 배열 예제 for(i = 0; i < 3;

8. 7 다중 포인터 • 포인터 배열 예제 for(i = 0; i < 3; i++) printf("%x %x %dn", &arr[i], *arr[i]); 0 x 18 ff 20 0 x 18 ff 34 0 x 18 ff 28 0 x 18 ff 30 0 x 18 ff 2 c 2022 -01 -07 53

8. 7 다중 포인터 • 생각해보기. . void main(){ char name[3][10] = { "Jasmine",

8. 7 다중 포인터 • 생각해보기. . void main(){ char name[3][10] = { "Jasmine", "Bell", "Chris"}; char *pstr[3] = { name[0], name[1], name[2] }; } 2022 -01 -07 for (i = 0; i < 3; i++) printf("%x %s %sn", pstr[i], name[i]); 54

8. 7 다중 포인터 (223) • int (*pnt_z)[10] = “abcdefg”; pnt_z “abcdefg” • int

8. 7 다중 포인터 (223) • int (*pnt_z)[10] = “abcdefg”; pnt_z “abcdefg” • int pnt[10] = “abcdefg”; pnt “abcdefg” 2022 -01 -07 55

8. 7 다중 포인터 (p. 223) #include<stdio. h> void main() { char ptr[10] =

8. 7 다중 포인터 (p. 223) #include<stdio. h> void main() { char ptr[10] = "abcdefg"; char (*pptr)[10] = "abcdefg"; printf("%x %x %sn", ptr, &ptr[0], ptr); printf("%d %dn", sizeof(ptr), sizeof(*ptr)); } 2022 -01 -07 printf("%x %x %sn", pptr, &pptr[0], pptr); printf("%d %dn", sizeof(pptr), sizeof(*pptr)); 56

예제(p 223) 2022 -01 -07 58

예제(p 223) 2022 -01 -07 58

구조체 변수 *구조체 정의와 변수 선언을 동시에 *구조체 정의와 변수 선언을 따로 struct point

구조체 변수 *구조체 정의와 변수 선언을 동시에 *구조체 정의와 변수 선언을 따로 struct point { int x; int y; } p 1, p 2, p 3; struct point { int x; int y; }; int main(void) { … return 0; } int main(void) { struct point p 1, p 2, p 3; … return 0; } 2022 -01 -07 63

구조체 변수(p 96) 2022 -01 -07 65

구조체 변수(p 96) 2022 -01 -07 65

구조체 변수 • 구조체 변수의 “초기화” #include <stdio. h> struct point { int x;

구조체 변수 • 구조체 변수의 “초기화” #include <stdio. h> struct point { int x; int y; }; int main(void) { struct point p 1={10, 20}; } 2022 -01 -07 // 구조체 변수 선언과 동시에 초기화 printf("%d %d n", p 1. x, p 1. y); return 0; 66

구조체 변수(P 97) 2022 -01 -07 68

구조체 변수(P 97) 2022 -01 -07 68

구조체 변수(P 97) #include <stdio. h> struct list { char *name; char sex; int

구조체 변수(P 97) #include <stdio. h> struct list { char *name; char sex; int age; }; // 이름을 문자열로 void main() { struct list a, b = {"Jason", 'M', 60}; a. name = "Test"; a. sex = 'F'; a. age = 40; printf("Notnametsextagen"); printf("%dt%st%ct%dn", 1, a. name, a. sex, a. age); printf("%dt%st%ct%dn", 2, b. name, b. sex, b. age); } 2022 -01 -07 69

구조체와 포인터 • 멤버변수로 포인터 사용하기 #include <stdio. h> struct point { int* x;

구조체와 포인터 • 멤버변수로 포인터 사용하기 #include <stdio. h> struct point { int* x; // 포인터 멤버변수 int* y; }; int main(void) { int num 1=4; int num 2=5; struct point p 1; p 1. x=&num 1; p 1. y=&num 2; } 2022 -01 -07 ‘. 연산자가 *연산자보다 우선순위가 높다. ’ printf("%d %d n", num 1, num 2); printf("%d %d n", *p 1. x, *p 1. y); return 0; 71

#include <stdio. h> struct point { int* x; // 포인터 멤버변수 int* y; char*

#include <stdio. h> struct point { int* x; // 포인터 멤버변수 int* y; char* z; }; int main(void) { int num 1=4; int num 2=5; char name[30]="201405123 Adam"; struct point p 1; p 1. x=&num 1; p 1. y=&num 2; p 1. z = name; } *p 1. z는? ? ? printf("%d %d %sn", num 1, num 2, name); printf("%d %d %sn", *p 1. x, *p 1. y, p 1. z); return 0; 2022 -01 -07 72

구조체와 포인터 • 구조체 포인터 변수사용하기 #include <stdio. h> struct student { char no[10];

구조체와 포인터 • 구조체 포인터 변수사용하기 #include <stdio. h> struct student { char no[10]; char name[20]; double total; }; int main(void) { struct student s // 학번 // 이름 // 총점 구조체포인터 p의 경우 = {"20101323", "Park", 160}; struct student *p; // 구조체 포인터 변수 선언 p = &s; printf("%s %s %lf n", s. no, s. name, s. total); printf("%s %s %lf n", (*p). no, (*p). name, (*p). total); printf("%s %s %lf n", p->no, p->name, p->total); } 2022 -01 -07 return 0; 73

구조체와 함수 #include<stdio. h> struct point { int x; int y; }; void function

구조체와 함수 #include<stdio. h> struct point { int x; int y; }; void function (struct point call); int main(void) { struct point p= {10, 20}; function(p); return 0; } // 구조체 정의 // 함수의 선언 // 값에 의한 호출(call by value) void function (struct point call) // 함수의 정의 { printf("%d %d n", call. x, call. y); // 10, 20 출력 } 2022 -01 -07 75

구조체와 함수 int main(void) { struct point p = { 10, 20 }; function

구조체와 함수 int main(void) { struct point p = { 10, 20 }; function ( p ); … } p는 call에 복사됨 void function (struct point call) { printf("%d %d n", call. x, call. y); } call. x = 30; call. y = 40; ? ? 2022 -01 -07 76

구조체와 함수 #include<stdio. h> struct point { int x; int y; }; void function

구조체와 함수 #include<stdio. h> struct point { int x; int y; }; void function (struct point* call); // 함수의 선언 int main(void) { struct point p = {10, 20}; function(&p); } // 주소에 의한 호출(call by reference) return 0; void function (struct point *call) // 함수의 정의 { printf("%d %d n", call->x, call->y); printf("%d %d n", (*call). x, (*call). y); } 2022 -01 -07 78

구조체와 함수 int main(void) { struct point p = { 10, 20 }; function

구조체와 함수 int main(void) { struct point p = { 10, 20 }; function ( &p ); … } p의 주소가 포인터 변수 call에 저장 void function (struct point* call) { printf("%d %d n", call->x, call->y); printf("%d %d n", (*call). x, (*call). y); } 2022 -01 -07 call->x = 30; *(call). y = 40; 79

p->sum = (*p). sum ≠ *(p. sum) = *p. sum struct student { int

p->sum = (*p). sum ≠ *(p. sum) = *p. sum struct student { int sum; } *p; 2022 -01 -07 struct student { int *sum; } p; 82