用C声明和修改字符串 [英] Declaring and modifying strings in C

查看:108
本文介绍了用C声明和修改字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始尝试学习C语言编程。在我的第一个程序(简单的Hello World的事情)我在不同的方法来声明一个字符串后,我意识到我不能只是做 VARIABLE_NAME =字符串数据

I've recently started to try learn the C programming language. In my first program (simple hello world thing) I came across the different ways to declare a string after I realised I couldn't just do variable_name = "string data":

char *variable_name = "data"
char variable_name[] = "data"
char variable_name[5] = "data"

我不明白的是它们之间的区别。我知道他们是不同的,其中一人专门分配的内存来存储数据的量,但仅此而已,我觉得我需要用C移动到更复杂的概念之前,了解这里面了。

What I don't understand is the difference between them. I know they are different and one of them specifically allocates an amount of memory to store the data in but that's about it, and I feel like I need to understand this inside out before moving onto more complex concepts in C.

另外,为什么不使用 *变量名让我重新分配变量名到一个新的字符串,但 VARIABLE_NAME [数字] 变量名[] 不?当然,如果我给你,比如说,10个字节到它(字符变量名称[10] =数据)并尝试重新分配到的东西是10字节或更小它应该工作,所以为什么不呢?

Also, why does using *variable_name let me reassign the variable name to a new string but variable_name[number] or variable_name[] does not? Surely if I assign, say, 10 bytes to it (char variable_name[10] = "data") and try reassigning it to something that is 10 bytes or smaller it should work, so why doesn't it?

什么是空方括号和星号在干什么?

What are the empty brackets and the asterix doing?

推荐答案

在此声明

char *variable_name = "data";

有被宣布为指针。这个指针指向字符串数据的第一个字符。编译器将在记忆某些区域字符串文字和文字的第一个字符的地址分配指针。

there is declared a pointer. This pointer points to the first character of the string literal "data". The compiler places the string literal in some region of memory and assigns the pointer by the address of the first character of the literal.

您可以重新分配的指针。例如:

You may reassign the pointer. For example

char *variable_name = "data";
char c = 'A';

variable_name = &c;

但是,您可能不会改变字符串本身。试图改变在程序的不确定行为一个字符串结果。

However you may not change the string literal itself. An attempt to change a string literal results in undefined behaviour of the program.

在这些声明

char variable_name[] = "data";
char variable_name[5] = "data";

其中

有声明的两个数组元素被用于初始化字符串字符初始化。例如这个声明

there are declared two arrays elements of which are initialized by characters of used for the initialization string literals. For example this declaration

char variable_name[] = "data";

是等效于以下

char variable_name[] = { 'd', 'a', 't', 'a', '\0' };

该数组将有5个元素。所以这个声明完全euivalent的声明

The array will have 5 elements. So this declaration is fully euivalent to the declaration

char variable_name[5] = "data";

是有区别的,如果你会指定数组的一些其他大小。例如:

There is a difference if you would specify some other size of the array. For example

char variable_name[7] = "data";

在这种情况下,该阵列将被初始化以下方式

In this case the array would be initialized the following way

char variable_name[7] = { 'd', 'a', 't', 'a', '\0', '\0', '\0' };

这是没有明确的初始化是零初始化数组中的所有元素。

That is all elements of the array that do not have explicit initializers are zero-initialized.

讲究,在C你可以使用一个字符串字面量按以下方式声明一个字符数组

Pay attention to that in C you may declare a character array using a string literal the following way

char variable_name[4] = "data";

这是字符串的结束零不会放到数组中为止。
在C ++这样的声明是无效的。

that is the terminating zero of the string literal is not placed in the array. In C++ such a declaration is invalid.

当然,如果你愿意,你可以改变数组中的元素(如果它没有被定义为一个常量数组)。

Of course you may change elements of the array (if it is not defined as a constant array) if you want.

考虑到,你可以附上一个字符串用作括号的初始账户。例如:

Take into account that you may enclose a string literal used as an initializer in braces. For example

char variable_name[5] = { "data" };

在C99,也可以使用所谓的目的地初始化。例如:

In C99 you may also use so-called destination initializers. For example

char variable_name[] = { [4] = 'A', [5] = '\0' };

下面是一个示范项目

#include <stdio.h>
#include <string.h>

int main(void) 
{
    char variable_name[] = { [4] = 'A', [5] = '\0' };

    printf( "%zu\n", sizeof( variable_name ) );
    printf( "%zu\n", strlen( variable_name ) );

    return 0;
}

程序输出为

6
0

在YPU使用标准的C函数的strlen 在头部声明&LT;文件string.h&GT; 你得到它返回0因为precede元件具有索引4数组的第一元素是零初始化

When ypu apply standard C function strlen declared in header <string.h> you get that it returns 0 because the first elements of the array that precede the element with index 4 are zero initialized.

这篇关于用C声明和修改字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆