char *array 和 char 数组[] [英] char *array and char array[]

查看:17
本文介绍了char *array 和 char 数组[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我写这个

 char *array = "关于音乐的一件好事";

我实际上创建了一个数组?我的意思是这样的吗?

char array[] = {"One", "good", "thing", "about", "music"};

解决方案

声明与初始化

char *array = 关于音乐的一件好事";

声明一个指针array并使其指向一个31个字符的常量数组.

声明和初始化

char array[] = "One, good, thing, about, music";

声明一个字符数组,包含 31 个字符.

是的,数组的大小是 31,因为它包括终止 '' 字符.


放在内存中,第一次是这样的:

<上一页>+--------+ +-----------------+|数组 |--> |关于音乐的一件好事" |+--------+ +-----------------+

第二个是这样的:

<上一页>+------------------------------+|关于音乐的一件好事" |+------------------------------+


数组衰减为指向数组第一个元素的指针.如果你有一个像

这样的数组

char array[] = "One, good, thing, about, music";

然后在需要指针时使用纯 array,它与 &array[0] 相同.

这意味着,例如,当您将数组作为参数传递给函数时,它将作为指针传递.

指针和数组几乎可以互换.例如,您不能使用 sizeof(pointer) 因为它返回实际指针的大小,而不是它指向的内容.当你这样做时,例如&pointer 你得到指针的地址,但是 &array 返回一个指向数组的指针.需要注意的是,&arrayarray(或其等效的 &array[0]very 不同>).&array&array[0] 都指向同一个位置,但类型不同.使用上面的数组,&array 的类型是 char (*)[31],而 &array[0] 的类型是char *.


为了更有趣:众所周知,在访问指针时可以使用数组索引.但是由于数组衰减为指针,因此可以对数组使用一些指针算法.

例如:

char array[] = "Foobar";/* 声明一个包含 7 个字符的数组 */

通过上述方法,您可以使用任一方法访问第四个元素('b' 字符)

数组[3]

*(数组 + 3)

而且因为加法是可交换的,所以最后也可以表示为

*(3 + 数组)

这导致了有趣的语法

3[数组]

if I write this

 char *array = "One good thing about music";

I actually create an array? I mean it's the same like this?

char array[] = {"One", "good", "thing", "about", "music"};

解决方案

The declaration and initialization

char *array = "One good thing about music";

declares a pointer array and make it point to a constant array of 31 characters.

The declaration and initialization

char array[] = "One, good, thing, about, music";

declares an array of characters, containing 31 characters.

And yes, the size of the arrays is 31, as it includes the terminating '' character.


Laid out in memory, it will be something like this for the first:

+-------+     +------------------------------+
| array | --> | "One good thing about music" |
+-------+     +------------------------------+

And like this for the second:

+------------------------------+
| "One good thing about music" |
+------------------------------+


Arrays decays to pointers to the first element of an array. If you have an array like

char array[] = "One, good, thing, about, music";

then using plain array when a pointer is expected, it's the same as &array[0].

That mean that when you, for example, pass an array as an argument to a function it will be passed as a pointer.

Pointers and arrays are almost interchangeable. You can not, for example, use sizeof(pointer) because that returns the size of the actual pointer and not what it points to. Also when you do e.g. &pointer you get the address of the pointer, but &array returns a pointer to the array. It should be noted that &array is very different from array (or its equivalent &array[0]). While both &array and &array[0] point to the same location, the types are different. Using the array above, &array is of type char (*)[31], while &array[0] is of type char *.


For more fun: As many knows, it's possible to use array indexing when accessing a pointer. But because arrays decays to pointers it's possible to use some pointer arithmetic with arrays.

For example:

char array[] = "Foobar";  /* Declare an array of 7 characters */

With the above, you can access the fourth element (the 'b' character) using either

array[3]

or

*(array + 3)

And because addition is commutative, the last can also be expressed as

*(3 + array)

which leads to the fun syntax

3[array]

这篇关于char *array 和 char 数组[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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