C语言程序:打印指向字符串的指针数组及其地址
首先,让我们了解一下C语言中的指针数组。
指针数组:(指向字符串)
它是一个数组,其元素是指向字符串基地址的指针。
声明和初始化如下:
char *a[ ] = {"one", "two", "three"};这里,a[0]是指向字符串“one”基地址的指针。
a[1]是指向字符串“two”基地址的指针。
a[2]是指向字符串“three”基地址的指针。

优点
下面解释了指针数组的优点:
与二维字符数组不同,在字符串数组和指向字符串的指针数组中,存储没有固定的内存大小。
字符串只占用所需字节数,因此不会浪费空间。
示例
下面给出了演示打印指向字符串的指针数组及其地址概念的C语言程序:
#include<stdio.h>
#include<string.h>
void main(){
//Declaring string and pointers, for loop variable//
int i;
char *a[5]={"One","Two","Three","Four","Five"};
//Printing values within each string location using for loop//
printf("The values in every string location are :
");
for(i=0;i<5;i++){
printf("%s
",a[i]);
}
//Printing addresses within each string location using for loop//
printf("The address locations of every string values are :
");
for(i=0;i<5;i++){
printf("%d
",a[i]);
}
}输出
执行上述程序后,将产生以下结果:
The values in every string location are: One Two Three Four Five The address locations of every string values are: 4210688 4210692 4210696 4210702 4210707
示例2
让我们考虑另一个示例。
下面是一个演示指向字符串的指针数组概念的C语言程序:
#include<stdio.h>
#include<string.h>
void main(){
//Declaring string and pointers//
char string[10]="TutorialPoint";
char *pointer = string;
//Printing the string using pointer//
printf("The string is : ");
while(*pointer!='\0'){
printf("%s",*pointer);
pointer++;
}
}输出
执行上述程序后,将产生以下结果:
The string is: TutorialPoint
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP