본문 바로가기

C Language

Hello World Hi 에서 World Hi를 출력하도록 하는 법.

포인터에 6을 더함.

이 코드에서 핵심 변경사항은 *my = *my + 6; 라인입니다. 이는 다음과 같이 작동합니다:
*my는 str을 가리킵니다.
str은 ch의 시작 주소를 가지고 있습니다.
*my + 6은 ch의 시작 주소에서 6바이트 뒤의 주소를 계산합니다.
이 새 주소를 다시 *my에 할당합니다.
결과적으로, *my는 이제 "World Hi"의 시작 부분을 가리키게 되어, "World Hi"가 출력됩니다.
이 방법은 단순히 포인터 산술을 사용하여 원하는 위치로 이동하는 것입니다.

 

The key change in this code is the line *my = *my + 6;. Here's how it works:
*my points to str.
str holds the starting address of ch.
*my + 6 calculates the address 6 bytes after the starting address of ch.
This new address is then reassigned to *my.
As a result, *my now points to the beginning of "World Hi", so "World Hi" is printed.
This method simply uses pointer arithmetic to move to the desired position.

이중포인터로 선언

이 코드의 작동 방식은 다음과 같습니다:
char *strings[]는 문자열 포인터의 배열을 생성합니다.
char **ch = strings;는 ch를 이 배열의 시작점을 가리키도록 설정합니다.
ch++;는 ch를 배열의 다음 요소로 이동시킵니다.
printf("%s\n", *ch);는 ch가 가리키는 문자열(즉, "World Hi")을 출력합니다.
이 방법을 사용하면 ch를 1 증가시켜 "World Hi"를 출력할 수 있습니다. 이는 이중 포인터를 사용하여 문자열 배열을 탐색하는 방법을 보여줍니다.

 

Here's how this code works:
char *strings[] creates an array of string pointers.
char **ch = strings; sets ch to point to the beginning of this array.
ch++; moves ch to the next element in the array.
printf("%s\n", *ch); prints the string that ch is pointing to (i.e., "World Hi").
Using this method, we can increment ch by 1 to output "World Hi". This demonstrates how to use a double pointer to navigate an array of strings.