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

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

问题描述

几个星期前,我开始学习编程语言C.我在网络技术,如HMTL / CSS,Javscript,PHP和基本的服务器管理知识,但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)字符字符串名称[]; 结果
B)的char *字符串名称;

我得到的字符字符串名称[]; 是一个字符数组。然而,第二行混淆了我。据我了解,第二行使得指针变量。是不是指针变量应该是另一个变量的内存地址?

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语言中,一个串是,如你所说,字符的数组。内置到C规格最字符串函数期望字符串是NUL终止,这意味着最后一个字符串的字符 0 。不是code再presenting数字为零,但 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};

当您使用字符的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或字符数组的地址。请注意或这句话,是要知道的情况是什么,是你,程序员,很重要的。

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天全站免登陆