CHAPTER 10
POINTERS

[IMAGE: An Quarter-Size Version of The Joy of C Front Cover]
This chapter introduces pointers, a data type that can hold addresses. We study how to use them to provide call-by-reference parameter passing and to traverse arrays efficiently. We also see how to use them to access dynamically allocated arrays, arrays for which space is allocated at run time rather than compile time. The chapter concludes with a new implementation of sets, this time with dynamically allocated sets of varying sizes.

Jump to: [Previous Chapter | Next Chapter]


  1. POINTERS
  2. USING POINTERS TO SIMULATE CALL BY REFERENCE
  3. TRAVERSING ARRAYS USING POINTERS
  4. ARRAY PARAMETERS AND POINTERS
  5. POINTERS AS FUNCTION RETURN VALUES
  6. CONSTANTS AND POINTERS
  7. GENERIC POINTERS AND POINTER CONVERSIONS
  8. DYNAMICALLY ALLOCATING ARRAYS
  9. CASE STUDY: DYNAMICALLY ALLOCATED SETS

Objectives



Pointer: Memory address�� �����ϴ� data structure (�޸� �ּҸ� �����ϴ� ����)

Pointer variable ����: base_type *pointer_variable;

��)

int *p1, p2;

Integer�� ������ �� �ִ� memory pointer ���� p1 (pointer to integer)�� integer�� ������ �� �ִ� ���� p2�� ����

Pointer�� ���� pointer (pointer to pointer), pointer�� ���� pointer�� ���� pointer�� ������ �� �ִ�. �׷��� �� �ܰ� �̻��� pointer ���Ǵ� ���α׷��� �����ϱ� ��ư� ����Ƿ� ������ ����� �����Ѵ�.

int **p; // pointer to pointer to integer


Pointer ���� operators

&: Address-of (������ �ּ�)

*: Value-pointed-to (pointer�� point�� �޸𸮿� ����� ��)

int val = 10;       // int �� 10�� �����ϰ� �ִ� �޸� : val
int *p = &val; // ���� p�� �� 10�� �����ϰ� �ִ� ���� val�� �Ҵ�� �޸��� �ּ� ���� ����: pointer variable 
// ���߿� �ʱ� ���� �ο��Ϸ��� p = &val;�� ����Ͽ��� �Ѵ�.

printf("&val=%X, p=%d, *p= %d, val=%d\n", &val, p, *p, val);

NULL pointer: Ư�� �������� ��� (address 0)



Pointer�� function arguments

void swap(int *a, int *b) // integer a, b ������ ���� ��ȯ

{
   int temp;

   temp = *a;
   *a = *b;
   *b = temp;
}

Function�� �ϳ��� ���� return�ϳ� pointer�� �̿��Ͽ� ���� ������ ȣ�� function���� ���� �� �ִ�. �̷��� function�� �����ϱ� ����� �μ� ȿ�� (side-effect)�� ������ ���� ����� ���� �ٶ������� �ʴ�. Ư�� function�� �ٸ� function�� argument�� ����ϱ� ��ư� �Ѵ�.

��) hours.c


Pointer arithmetic (address arithmetic)

Pointer�� ���õ� �ǹ��ִ� ������� ������ ����.



Pointer ���� �� pointer�� point�ϴ� �ڷ����� ���� �ڵ������� �ּ� ����� ��������. ��, pointer to integer�� pointer to float ���� 1 ���� ��Ű�� ���� ���� integer�� float�� ���Ǵ� �޸� byte �� ��ŭ ������Ű�� ����� ���´�.

* ����: Pointer�� pointer�� ���ϱ�, pointer�� ���� ���ϱ�, ������� �ƹ� �ǹ̰� ����.

��) 32bit compile ��

char *p1;
int *p2;
float *p3;
double *p4;

p1 = 100;
p2 = 200;
p3 = 300;
p4 = 400;
// p1 + 1 ==> 101
// p2 + 1 ==> 204
// p3 + 1 ==> 304
// p4 + 1 ==> 408



Pointer�� array

�迭���� �ּ� (pointer ��)�� ó���ȴ�. (���α׷� ������ ���� �����ų �� ����. ��, ����� ó����.)

int ary[5] = { 10, 20, 30, 40, 50 }; 
int *p1, *p2; // integer�� ������ �� �ִ� �޸� �ּҸ� �����ϴ� ������ ���� p1, p2 ����

p1 = ary; // p1 = &ary[0]; �� ����
p2 = &ary[4]; // p2 = p1 + 4;�� ����

1: *p1 == ary[0],  *(p1 + 1) == ary[1],  *(p + i) == ary[i] == p[i]
2: (p1 < p2)�� TRUE
3: *p1 ==> 10
4: *(p1 + 3) ==> 40
5: *p1 + 3 ==> 13
6: *p1++ ==> *(p1++) ==> evaluate ����� 10�̰� �� �� p1�� 1 ���� ��Ŵ (p1 == &ary[1])
7: (*p1)++ ==> 11
8: *++p1 ==> 20
9: ++*p1 ==> 11
10: *p1 = 100; ==> ary[0] = 100;



Pointer array

int *ptr[10];  // pointer to integer 10���� ������ �� �ִ� �迭 ptr ����
int (*ptr)[10];  // integer 10���� ������ �� �ִ� �迭�� ���� ������ ptr ����



Pointers to functions

int (*fp)(); // fp�� int�� return�ϴ� function�� ���� pointer

int *f(); // f�� pointer to integer�� return�ϴ� function

��)

void f1() { printf("Function 1 is called.\n"); }
void f2() { printf("Function 2 is called.\n"); }

void main()
{
void (*fp)();

fp = f1;
(*fp)(); // f1();�� ����
fp = f2;
(*fp)(); // f2();�� ����
}

���:
Function 1 is called.
Function 2 is called.


������ pointer p ���� ��)

char *p; // pointer to char
char **p;  // pointer to pointer to char
char (*p)[10];  // pointer to array [10] of char
char *p[10];  // array [10] of pointer to char
char *p();  // function returning pointer to char
char (*p)();  // pointer to function returning char
char (*p[10])();  // array [10] of pointer to function returning char



Dynamic memory allocation

Static allocation: global variableó�� compile �� Ư�� ������ �޸𸮰� �Ҵ�Ǿ� ���α׷� ���� �� ��� ��� ������ �޸� ���

Automatic allocation: function �Ǵ� block ���� �� �Ҵ�Ǿ� �� function �Ǵ� block ������ ������ ������ ������ �޸� ���

Dynamic allocation: ���α׷� ���� ���� �ʿ� �� ��� ���� �޸� ���� (heap; pool of unallocated memory)���� �Ҵ�޾� ����ϰ� ����� ������ �ٽ� heap���� �ǵ��� �� �ִ� �޸� ���


Generic pointer: void *

������ �ڷ����� point�� �� ������ ũ�⸦ �𸣱� ������ pointer ���� ������ �� �� ����. ��� �� Ư�� �ڷ��� pointer�� casting�Ͽ� ����� �� �ִ�.

Heap���� memory�� �Ҵ���� ��� malloc function�� ����ϰ� �Ҵ���� memory�� heap���� ������ ��� free function�� ����Ѵ�. (memory ���)

Prototypes:

void *malloc(int);

free(void *);

��) char 10���� ������ �� �ִ� �޸� �Ҵ�

char *cp;
cp = (char *)malloc(10);

// int 100���� ������ �� �ִ� �޸� �Ҵ�

int *ip;
ip = (int *)malloc(100 * sizeof(int));

// ���� pointer ���� ����� ����Ͽ� memory�� �̿��� �� �ִ�.
// malloc function�� memory�� return�� �� ���� ��� (heap�� �����
memory�� ���� ���� ���� ���) NULL pointer (Ȥ�� NULL ��)�� return�Ѵ�.
// ����ڴ� malloc function�� ����� memory�� �Ҵ��Ͽ����� ������
���� �˻��Ͽ��� �Ѵ�.

if (ip == NULL) Error("No memory available");

// ����� ���� memory�� ������ ���� heap���� �����ش�.

free(ip); // �Ǵ� free((void *) ip); 



�ǽ�) �й�(long)�� ����(float)�� �Է¹޾� ���������� ����ϴ� ���α׷��� ���� �� ���� ��츦 ó���ϵ��� ���� �ۼ��϶�.



Assignment #8 (�Ⱓ: 1����)

Integer�� ������ array�� �� ũ�⸦ �Ķ���ͷ� �޾� array�� ����� �� ������ �󵵼��� ����ϰ� array�� ����� �� �� �ּҰ��� �ִ밪�� ��� return�ϴ� ���α׷��� �ۼ��϶�.

��)
array ����: 91 93 98 92 92 95 93 92 91 95 99 92 98
���:
91: 2
92: 4
93: 2
95: 2
98: 2
99: 1
�ּҰ�: 91
�ִ밪: 99


[ Table Of Contents | Previous Chapter | Next Chapter]