อาร์เรย์ของพอยน์เตอร์ (เป็นสตริง)
Array of pointers คืออาร์เรย์ที่มีองค์ประกอบเป็นตัวชี้ไปยังที่อยู่พื้นฐานของสตริง
มีการประกาศและเริ่มต้นดังนี้ -
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" ข้อดี
-
ยกเลิกการเชื่อมโยงอาร์เรย์สองมิติของอักขระ ใน (อาร์เรย์ของสตริง) ในอาร์เรย์ของตัวชี้ไปยังสตริงนั้นไม่มีขนาดหน่วยความจำคงที่สำหรับการจัดเก็บ
-
สตริงใช้ไบต์ได้มากเท่าที่ต้องการ จึงไม่เปลืองเนื้อที่
ตัวอย่างที่ 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