문자열에 대한 포인터 배열
포인터 배열은 요소가 문자열의 기본 주소에 대한 포인터인 배열입니다.
다음과 같이 선언하고 초기화합니다 -
char *a[3 ] = {"one", "two", "three"};
//Here, a[0] is a ptr to the base add of the string "one"
//a[1] is a ptr to the base add of the string "two"
//a[2] is a ptr to the base add of the string "three" 장점
-
2차원 문자 배열의 연결을 해제합니다. (문자열 배열)에서 문자열에 대한 포인터 배열에는 저장을 위한 고정된 메모리 크기가 없습니다.
-
문자열은 필요한 만큼의 바이트를 차지하므로 공간 낭비가 없습니다.
예시 1
#include<stdio.h>
main (){
char *a[5] = {“one”, “two”, “three”, “four”, “five”};//declaring array of pointers to
string at compile time
int i;
printf ( “The strings are:”)
for (i=0; i<5; i++)
printf (“%s”, a[i]); //printing array of strings
getch ();
} 출력
The strings are: one two three four five
예시 2
문자열에 대한 포인터 배열에 대한 또 다른 예를 고려하십시오 -
#include <stdio.h>
#include <String.h>
int main(){
//initializing the pointer string array
char *students[]={"bhanu","ramu","hari","pinky",};
int i,j,a;
printf("The names of students are:\n");
for(i=0 ;i<4 ;i++ )
printf("%s\n",students[i]);
return 0;
} 출력
The names of students are: bhanu ramu hari pinky