以下程序中的转换错误是什么? [英] What is the conversion error coming in the following program ?
本文介绍了以下程序中的转换错误是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下程序借助指针将3D数组转换为1D数组。一些转换错误即将到来。错误发生的行包含赋值运算符,指向两侧指向int类型的指针。
The following program converts 3D array into 1D array with help of pointers . Some conversion error is coming . The line in which error is coming contains assignment operator with pointer to pointer to int type on both sides .
#include<iostream>
using namespace std ;
int main()
{
// REPLACING 3D ARRAY WITH A 2D ARRAY AND THEN INTO A 1D ARRAY USING POINTERS .
int abc [2][2][3] ;
int **p[3][2] ;
int *c[6] ;
// // abc gives address of first element of 3d array ie first 2d array .
// abc is pointer to pointer to pointer to int type .
int i , j ; // * abc represents address of first 1d array of first 2d array .
for (i=0 ; i<=2 ; i++) // *abc +1:address of second 1d array of first 2d
{ // array .
for (j=0 ; j<=1 ; j++)
{
p[i][j] = *(abc+i ) + j ; // Conversion error comes here , but WHY ?
}
}
for (i=0 ; i<=5 ; i++)
{
for (j=0 ; j<=1 ; j++ )
{
c[i] = *p[i][j] ;
}
}
// entering array elements .
for (i=0 ; i<=5 ; i++)
{
cin>>* c[i] ;
}
// required array elements .
for (i=0 ;i<=5 ;i++)
{
cout<<*c[i]<<" "; // 3d array elements are accessed using 1d array
} // of pointers .
}
我的尝试:
我试过在dev c ++上运行这个程序,但显示以下错误:
[错误]无法转换'int(*)[3]分配中的'to'int **'
What I have tried:
I have tried to run this program on dev c++ but displays following error :
[Error] cannot convert 'int (*)[3]' to 'int**' in assignment
推荐答案
很明显,当你理解数组访问中的机制时。你的abc是一个三维整数数组。
有效代码是:
It is pretty obvious, when you would understand the mechanismins in array access. Your abc is an 3-dimensional array of integers.
valid code is:
int value = abc[i][j][k] ;//get value with array access
int *pointer = &(abc[i][j][k]);//get pointer
使用数组访问更容易,避免使用大指针arhitmetics
It is easier to work with the array access and avoid big pointer arhitmetics
这篇关于以下程序中的转换错误是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文