什么是“数组名”的字符指针数组的情况下,是什么意思? [英] What does the 'array name' mean in case of array of char pointers?

查看:105
本文介绍了什么是“数组名”的字符指针数组的情况下,是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的code:

 的char * str中[] = {forgs,做,不是,死};
 的printf(%D的sizeof(STR)的sizeof(海峡[0]));

我得到的输出为 12 2 ,所以我的疑惑是:


  1. 为什么有区别吗?

  2. 双方 STR 海峡[0] 是字符指针,对吧?


解决方案

在大多数情况下,一个阵列名称将衰减到其第一元素的地址的值,以及与类型为相同的指针的元素类型。所以,你会期望一个光秃秃的 STR 有等于值&放大器;海峡[0] 型指针指针字符

然而,这不是sizeof的为的情况。在这种情况下,数组名一直为类型的sizeof ,这将是4指针数组字符

的sizeof的的返回类型是为size_t 。如果你有一个C99编译器,可以使用%祖在格式字符串打印由的sizeof 返回的值。

In my code:

 char *str[] = {"forgs", "do", "not", "die"};
 printf("%d %d", sizeof(str), sizeof(str[0]));  

I'm getting the output as 12 2, so my doubts are:

  1. Why is there a difference?
  2. Both str and str[0] are char pointers, right?

解决方案

In most cases, an array name will decay to the value of the address of its first element, and with type being the same as a pointer to the element type. So, you would expect a bare str to have the value equal to &str[0] with type pointer to pointer to char.

However, this is not the case for sizeof. In this case, the array name maintains its type for sizeof, which would be array of 4 pointer to char.

The return type of sizeof is a size_t. If you have a C99 compiler, you can use %zu in the format string to print the value returned by sizeof.

这篇关于什么是“数组名”的字符指针数组的情况下,是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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