소개
C 프로그래밍에서 포인터 배열은 각 요소가 int, char, float 등과 같은 다른 데이터 유형에 대한 포인터인 배열입니다. 이 랩에서는 정수 포인터 배열과 문자 포인터 배열에 접근하는 방법을 배우겠습니다.
C 프로그래밍에서 포인터 배열은 각 요소가 int, char, float 등과 같은 다른 데이터 유형에 대한 포인터인 배열입니다. 이 랩에서는 정수 포인터 배열과 문자 포인터 배열에 접근하는 방법을 배우겠습니다.
이 단계에서는 정수 포인터 배열에 접근합니다. ~/project/ 디렉토리에서 main.c 파일을 열고 다음 코드를 붙여넣으세요:
#include <stdio.h>
// Global declaration.
const int MAX = 5;
int main() {
printf("\n\n\t\t==== Array of Integer Pointers ====\n\n\n");
int var[] = {10, 20, 30, 40, 50}; // initializing an array(here var) of int pointers
int i = 0;
/*
ptr is an array of int pointers i.e.
it stores the address of each array element
*/
int *ptr[MAX];
for(i = 0; i < MAX; i++) {
/*
Assign the address of each of the array
element to the ptr array
*/
ptr[i] = &var[i];
}
for(i = 0; i < MAX; i++) {
/*
ptr[i] stores the address of the element var[i].
Hence, *ptr[i] returns the value of the element
stored at location ptr[i]
*/
printf("Value of var[%d] = %i\n\n", i, *ptr[i]);
}
printf("\n\n\t\t==== End of Program ====\n\n\n");
return 0;
}
이 프로그램은 정수 포인터 배열을 초기화하고 각 배열 요소의 주소를 포인터 배열에 할당합니다. 그런 다음 포인터 배열을 사용하여 각 요소의 값을 출력합니다.
이 단계에서는 문자 포인터 배열에 접근합니다. 이전 단계의 코드 뒤에 다음 코드를 붙여넣으세요:
#include <stdio.h>
// Global declaration.
const int MAX = 4;
int main() {
printf("\n\n\t\t==== Array of Character Pointers ====\n\n\n");
char *names[] = {"Google", "Amazon", "Facebook", "Apple"}; // initializing an array(here names) of char pointers
int i = 0;
for(i = 0; i < MAX; i++) {
printf("Value of names[%d] = %s\n\n", i, names[i]);
}
printf("\n\n\t\t==== End of Program ====\n\n\n");
return 0;
}
이 프로그램은 문자 포인터 배열을 초기화하고 각 문자열의 첫 번째 문자의 주소를 포인터 배열에 할당합니다. 그런 다음 포인터 배열을 사용하여 각 요소의 전체 이름을 출력합니다.
이 단계에서는 프로그램을 컴파일하고 실행합니다. 터미널을 열고 ~/project/ 디렉토리로 이동합니다. 다음 명령을 입력하여 프로그램을 컴파일합니다:
gcc main.c -o main
컴파일이 성공적으로 완료되면 다음 명령을 입력하여 프로그램을 실행합니다:
./main
이 명령은 프로그램을 실행하고 터미널에 출력을 표시합니다:
==== Array of Integer Pointers ====
Value of var[0] = 10
Value of var[1] = 20
Value of var[2] = 30
Value of var[3] = 40
Value of var[4] = 50
==== Array of Character Pointers ====
Value of names[0] = Google
Value of names[1] = Amazon
Value of names[2] = Facebook
Value of names[3] = Apple
==== End of Program ====
#include <stdio.h>
// Global declaration.
const int MAX = 5;
int main() {
printf("\n\n\t\t==== Array of Integer Pointers ====\n\n\n");
int var[] = {10, 20, 30, 40, 50}; // initializing an array(here var) of int pointers
int i = 0;
/*
ptr is an array of int pointers i.e.
it stores the address of each array element
*/
int *ptr[MAX];
for(i = 0; i < MAX; i++) {
/*
Assign the address of each of the array
element to the ptr array
*/
ptr[i] = &var[i];
}
for(i = 0; i < MAX; i++) {
/*
ptr[i] stores the address of the element var[i].
Hence, *ptr[i] returns the value of the element
stored at location ptr[i]
*/
printf("Value of var[%d] = %i\n\n", i, *ptr[i]);
}
printf("\n\n\t\t==== Array of Character Pointers ====\n\n\n");
char *names[] = {"Google", "Amazon", "Facebook", "Apple"}; // initializing an array(here names) of char pointers
i = 0;
for(i = 0; i < MAX; i++) {
printf("Value of names[%d] = %s\n\n", i, names[i]);
}
printf("\n\n\t\t==== End of Program ====\n\n\n");
return 0;
}
이 랩에서는 정수 포인터 배열과 문자 포인터 배열에 접근하는 방법을 배웠습니다. 포인터 배열은 각 요소가 다른 데이터 유형에 대한 포인터인 배열입니다. 포인터 배열을 초기화하는 방법, 배열 요소의 주소를 포인터 배열에 할당하는 방법, 그리고 포인터 배열을 사용하여 배열 요소에 접근하는 방법을 배웠습니다.