C中的字符串

"本文讲解了C语言中如何使用char数组模拟字符串,以及如何通过索引操作、null终止符和sizeof/sprintf等函数进行字符串处理。重点介绍了字符串数组的创建、访问、修改和创建方式的区别,以及为何需要字符。"
摘要由CSDN通过智能技术生成

摘别人的,记录一下:

重点:

C语言中没有数组,但是可以用char array代替。

在创建char array时,如果赋值双引号意味着创建string类型,会在末尾自动增加\0用以区别字符串的末尾。

sizeof()函数计算字符串长度,包括结尾的\0

srtlen()函数计算字符的个数,不包括结尾的\0

char string[]="111";

字符串的名称string代表首元素的地址,&string 也代表字符串的首地址,但表征异议不同,一个代表元素的首地址,一个代表整体的首地址。如果进行加一运算的时候编译器会做处理如下:

string+1 指向字符串中第二个元素的地址

&string+1指向字符串后面变量的地址,会跳过整个字符串

Strings

Strings are used for storing text/characters.

For example, "Hello World" is a string of characters.

Unlike many other programming languages, C does not have a String type to easily create string variables. However, you can use the char type and create an array of characters to make a string in C:

char greetings[] = "Hello World!";

Note that you have to use double quotes.

To output the string, you can use the printf() function together with the format specifier %s to tell C that we are now working with strings:

Example

char greetings[] = "Hello World!";
printf("%s", greetings);

Try it Yourself »


Access Strings

Since strings are actually arrays in C, you can access a string by referring to its index number inside square brackets [].

This example prints the first character (0) in greetings:

Example

char greetings[] = "Hello World!";
printf("%c", greetings[0]);

Try it Yourself »

Note that we have to use the %c format specifier to print a single character.


Modify Strings

To change the value of a specific character in a string, refer to the index number, and use single quotes:

Example

char greetings[] = "Hello World!";
greetings[0] = 'J';
printf("%s", greetings);
// Outputs Jello World! instead of Hello World!

Try it Yourself »


Another Way Of Creating Strings

In the examples above, we used a "string literal" to create a string variable. This is the easiest way to create a string in C.

You should also note that you can to create a string with a set of characters. This example will produce the same result as the one above:

Example

char greetings[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};
printf("%s", greetings);

Try it Yourself »

Why do we include the \0 character at the end? This is known as the "null termininating character", and must be included when creating strings using this method. It tells C that this is the end of the string.


Differences

The difference between the two ways of creating strings, is that the first method is easier to write, and you do not have to include the \0 character, as C will do it for you.

You should note that the size of both arrays is the same: They both have 13 characters (space also counts as a character by the way), including the \0 character:

Example

char greetings[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!', '\0'};
char greetings2[] = "Hello World!";

printf("%lu\n", sizeof(greetings));   // Outputs 13
printf("%lu\n", sizeof(greetings2));  // Outputs 13

#include<stdio.h>

int main()

{

    int arr[] = { 1, 2, 3, 4, 5, 6, 7 };//数组随着下标的增长,地址是由低到高变化点

    printf("%p\n",arr);

    数组名就是数组首元素地址,是一样的。见附图1

    printf("%p\n",&arr[0]);

    但有两个意外

    1.sizeof(数组名) - 数组名表示整个数组 ,计算的是整个数组的大小,单位为字节(byte)

    int sz = sizeof(arr);

    printf("%d\n",sz);// 4(元素大小)* 7(元素个数)== 28,效果见附图 2

    2.&(数组名),数组名代表整个数组,&数组名,取的是整个数组的地址

    printf("%p\n", &arr);//表示数组的起始地址,是整个数组的地址

    上下两者的值一样,但 意义完全不同,效果图 见附图 3

    printf("%p\n", arr);//首元素的地址

    //进一步论证  &arr 和 arr的意义完全不同,不同在哪里

    printf("%p\n", &arr+1);//地址增加28字节,一个元素4byte,7个元素28字节,意味着 &arr + 1 跳过一整个数组的元素,指向最后一个元素后面的地址

    printf("%p\n", arr+1);//地址增加4字节,意味着 arr +1,跳过一个元素,改地址 arr+1,指向第二个元素

    效果见附图 4

    return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值