6.2 Pointers
A simple variable in a program is stored in a certain number of bytes at a particular memory location, or address, in the machine. Pointers are used in programs to access memory and manipulate addresses.
프로그램 안에서 하나의 단순한 변수(variable)는 머신 안에서의 특정한 메모리지역 bytes, 혹은 주소의 확실한 수(number)안에 저장된다. 포인터는 프로그램 안에서 메모리를 access 하고 주소들을 솜씨 있게 다루기 위해,조종하기위해(manipulate)사용 되어 진다.
If v is a variable, then &v is the location, or address, in memory of its stored value. The address operator & is unary and has the same precedence and right to left associativity as the other unary operators. Addresses are a set of values that can be manipulated. Pointer variables can be declared in programs and then used to take addresses as values. The declaration
만약 v 가 변수라면, 그러면 &v는 그것의 저장된 값의 메모리 안에서의 지역(location) 혹은 주소가 된다. 주소연산자 & 은 단일체(unary)이고 다른 단일(unary)연산자들처럼 오른쪽에서 왼쪽으로의 관계를 갖고 동등한 선행처리권을 갖는다. 주소들은 다루어질 수 있는 값들의 집합이다. 포인터 변수들은 프로그램 안에서 선언되어질 수 있고 그리고 나서 값들(values)처럼 주소들을 취하여 사용한다. 그 선언 int *p
int *p;
declares p to be of type pointer to int. Its legal range of values always includes the special address 0 and a set of positive integers that are interpreted as machine addresses on the given C system. Some examples of assignment to the pointer p are
는 p를 int로의 포인트 형으로 선언한다. 값들의 올바른, 합법적인(legal) 범위는 항상 특별한 주소 0을 그리고 C가 주어진 시스템 위의 머신 주소들처럼 해석되어진 양의 정수로의 집합을(a set) 포함한다. 포인터 p로의 할당의 예들은 (다음과 같다.)
p = 0;
p = NULL; /* equivalent to p = 0; */
p = &i;
p = (int *) 1776; /* an absolute address in memory */
In the third example, we think of p as “referring to i” or “pointing to i” or “containing the address of i.” In the fourth example, the cast is necessary to avoid a compiler error.
세번째 예에서, 우리는 p에 대해 “i로의 돌림” 또는 “i로의 pointing” 또는 “i의 수소를 포함하는” 것으로 생각한다. 네번째 예에서는, 컴파일러 에러를 피하기 위한 주형, 주조물, 깁스(cast, 그러니까 일종의 형태라고 해석하면 좋겠네요.) 이다.
The indirection or dereferencing operator * is unary and has the same precedence and right to left associativity as the other unary operators. If p is a pointer, then *p is the value of the variable of which p is the address.
간접(직접적이지 않은) 혹은 dereferencing(사전에 없습니다. 그냥 대충 알아서 해석하세요. Indirection과 비슷한 뜻이겠거니) 연산자인 *은 단일체이고 다른 단일 연산자들처럼 오른쪽에서 왼쪽으로의 그리고 동등한 선행처리권을 갖는다. 만약 p가 포인터이면, 그러면 *p는 p가 주소가 되는 변수의 값이다.
The name “indirection” is taken from machine language programming. The direct value of p is a memory location, whereas *P is the indirect value of p-namely, the value at the memory location stored in p. In a certain sense * is the inverse operator to &. We want to give an explicit, yet elementary, example of how the pointer mechanism works. Let us start with the declaration
“indirection”이라는 이름은 기계언어 프로그래밍으로부터 가져왔다. P의 direct값은 메모리 지역(location)이다. 그에 반하여 *P 는 P의 값의 indirect 이다. P-즉, p안에 저장된 메모리 지역(location)의 값.
int a = 1, b = 2, *p;
At this point, we can think of the variables a, b, and p stored in memory as
이 시점에서, 우리는 생각해 볼 수 있다. 변수 a,b를, 그리고 메모리 안에 저장된 p를
그림: (a라는 박스안에 1이 들어있는 그림을, b라는 박스안에 2를 그리세요, 그리고 p라는 박스안에
화살표를 그려 그 화살표를 박스 밖으로 끄집어 내세요. 이 그림을 상상해 보세요.)
We think of the pointer p as an arrow, but because it has not yet been assigned a value, we do not know what it pointers to. Suppose that our next line of code is
우리는 pointer p를 화살표와 같이 생각한다, 하지만 아직 값이 할당된 것이 아니기 때문에, 우리는 알 수 없다. 그것이 무엇을 pointers to 하는지를. 생각해보자. 우리의 다음 라인의 코드
p = &a;
We read this as “p is assigned the address of a,” and we have the following picture:
우리는 이것을 “p는 a의 주소에 할당되어 졌다.”라고 읽는다. 그리고 우리는 다음과 같은 그림을 갖는다.
그림: (위에 p에서 끄집어 낸 화살표를 a라는 박스로 가져가서 붙이세요.그 그림을 상상해 보세요.)
Now let us make the assignment
이제 할당을 해보자.
b = *p;
We read this as “b is assigned the value pointed to by p.” Because the pointer p points to a, the statement
우리는 “b는 p에의해 포인트된 값으로 할당되었다”라고 읽는다. Pointer p 는 a를 points to(포인트)하기 때문에, 그 진술은 (아래 두개의 진술은 같다.)
b = *p; is equivalent to b = a;
Let us write a simple program that illustrates the distinction between a pointer value and its dereferenced value. We will use the %p format to print the value of a pointer, which on most systems produces a hexadecimal number. On ANSI C systems, the %p format is preferred. (See exercise 6, on page 312.)
Pointer값과 그것의 dereferenced (indirected)된 값의 구별을 써내려간 간단한 프로그램을 써보자. 우리는 %p format을 a pointer의 값을 프린트하기 위해 사용할 것이다. 그것은(%p)는 대부분의 시스템에서 16진수의 값을 생산한다. ANSI C 시스템에서, %p format은 제공되어 진다(선호되어 진다, is preferred).
In file locate.c
/* Printing an address, or location. */
#include
int main(void)
{
int i = 7, *p = &i;
printf("%s%d\n%s%p\n", "Value of i: ", *p,
"Location of i: ", p);
return 0;
}
The output of this program on our system is
Value if i: 7
Location if i: effffb24
A pointer can be initialized in a declaration. The variable p is of type int * and its initial value is &i. Also, the declaration of i must occur before we take its address. The actual location of a variable in memory is system-dependent. The operator * dereferences p. That is, p contains an address, or location, and the expression *p has the value of what is stored at this location appropriately interpreted according to the type declaration of p.
포인터는 선언에서 초기화되어 질 수 있다. 변수 p는 int * 형(type)이다. 그리고 그것의 초기 값은 %i 이다. 또한, i 의 선언은 우리가 그것의 주소를(address)를 취하기 전에 발생해야만 한다. 메모리 안에서 변수의 사실적인 위치(actual location)는 시스템에 의존적이다. 연산자 * 은 p를 dereferences(간접적으로 돌려준다,indirects)한다. 그것은(다시 말해서), p 가 주소, 혹은 위치(location), 그리고 p의 선언 형(type)에 의해 적절하게,정확하게 해석되어진 이 지역에(location) 저장되어 있는 값을 표현하는 *p를 포함한다.
|