了解声明 C 字符串的两种方式 [英] Understanding two ways of declaring a C string

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

问题描述

几周前,我开始学习编程语言 C.我对 HMTL/CSS、Javscript、PHP 和基本服务器管理等 Web 技术有一定的了解,但 C 让我很困惑.据我了解,C 语言没有字符串的数据类型,只有字符,但我可能错了.

A few weeks ago I started learning the programming language C. I have knowledge in web technologies like HMTL/CSS, Javscript, PHP, and basic server administration, but C is confusing me. To my understanding, the C language does not have a data type for strings, just characters, however I may be wrong.

我听说有两种声明字符串的方法.这两行声明字符串有什么区别:

I have heard there are two ways of declaring a string. What is the difference between these two lines of declaring a string:

a.) char stringName[];
b.) char *stringName;

我知道 char stringName[]; 是一个字符数组.但是,第二行让我感到困惑.据我了解,第二行是一个指针变量.指针变量不应该是另一个变量的内存地址吗?

I get that char stringName[]; is an array of characters. However, the second line confuses me. To my understanding the second line makes a pointer variable. Aren't pointer variables supposed to be the memory address of another variable?

推荐答案

在 C 语言中,字符串"如您所说,是 char 的数组.C 规范中内置的大多数字符串函数都期望字符串是NUL 终止",这意味着字符串的最后一个 char0.不是表示数字零的代码,而是 0 的实际值.

In the C language, a "string" is, as you say, an array of char. Most string functions built into the C spec expect the string to be "NUL terminated", meaning the last char of the string is a 0. Not the code representing the numeral zero, but the actual value of 0.

例如,如果你的平台使用 ASCII,那么下面的字符串"就是ABC":

For example, if you're platform uses ASCII, then the following "string" is "ABC":

char myString[4] = {65, 66, 67, 0};

当您使用 char varName[] = "foo" 语法时,您是在堆栈上分配字符串(或者如果它在全局空间中,您是在全局分配它,但是不是动态的.)

When you use the char varName[] = "foo" syntax, you're allocating the string on the stack (or if its in a global space, you're allocating it globally, but not dynamically.)

与您可能使用过的许多其他语言相比,C 中的内存管理更加手动.特别是,有一个指针"的概念.

Memory management in C is more manual than in many other langauges you may have experience with. In particular, there is the concept of a "pointer".

char *myString = "ABC"; /* Points to a string somewhere in memory, the compiler puts somewhere. */

现在,char * 是指向 char 或 char 数组的地址".请注意该语句中的或",对于程序员来说,了解情况是什么很重要.

Now, a char * is "an address that points to a char or char array". Notice the "or" in that statement, it is important for you, the programmer, to know what the case is.

确保您执行的任何字符串操作不超过分配给指针的内存量也很重要.

It's important to also ensure that any string operations you perform don't exceed the amount of memory you've allocated to a pointer.

char myString[5];
strcpy(myString, "12345"); /* copy "12345" into myString. 
                            * On no! I've forgot space for my nul terminator and 
                            * have overwritten some memory I don't own. */

"12345" 实际上是 6 个字符长(不要忘记末尾的 0),但我只保留了 5 个字符.这就是所谓的缓冲区溢出",是许多严重错误的原因.

"12345" is actually 6 characters long (don't forget the 0 at the end), but I've only reserved 5 characters. This is what's called a "buffer overflow", and is the cause of many serious bugs.

[]"和*"之间的另一个区别是创建一个数组(如您所料).另一个不保留任何空间(除了用于保存指针本身的空间.)这意味着在您将其指向您知道有效的地方之前,不应使用指针的值来读取或写入.

The other difference between "[]" and "*", is that one is creating an array (as you guessed). The other one is not reserving any space (other than the space to hold the pointer itself.) That means that until you point it somewhere that you know is valid, the value of the pointer should not be used, for either reading or writing.

另一点(评论中有人提出)

Another point (made by someone in the comment)

您不能将数组作为参数传递给 C 中的函数.当您尝试时,它会自动转换为指针.这就是为什么我们传递指向字符串的指针而不是字符串本身

You cannot pass an array as a parameter to a function in C. When you try, it gets converted to a pointer automatically. This is why we pass around pointers to strings rather than the strings themselves

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

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