字符的区别** P,字符* P [],CHAR P [] [] [英] Difference between char **p,char *p[],char p[][]

查看:196
本文介绍了字符的区别** P,字符* P [],CHAR P [] []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char *p = "some string"   

创建了一个指针p指向包含字符串块。

creates a pointer p pointing to a block containing the string.

char p[] = "some string" 

创建一个字符数组,并在它的文字。

creates a character array and with literals in it.

和第一个是一个常数declaration.Is它相同的二维阵列的

And the first one is a constant declaration.Is it the same of two-dimensional arrays?

就是

char **p,char *p[],char p[][]. 

我读了一些关于这一点,焦炭** P创建数组的指针,因此相比于字符p的开销[] [] 用于存储指针值

前两个声明创建不断arrays.i没有得到任何运行时错误,当我试图修改的argv 的内容主( INT ARGC,字符** argv的)。是不是因为他们在函数原型声明?

the first two declarations create constant arrays.i did not get any run time error when i tried to modify the contents of argv in main(int argc,char **argv). Is it because they are declared in function prototype?

推荐答案

的char ** P; 声明了一个指针的指针,以字符。它保留了指针的空间。它不保留任何空间指向的指针或任何字符

Normal Declarations (Not Function Parameters)

char **p; declares a pointer to a pointer to char. It reserves space for the pointer. It does not reserve any space for the pointed-to pointers or any char.

的char * P [N]; 声明中的 N 的指针数组字符。它保留空间的 N 的指针。它不预留任何空间任何字符 N 必须明确隐式让编译器计数初始化提供或在与初始化定义。

char *p[N]; declares an array of N pointers to char. It reserves space for N pointers. It does not reserve any space for any char. N must be provided explicitly or, in a definition with initializers, implicitly by letting the compiler count the initializers.

字符P [M] [N]; 声明数组的 M N 的<$ C数组$ C>字符。它保留空间的 M 的• N 字符。有没有涉及指针。 M N 必须明确隐式让编译器计数初始化设置,或在定义与初始化。

char p[M][N]; declares an array of M arrays of N char. It reserves space for MN char. There are no pointers involved. M and N must be provided explicitly or, in a definition with initializers, implicitly by letting the compiler count the initializers.

的char ** P 声明了一个指针的指针,以字符。当调用该函数时,空间提供了一种用于该指针(通常在一个堆栈或在一个寄存器)。没有足够的空间保留给指针指向的指针或任何字符

char **p declares a pointer to a pointer to char. When the function is called, space is provided for that pointer (typically on a stack or in a processor register). No space is reserved for the pointed-to-pointers or any char.

的char * P [N] 调整为的char ** P ,所以它是一样的以上。 N 被忽略,可能是不存在的。

char *p[N] is adjusted to be char **p, so it is the same as above. N is ignored and may be absent.

字符P [M] [N] 调整为字符(* P)[N] ,所以它是一个指针数组的 N 字符 M 被忽略,可能是不存在的。 N 必须提供。当调用该函数时,空间被提供给指针(通常在一个堆栈或在一个寄存器)。没有空间保留给数组的 N 字符

char p[M][N] is adjusted to be char (*p)[N], so it is a pointer to an array of N char. M is ignored and may be absent. N must be provided. When the function is called, space is provided for the pointer (typically on a stack or in a processor register). No space is reserved for the array of N char.

的argv 。它充满了数据,该软件从环境获得。你被允许修改它的字符数据。

argv is created by the special software that calls main. It is filled with data that the software obtains from the "environment". You are allowed to modify the char data in it.

在你的定义的char * p =一些字符串; ,则不允许修改数据 P 点,因为C标准说,在一个字符串的字符可以不进行修改。 (从技术上讲,它说的是,如果你尝试没有定义的行为。)在这个定义中, P 不是数组;它是一个指针到第一个字符在一个阵列,而那些字符是一个字符串字母,你是不允许修改字符串的内容字面

In your definition char *p = "some string";, you are not permitted to modify the data that p points to because the C standard says that characters in a string literal may not be modified. (Technically, what it says is that it does not define the behavior if you try.) In this definition, p is not an array; it is a pointer to the first char in an array, and those char are inside a string literal, and you are not permitted to modify the contents of a string literal.

在你的定义字符P [] =一些字符串; ,您可以修改 P 内容。他们不是一个字符串文字。在这种情况下,字符串实际上并不存在,在运行时;它只是用来指定如何数组 P 初始化的东西。一旦 P 被初始化,您可以修改它。

In your definition char p[] = "some string";, you may modify the contents of p. They are not a string literal. In this case, the string literal does not actually exist at run-time; it is only something used to specify how the array p is initialized. Once p is initialized, you may modify it.

数据设置为的argv 设置的方式,允许你修改它(因为C标准规定这一点)。

The data set up for argv is set up in a way that allows you to modify it (because the C standard specifies this).

这篇关于字符的区别** P,字符* P [],CHAR P [] []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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