为什么char []和char *作为typedef不同,但有时...不是? [英] Why are char[] and char* as typedefs different, but sometimes... not?
问题描述
我在此问题约 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中,隐式转换 发生的是:
- 当数组表达式是
sizeof
(sizeof arr
产生数组的大小,而不是指针的大小);
- 当数组表达式是操作数of unary
&
(& arr
是指向数组的指针,而不是指向指针的指针) ;和
- 当数组表达式是用于初始化数组类型(
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[]
andchar*
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 theirtypedef
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:
- When the array expression is the operand of
sizeof
(sizeof arr
yields the size of the array, not the size of a pointer);- When the array expression is the operand of unary
&
(&arr
is a pointer-to-array, not a pointer-to-pointer); and- When the array expression is a string literal used to initialize an object of array type (
char s[] = "hello";
initializess
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*
tof2
.Inside
f2
, the parameter objectsx
andy
are both of typechar*
, sostd::is_same<decltype(x), decltype(y)>::value
is true.But the types
ar
andpr
are distinct.ar
is an incomplete array typechar[]
, andpr
is the pointer typechar*
.Which explains your program's output. The weirdness happens because the parameter
x
, which you defined with the array typear
, is really of typechar*
, which is the same type aspr
.这篇关于为什么char []和char *作为typedef不同,但有时...不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!