关于typedef定义数组类型的问题

Gzd2003 2008-11-16 02:01:14
存在以下程序:
#include<iostream>
using namespace std;
typedef int[10] array;
void main()
{
array a={12,34,56,78,90,98,76,85,64,43};
array &b=a;
a[2]=100;
for(int i=0;i<10;i++)
cout<<b[i]<<" ";
}

我感觉typedef int[10] array;应该写成typedef int[10] array;因为a数组是10个元素的int类型数组,所以我觉得应该写成typedef int[10] array;结果编译出错,错误信息如下:
--------------------Configuration: Test - Win32 Debug--------------------
Compiling...
CopyTest.cpp
d:\Test\CopyTest.cpp(4) : warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared
d:\Test\CopyTest.cpp(4) : error C2143: syntax error : missing ';' before '['
d:\Test\CopyTest.cpp(4) : error C2143: syntax error : missing ';' before '['
d:\Test\CopyTest.cpp(7) : error C2065: 'array' : undeclared identifier
d:\Test\CopyTest.cpp(7) : error C2146: syntax error : missing ';' before identifier 'a'
d:\Test\CopyTest.cpp(7) : error C2065: 'a' : undeclared identifier
d:\Test\CopyTest.cpp(7) : error C2059: syntax error : '{'
d:\Test\CopyTest.cpp(7) : error C2143: syntax error : missing ';' before '{'
d:\Test\CopyTest.cpp(7) : error C2143: syntax error : missing ';' before '}'
d:\Test\CopyTest.cpp(8) : error C2065: 'b' : undeclared identifier
d:\Test\CopyTest.cpp(8) : error C2106: '=' : left operand must be l-value
d:\Test\CopyTest.cpp(9) : error C2109: subscript requires array or pointer type
d:\Test\CopyTest.cpp(9) : error C2106: '=' : left operand must be l-value
d:\Test\CopyTest.cpp(11) : error C2109: subscript requires array or pointer type
Error executing cl.exe.

Test.exe - 13 error(s), 1 warning(s)


我的理解哪里出了问题?为什么typedef int[10] array;反而是对的?谢谢。
...全文
620 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
maochongsandai110 2010-07-31
  • 打赏
  • 举报
回复
myarr是一个array类型的数组,即一个二维数组。
Yanger_xy 2009-06-19
  • 打赏
  • 举报
回复
那 array myarr[2]又代表什么意思呢?!
toadzw 2008-11-16
  • 打赏
  • 举报
回复
typedef不能算是真的类型定义,只能算是别名;
HenryCCG 2008-11-16
  • 打赏
  • 举报
回复
typedef int[10] array改为typedef int array[10]就可以了

建议你去看看typedef吧,它不是简单的替换
如果是简单的替换的话,那么直接用 #define 不就可以了

编程-鸟人-_-- 2008-11-16
  • 打赏
  • 举报
回复
#include <iostream> 
using namespace std;
typedef int array[10] ; //这样定义就可以了.
void main()
{
array a={12,34,56,78,90,98,76,85,64,43};
array &b=a;
a[2]=100;
for(int i=0;i <10;i++)
cout <<b[i] <<" ";
}

zbihong 2008-11-16
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
typedef int array;
void main()
{
array a[10]={12,34,56,78,90,98,76,85,64,43};
array *b=a;
a[2]=100;
for(int i=0;i <10;i++)
cout<<b[i]<<endl;
}
这样就行了!!
  • 打赏
  • 举报
回复
array改这样定义

typedef int array[10];

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧