为什么char []和char *作为typedef不同,但有时...不是? [英] Why are char[] and char* as typedefs different, but sometimes... not?

查看:203
本文介绍了为什么char []和char *作为typedef不同,但有时...不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此问题约 char [] char * 的差异。

  #include< iostream> 

typedef char ar []
typedef char * pr;
void f2(ar x,pr y)
{
std :: cout<< std :: is_same< decltype(x),decltype(y)> :: value<< '\\\
';
std :: cout<< std :: is_same< ar,pr> :: value< '\\\
';
}

int main()
{
char data [] =data;
char * ptr = data;
f2(data,ptr);
return 0;
}

输出 -425.0.28))

  1 
0
decltype() s?我的怀疑是,由于他们的 typedef 声明,它们实际上是不同的类型,但为什么变量报告为相同 type?

解决方案

在C ++中,和C一样,声明为数组类型的参数是(在编译时)为指针类型,特别是指向数组的元素类型的指针。



无论数组类型是直接指定还是通过typedef(记住,typedef不会创建一个新类型,只是现有类型的别名)。



这样:

  typedef char ar []; 
typedef char * pr;
void f2(ar x,pr y)
{
// ...
}

真的意味着:

  void f2(char * x,char * y)另一个规则是,如果一个数据库中有一个数据库,也由C和C ++共享,是在大多数但不是所有上下文中,数组类型的表达式被隐式地转换为指向数组对象的第一个元素的指针。这意味着如果你定义一个数组对象:

  char arr [10] 

您可以使用该对象的名称作为函数的参数, c> char * 参数(它丢失了边界信息)。



在C中,隐式转换 发生的是:


  1. 当数组表达式是 sizeof sizeof arr 产生数组的大小,而不是指针的大小);

  2. 当数组表达式是操作数of unary & & arr 是指向数组的指针,而不是指向指针的指针) ;和

  3. 当数组表达式是用于初始化数组类型( char s [] =hello)的对象的字符串文字时; s 初始化为数组,而不是指针)。

无这些情况(或C ++中发生的其他情况)出现在您的程序中,因此您的调用:

  ptr); 

传递 char * f2



x y 均为 char * std :: is_same< decltype(x),decltype(y)> :: value 为true。



但是类型 ar pr 是不同的。 ar 是不完整的数组类型 char [] pr 是指针类型 char *



这说明了程序的输出。奇怪的情况发生是因为你定义的数组类型 ar 的参数 x 真的是类型 char * ,其类型与 pr 相同。


The following observation arose as I was following this question about char[] and char* differences.

#include <iostream>

typedef char ar[];
typedef char* pr;
void f2(ar x, pr y)
{
    std::cout << std::is_same<decltype(x), decltype(y)>::value << '\n';
    std::cout << std::is_same<ar, pr>::value << '\n';
}

int main()
{
    char data[] = "data";
    char *ptr = data;
    f2(data,ptr);
    return 0;
}

Output ( on Apple LLVM version 4.2 (clang-425.0.28) )

1
0

Why do these report as different types, but not different decltype()s ? My suspicion is they are in fact different types due to their typedef declarations, but then why are variables reported as the same type?

解决方案

In C++, as in C, a parameter that's declared to be of array type is adjusted (at compile time) to be of pointer type, specifically a pointer to the array's element type.

This happens whether the array type is specified directly or via a typedef (remember that a typedef doesn't create a new type, just an alias for an existing type).

So this:

typedef char ar[];
typedef char* pr;
void f2(ar x, pr y)
{
    // ...
}

really means:

void f2(char* x, char* y)
{
    // ...
}

Another rule, also shared by C and C++, is that an expression of array type is, in most but not all contexts, implicitly converted to a pointer to the first element of the array object. Which means that if you define an array object:

char arr[10];

you can use the name of that object as an argument to a function that takes a char* parameter (which loses the bounds information).

In C, the cases where this implicit conversion doesn't happen are:

  1. When the array expression is the operand of sizeof (sizeof arr yields the size of the array, not the size of a pointer);
  2. When the array expression is the operand of unary & (&arr is a pointer-to-array, not a pointer-to-pointer); and
  3. When the array expression is a string literal used to initialize an object of array type (char s[] = "hello"; initializes s as an array, not as a pointer).

None of these cases (or the other cases that occur in C++) appear in your program, so your call:

f2(data,ptr);

passes two pointer values of type char* to f2.

Inside f2, the parameter objects x and y are both of type char*, so std::is_same<decltype(x), decltype(y)>::value is true.

But the types ar and pr are distinct. ar is an incomplete array type char[], and pr is the pointer type char*.

Which explains your program's output. The weirdness happens because the parameter x, which you defined with the array type ar, is really of type char*, which is the same type as pr.

这篇关于为什么char []和char *作为typedef不同,但有时...不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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