본문 바로가기

Linux/C

[linux] 포인터#2

=====================================================================
LAB> pointer 예제 5

-- point5.c --
#include <stdio.h>

int main()
{
  char os[] = "Linux, Windows, Unix";
  char *p;

  p = os;  // p --> os

  printf(os);
  putchar('\n');

  printf(p);
  putchar('\n');

  printf(p+7);
  putchar('\n');
  
  return 0;
}
-- point5.c --
=====================================================================


=====================================================================
LAB> pointer 예제 6


참고 : http://forum.falinux.com/zbxe/index.php?document_srl=408108&mid=C_LIB

 [1]
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
L|i|n|u|x|,| |W|i|n|d|o|w|s|,| |U|n|i|x|
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
[0]
^
|
os

os[0] = 'L';
os[1] = 'i'; 

배열의 이름은 배열의 첫 번째 index 의 주소를 의미한다.
따라서 배열 이름은 주소다.

os  == &os[0]

-- point6.c --
#include <stdio.h>
#include <string.h> 

int main()
{
  char os[] = "Linux, Windows, Unix";
  char *p;

  printf("os = %s \n", strchr(os, 'W'));
  printf("os = %s \n", strrchr(os, 'U'));
 
  return 0;
}
-- point6.c --

-- 실행결과 --
os = Windows, Unix 
os = Unix
-- 실행결과 --

-- 디버깅 --
-- 디버깅 --
=====================================================================


=====================================================================
LAB> pointer 예제 7

- pointer 예제 6에 해당하는 strchr 함수를 구현해보자.

함수 원형 : char *strchr(const char *s, int c);
사용자 정의함수 : char *Strchr(const char *s, int c);

-- point7.c --
#include <stdio.h>

char *Strchr(const char *s, int c);  // 함수 선언

int main()
{
  char os[] = "Linux, Windows, Unix";
  char ch;

  printf("os = %s \n", os);
  printf("찾을 문자를 입력하세요 : ");
  scanf("%c", &ch);

  printf("os = %s \n", Strchr(os, ch)); // 함수 호출
  //printf("os = %s \n", Strchr(os, 'W')); // 함수 호출
  return 0;
}

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
L|i|n|u|x|,| |W|i|n|d|o|w|s|,| |U|n|i|x|0|
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

char *Strchr(const char *s, int c)  // 함수 정의
{
  const char *p;
  p = s;

  while(*p)
  {
    if(*p == c)
      return (char *)p;
    p++;
  } 
  return NULL;
}
-- point7.c --

-- 실행결과 --
# ./point7
os = Linux, Windows, Unix 
찾을 문자를 입력하세요 : L
os = Linux, Windows, Unix 
# ./point7
os = Linux, Windows, Unix 
찾을 문자를 입력하세요 : x
os = x, Windows, Unix 
-- 실행결과 --

-- 디버깅 --
b main
r
n
:
(gdb) n
찾을 문자를 입력하세요 : W
14	  printf("os = %s \n", Strchr(os, ch)); // 함수 호출
(gdb) s
Strchr (s=0xbfffe9df "Linux, Windows, Unix", c=87) at point7.c:22
22	  p = s;
display p
display s
display c
(gdb) n
24	  while(*p)
3: c = 87
2: s = 0xbfffe9df "Linux, Windows, Unix"
1: p = 0xbfffe9df "Linux, Windows, Unix"
(gdb) n
26	    if(*p == c)
3: c = 87
2: s = 0xbfffe9df "Linux, Windows, Unix"
1: p = 0xbfffe9df "Linux, Windows, Unix"
(gdb) 
(gdb) x/c 0xbfffe9df
0xbfffe9df:	76 'L'
n
:
24	  while(*p)
3: c = 87
2: s = 0xbfffe9df "Linux, Windows, Unix"
1: p = 0xbfffe9e0 "inux, Windows, Unix"
n
:
:
(gdb) 
26	    if(*p == c)
3: c = 87
2: s = 0xbfffe9df "Linux, Windows, Unix"
1: p = 0xbfffe9e6 "Windows, Unix"
(gdb) x/c 0xbfffe9e6
0xbfffe9e6:	87 'W'
(gdb) x/d 0xbfffe9e6
0xbfffe9e6:	87
:
:
(gdb) n
os = Windows, Unix 
main () at point7.c:16
16	  return 0;
-- 디버깅 --
=====================================================================

=====================================================================
LAB> pointer 예제 8

- pointer 예제 7에 해당하는 strchr의 소스를 수정해서
strrchr 함수를 구현해보자.

함수 원형 : char *strrchr(const char *s, int c);
사용자 정의함수 : char *Strrchr(const char *s, int c);

-- point8.c --
#include <stdio.h>

char *Strrchr(const char *s, int c);  // 함수 선언

int main()
{
  char os[] = "Linux, Windows, Unix";
  char ch;

  printf("os = %s \n", os);
  printf("찾을 문자를 입력하세요 : ");
  scanf("%c", &ch);

  printf("os = %s \n", Strrchr(os, ch)); // 함수 호출
  return 0;
}

/*
 * 1. p를 끝까지 이동시켜야 한다.
 * p 가 증가하는 방향
 * ----------------------------------------->
 * p가 정방향으로 가면서 문자를 검색한다.
 *                                    p++ --+
 *                                          |
 *  i=0                                     v
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |L|i|n|u|x|,| |W|i|n|d|o|w|s|,| |U|n|i|x|0|
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  ^                                     ^ i=20
 *  |  p--                                |
 *  |                                     +-- p--
 *  +-- p가 역방향으로 오면서 문자를 검색해야 한다.
 *  <----------------------------------------
 *  p가 감소하는 방향
 *
 * 2. 다시 처음으로 와야 한다.
 *
 */

char *Strrchr(const char *s, int c)  // 함수 정의
{
  const char *p;
  p = s;
  int i = 0;

  // 배열 s의 끝까지('\0') 가기 위해서는 아래를 이용하면 된다.
  // 1. p를 끝까지 이동시켜야 한다.
  while(*p)
  {
    p++;
    i++;
  }
  
  p--;
/*
 *  i=0                                     v
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |L|i|n|u|x|,| |W|i|n|d|o|w|s|,| |U|n|i|x|0|
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  ^                                     ^ i=20
 *  |
 */
  while(i > 0 )
  { 
    if(*p == c)
      return (char *)p;
    p--;
    i--;
  } 

  return NULL;
}
-- point8.c --

-- 실행결과 --
# ./point8 
os = Linux, Windows, Unix 
찾을 문자를 입력하세요 : W
os = Windows, Unix 
# ./point8 
os = Linux, Windows, Unix 
찾을 문자를 입력하세요 : x
os = x 
# ./point8 
os = Linux, Windows, Unix 
찾을 문자를 입력하세요 : a
os = (null) 
-- 실행결과 --

-- 디버깅 --
b main
r
n
:
:
(gdb) 
찾을 문자를 입력하세요 : W
14	  printf("os = %s \n", Strrchr(os, ch)); // 함수 호출
(gdb) s
Strrchr (s=0xbfffe9df "Linux, Windows, Unix", c=87) at point8.c:21
21	  p = s;

display i
display s
display p
display c
n
:
:
(gdb) n
26	    p++;
3: s = 0xbfffe9df "Linux, Windows, Unix"
2: p = 0xbfffe9f1 "ix"
1: i = 18
(gdb) n
24	  while(*p)
3: s = 0xbfffe9df "Linux, Windows, Unix"
2: p = 0xbfffe9f2 "x"
1: i = 19
(gdb) n
26	    p++;
3: s = 0xbfffe9df "Linux, Windows, Unix"
2: p = 0xbfffe9f2 "x"
1: i = 19
(gdb) n
27	    i++;
3: s = 0xbfffe9df "Linux, Windows, Unix"
2: p = 0xbfffe9f3 ""
1: i = 19
(gdb) x/1xb 0xbfffe9f3
0xbfffe9f3:	0x00
(gdb) n
24	  while(*p)
3: s = 0xbfffe9df "Linux, Windows, Unix"
2: p = 0xbfffe9f3 ""
1: i = 20
(gdb) n
30	  p--;
3: s = 0xbfffe9df "Linux, Windows, Unix"
2: p = 0xbfffe9f3 ""
1: i = 20
(gdb) n
32	  while(i > 0)
3: s = 0xbfffe9df "Linux, Windows, Unix"
2: p = 0xbfffe9f2 "x"
1: i = 20
-- 디버깅 --
=====================================================================


'Linux > C' 카테고리의 다른 글

Call by Reference  (0) 2015.06.01
[linux] 포인터#3  (0) 2015.05.14
[linux] 포인터#1  (0) 2015.05.12
[linux] 명령행 인자를 이용한 프로그램 실행  (0) 2015.05.11
[linux] 반복문( while )  (0) 2015.05.11