如何使用C语言为字符串创建指针?
指针数组(指向字符串)
指针数组是一个数组,其元素是指向字符串基地址的指针。
它的声明和初始化如下-
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输出结果main (){ char *a[5] = {“one”, “two”, “three”, “four”, “five”};//declaringarrayofpointersto string at compile time int i; printf ( “The strings are:”) for (i=0; i<5; i++) printf (“%s”, a[i]); //printingarrayofstrings getch (); }
The strings are: one two three four five
示例2
考虑另一个关于字符串指针数组的例子-
#include输出结果#include int main(){ //initializingthepointerstringarray 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
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语