紧急问题:Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

godaga 2002-06-27 09:53:14
本程序的目的是为了让此C程序 在VC里编译通过运行

对于b[][] vc编译一直提示出错。。。。。。。。。

请帮忙 谢谢





// temp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int convert(int b[]);

int main(int argc, char* argv[])
{
int a[3][6];

a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 4;
a[1][1] = 5;
a[1][2] = 6;
a[2][0] = 7;
a[2][1] = 8;
a[2][2] = 9;

convert( a );
return 0;
}

int convert(int b[])
{
int i,j;
int num;

for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
num = b[i*3+j];
printf("hello %d\n",num);
}

return 1;
}


错误提示::\My Documents\zej\temp\temp.cpp(22) : error C2664: 'convert' : cannot convert parameter 1 from 'int [3][6]' to 'int []'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
...全文
1310 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangrk 2002-06-27
  • 打赏
  • 举报
回复
#include "stdafx.h"
int convert(int *);
int main(int argc, char* argv[])
{
int a[3][6];

a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 4;
a[1][1] = 5;
a[1][2] = 6;
a[2][0] = 7;
a[2][1] = 8;
a[2][2] = 9;

convert( &a[0][0] );
return 0;
}

int convert(int *b)
{
int i,j;
int num;

for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
num = b[i*3+j];
printf("hello %d\n",num);
}

return 1;
}
林雨亭 2002-06-27
  • 打赏
  • 举报
回复
convert( int b[]) --> convert ( int b[][6])
or
-->convert( int ** b)
num = b + 3 * i + j;

try it.
zhao_sh 2002-06-27
  • 打赏
  • 举报
回复
因为在主函数中是二维数组,而在convert(int b[])中的参数是一维数组,因此会出错其方法是把convert(int b[])改为convert(int *b)
shornmao 2002-06-27
  • 打赏
  • 举报
回复
类型不匹配错误,使用C风格的
convert ((int *)a)
或者C++风格的
convert (reinterpret_cast <int *> (a))
的强制转型。

70,027

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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