字符数组称为字符串。
声明数组的语法如下-
char stringname [size];
例如-char string [50]; 长度为50个字符的字符串
使用单字符常量-
char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}
使用字符串常量-
char string[10] = “Hello”:;
访问-控制字符串“%s”用于访问字符串,直到遇到“ \ 0”为止。
现在,让我们了解C编程语言中的指针数组是什么。
它是一个数组,其元素是字符串基本添加的ptrs。
它被声明和初始化如下-
char *a[ ] = {“one”, “two”, “three”};
此处,a [0]是指向字符串“ one”的基本加法的指针。
a [1]是指向字符串“ two”的基本加法的指针。
a [2]是指向字符串“三”的基加的指针。
以下是一个C程序,演示了字符串的概念-
#include<stdio.h> #include<string.h> void main(){ //Declaring string and pointers// char *s="Meghana"; //Printing required O/p// printf("%s\n",s);//Meghana// printf("%c\n",s);//如果您使用%c,则字符串应为*。别的你 will see no output//// printf("%c\n",*s);//M because it's the character in the base address// printf("%c\n",*(s+4));//Fifth letter a because it's the character in the (base address+4)th position// printf("%c\n",*s+5);//R because it will consider character in the base address + 5 in alphabetical order// }输出结果
执行以上程序后,将产生以下结果-
Meghana P M a R
考虑另一个例子。
下面给出的是一个C程序,它使用后增量和前增量运算符演示打印字符的概念-
#include<stdio.h> #include<string.h> void main(){ //Declaring string and pointers// char *s="Meghana"; //Printing required O/p// printf("%s\n",s);//Meghana// printf("%c\n",++s+3);//s becomes 2nd position - 'e'. O/p is Garbage value// printf("%c\n",s+++3);//s becomes 3rd position - 'g'. O/p is Garbage value// printf("%c\n",*++s+3);//s = 3递增1 ='h'.s成为第4 position.h+3 - k is the O/p// printf("%c\n",*s+++3);//s=4 - h is the value. h=3 = k will be the O/p. S is incremented by 1 now. s=5th position// }输出结果
执行以上程序后,将产生以下结果-
Meghana d d k k