1 / 14

Pointers

Pointers. 황 강 욱 Next Generation Communication Networks Lab. Division of Applied Mathematics KAIST. Pointer. Pointer : Memory 주소 값을 저장하고 있는 변수 Pointer 의 자료형 : int, float, char 포인터가 저정하고 있는 메모리에 저장된 data type Pointer 의 선언 int *int_ptr; float *float_ptr; char *char_ptr;.

Download Presentation

Pointers

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pointers 황 강 욱 Next Generation Communication Networks Lab. Division of Applied Mathematics KAIST

  2. Pointer • Pointer : Memory 주소 값을 저장하고 있는 변수 • Pointer의 자료형 : int, float, char • 포인터가 저정하고 있는 메모리에 저장된 data type • Pointer의 선언 • int *int_ptr; • float *float_ptr; • char *char_ptr; Next Generation Communication Networks Lab.

  3. 변수의 주소값을 알려면? • int i; • int *i_ptr; • i_prt = & i; • cout << i_prt << endl; Next Generation Communication Networks Lab.

  4. a[0] a[1] a[2] a[3] a[4] a[5] 1차원 배열과 pointer • int a[6]; • 배열의 시작주소 : a 또는 &a[0] c.f. 배열의 이름이 그 배열의 시작주소를 나타냄. • pointer 연산 • a == &a[0] • a + i == &a[i] • *(a+i) == a[i] Next Generation Communication Networks Lab.

  5. 2차원 배열과 pointer • int a[2][2]; • a == a[0] == & a[0][0] • a[0] + 1 == & a[0][1] • a[0] + 2 == a[1] == & a[1][0] • a[0] + 3 == a[1]+1 == & a[1][1] • a + 1 == a[1] • *a[0] == a[0][0] • *a == a[0] • *a + 1 == a[1] • *(*a+1) == a[1][0] a a[0] a[0]+1 a+1 a[1] a[1]+1 a[0][0] a[0][1] a[1][0] a[1][1] Next Generation Communication Networks Lab.

  6. int *b[2]; // b[0]와 b[1] 두개의 int pointer 선언 • int (*c)[2]; // 2개의 원소를 갖는 int 배열의 pointer 선언 • 함수에서의 활용 • void function(int a[][2]) • void function(int (*a)[2]) Next Generation Communication Networks Lab.

  7. Pointers to pointers main() { int a; int * b; int ** c; //pointer to pointer a = 1; b = &a; c = &b; cout << a << endl; cout << b << endl; cout << *b << endl; cout << c << endl; cout << *c << endl; } a b c 0x100 1 0x520 0x100 0x520 0x700 Next Generation Communication Networks Lab.

  8. 함수의 포인터 int inc(int i) { return i+1;} int dec(int i) {return i-1;} main() { int i=1; int (*func)(int); func = inc; cout << func(1) << endl; //함수 포인터 사용 예 1 func = dec; cout << (*func)(2) << endl; //함수 포인터 사용 예 2 } 함수의 포인터 Next Generation Communication Networks Lab.

  9. 함수의 포인터 배열 enum {add, sub, multi, div}; int add(int a, int d) {return a+d;} int sub(int a, int d) {return a-d;} int multi(int a, int d) {return a*d;} int div(int a, int d) {return (a/d);} int (*func[4])(int, int) = {add, sub, multi, div}; main() { cout << (*func[add])(2,1) << “, “ << (*func[sub])(2,1); cout << (*func[multi])(2,1) << “, “ << (*func[div])(2,1); } Next Generation Communication Networks Lab.

  10. void pointer • void pointers are pointers that point to a value that has no type . • This allows void pointers to point to any data type, from an integer value or a float to a string of characters. Next Generation Communication Networks Lab.

  11. #include <iostream> void increase (void* data) { switch (sizeof(data)) { case sizeof(char) : (*((char*)data))++; break; case sizeof(int) : (*((int*)data))++; break; } } int main () { char a = 'x'; int b = 1602; increase (&a); increase (&b); cout << a << ", " << b; return 0; } y, 1603 Next Generation Communication Networks Lab.

  12. NULL Pointer • A null pointer is a regular pointer of any pointer type which has a special value that indicates that it is not pointing to any valid reference or memory address • int * p; p = NULL; // p has a null pointer value Next Generation Communication Networks Lab.

  13. pointers to classes main() class person { public: name[30]; phone[20]; }; person p_list[100]; person *list_ptr; list_ptr = p_list; strcpy(list_ptr->name,"aaa"); cout << list_ptr->name << endl; } Next Generation Communication Networks Lab.

  14. 실습 (Due Nov. 16) • 주어진 파일에서 사람이름과 전화번호를 입력받아 person 배열에 저장하고, 이름에 따른 사전식 순서대로 sorting하여 출력하는 프로그램을 작성하시오. (person class를 이용함) • 파일예 David Kim 111-1234 Tom Kim 123-9099 Jane Jung 190-0090 Brown Lee 123-1234 John Ahn 729-0987 • void sorting(person *a, person *b) Next Generation Communication Networks Lab.

More Related