如何使用 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<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:
");
for(i=0 ;i<4 ;i++ )
printf("%s
",students[i]);
return 0;
}输出
The names of students are: bhanu ramu hari pinky
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP