Ç - 字符数组和字符指针 [英] C - char array and char pointer

查看:157
本文介绍了Ç - 字符数组和字符指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我无法定义
数组

 的char ** PP = {123,456,789};

不过,我可以把它定义为一个char * [],并将其发送到将接受它作为一个char功能**

 的char * PP [] = {123,456,789};
有趣(PP);无效的乐趣(字符** pointerToPointer)
{
    // +(** pointerToPointer); //错误
    的printf(%S,* pointerToPointer);
}
//输出::123

和我为什么不能增加

  ++(** pointerToPointer);


解决方案

要回答第一个问题,如果我们用指针的一个深度的原则可能会更清晰。这code是非法出于同样的原因:

 为int * PTR = {1,2,3};

在C,一个支撑初始化列表是不是一个对象(尤其不是一个数组)。它只能被视为物品从其中一个对象时被初始化读取初始化的列表。

PTR 是一个对象,所以最多有一个初始化可以采取,而初始的预期形式是指针(其中 1 不是)。

事实上,这code是在C11明确非法6.7.9 / 11:


  

有关标量的初始化应该是单个前pression,可选括在大括号


不过,有一个GCC的bug /功能它允许多余的初始化一个标量和忽略它们。此外,一些编译器可能是有益的和唯一发出警告,并初始化 PTR 来指向地址 1 ,无论这可能是。

标是指一个对象,这不是一个结构或数组。


由于C99可以这样写:

 为int * PTR =(INT []){1,2,3};

这将创建一个数组(使用相同的存储时间为 PTR ),并分 PTR 在它的第一个元素。

此数组是可变的;一个不可变一用 INT常量* PTR =(INT常量[]){1,2,3}; 而不是


更换 INT 的char * ,我们看到,你可以写:

 的char ** p值=(字符* []){123,456,789};

在这种情况下的数组中的指针的是可变的,但它们指向的东西(即字符串),还是没有。

请注意,你应该总是用字符常量*与字符串文字打交道时,因为它们不是可变。该字符串文字具有类型事实的char [N] 是在常量加入C.因此,一个历史的宿醉

 字符常量** PP =(字符常量* []){123,456,789};

或与非可变指向字符串:

 字符常量* const的* PP =(字符常量* const的[]){123,456,789};

Why I can't define an array

char **pp={ "123", "456", "789" };

But I can define it as a char*[] ,and send it to a function that will accept it as a char **

char *pp[]={ "123", "456", "789" };
fun(pp);

void fun(char **pointerToPointer)
{
    //++(**pointerToPointer);//error
    printf("%s", *pointerToPointer); 
}
//output::"123"

And why I can't increment

++(**pointerToPointer);

解决方案

To answer the first question, the principles might be clearer if we use a single depth of pointer. This code is illegal for the same reason:

int *ptr = { 1, 2, 3 };

In C, a braced initializer list is not an object (especially not an array). It can only be taken as a list of items from which to read initializers when an object is being initialized.

ptr is one object, so at most one initializer could be taken, and the expected form of that initializer is a pointer (which 1 is not).

In fact this code is explicitly illegal under C11 6.7.9/11:

The initializer for a scalar shall be a single expression, optionally enclosed in braces

However, there is a gcc bug/feature where it permits excess initializers for a scalar and ignores them. Further, some compilers may "be helpful" and "only" issue a warning, and initialize ptr to point to address 1, wherever that might be.

"scalar" means an object that's not a struct or an array.


Since C99 you can write:

int *ptr = (int []){1, 2, 3};

which creates an array (using the same storage duration as ptr) and points ptr at its first element.

This array is mutable; for a non-mutable one use int const *ptr = (int const[]){1, 2, 3}; instead.


Replacing int by char *, we see that you could write:

char **p = (char *[]){ "123", "456", "789" };

in which case the pointers in the array are mutable, but the things they point to (i.e. the string literals) still aren't.

Note that you should always use char const * when dealing with string literals, because they are not mutable. The fact that string literals have type char [N] is a historical hangover from before const was added to C. So:

char const **pp = (char const *[]){ "123", "456", "789" };

or with non-mutable pointers to strings:

char const *const *pp = (char const *const []){ "123", "456", "789" };

这篇关于Ç - 字符数组和字符指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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