2016-01-18 58 views
0

我正在使用C++编写一个学校项目。我正在尝试根据用户输入生成一个rgb颜色数组,以生成分形(不相关)的程序。动态初始化二维数组的输出错误

我发现需要使用指向二维数组的指针。指针具有全局范围,因为它需要在多个函数中的多个文件中访问。

指针声明没有问题。这是因为这样:

在 “global.h”

int **ptr; 
int palette[10][3]; 
//all the statements like #ifndef GLOBAL_H_INCLUDED, etc are present 

在空隙gen_array()的main.cpp

extern int **ptr; 
extern int palette[10][3]; 
void gen_array(); 
int main() 
{ 
    //initialize values of palette 
    //palette is basically list of colors that the user selects for the 
    //desired blend of colors to appear on the screen 

    gen_array(); 
} 

,在main.cpp中

void gen_array() 
{ 
    int i=0, size=0, size_arr=0; 
    //size is a variable helpful in calculating "size_arr", which is the size of the dynamic array to be initialized 

    while(palette[i][0]!=256) 
    { //calculates "size" } 

    for(int i=0; i<size-1; i++) 
    { 
     //to find "size_arr" 
    } 

    ptr= new int*[size_arr](); 
    for(int i = 0; i < size_arr; ++i) 
    { 
     ptr[i] = new int[3]; 
    } 

    //the following piece of code enter's values column-wise into the array 

    int s=0; 
    for(int i=0; i<size-1; i++) 
    { 
     for(int k=0; k<3; k++) 
     { 
      int x=-1, a=0, b=0; 

      a= palette[i][k] - palette[i+1][k]; 
      b= palette[i][k]; 
      if(a<0) 
      { 
       a= -a; 
       x=1; 
      } 
      for(int j=0; j<=a; j++, s++) 
      { 
       ptr[i][k] = b; 
       //cout<<*(*(ptr+i)+k)<<' '; 
       b= b+ x; 
      } 
      b= b-x; 

      for(int j=a+1; j<=diff[i]; j++, s++) 
      { 
       ptr[i][k] = b; 
       cout<<ptr[i][k]<<' '; 
      } 
      cout<<endl; 
     } 
    } 

    //this output's the array that the pointer points to, on the screen 
    for(int i=0; i<size_arr; i++) 
    { 
     for(int j=0; j<3; j++) 
     { 
      cout<<ptr[i][j]<<' '; 
     } 
     cout<<endl; 
    } 
} 

问题是,数据正在生成并正确输入阵列[在倒数第二个循环中],但输出时[最后一个for循环]我收到垃圾值。

从现在评论的cout语句获得的[正确值]输入的数据是this [打印的列],而输出的O/P是[打印的行] this。如果需要知道,“大小”(在这种情况下)= 2,size_arr = 256和调色板[0] = {0,0,255}和调色板[1] = {0,255,0} 。

任何人都可以指出问题是什么,以及它是否与指针初始化有关?

+0

发布的代码不能编译。请发布[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。 –

+1

'while(palette [i] [0]!= 256)''i'未初始化。 – PaulMcKenzie

+0

@PaulMcKenzie -it在int main()中完成() –

回答

0

即使不读像

for(int j=0; j<=a; j++, s++) 
{ 
    ptr[i][k] = b; 
    //cout<<*(*(ptr+i)+k)<<' '; 
    b= b+ x; 
} 

所有的程序代码,看起来显然是错误的。在每次迭代中,您正在写入ptr[i][k],并且循环不会更改ptr,ik。这意味着它将在循环过程中继续写入相同的位置。

+0

是的,它必须是's'而不是'i' ['k'仍然保持原样],然后我只需要再添加2个代码即可使其工作。谢谢。 –

0

看你的int我在你用来填充int ** ptr的循环中。如果size = 2,那么你永远不会将ptr提前到足以填充数组。考虑填充所述阵列是这样的:

for(int i = 0; i < size_arr; ++i){//for every pixel in the main array 
    for(int k = 0; k < 3; ++k){//for every subpixel 
    int my_value = ...;//calculate the value for this entry 
    *(*(ptr+i)+k) = my_value;//assign 

更好的是,使用命名像素结构包含UINT8 R,G,B和分配作为像素*的ptr =新像素[大小]和遍历像素和分配的ptr [I ] .R PTR [I] .G PTR [I] .B

+0

'i = 0; I <1;我++;该循环运行一次,这正是它需要运行以填充细节的次数。做'我

+1

您不是遍历数组中的每个元素。看看在分配值时如何索引到数组中。 '*(*(ptr + i)+ k)'如果我是1或2,你怎么遍历整个数组?你需要检查你在'ptr = new int * [size_arr];'中分配的每一个指针,然后遍历每一个分配给那些来自:for(int i = 0; i ghjdgfh