从函数C ++传递指针

时间:2013-11-24 15:50:16

标签: c++ arrays function pointers

我有一个文本文件,其中我读入了这样的2D数组:

 153 62 328 71 0.998263 0 0 0 0 0 0 0 0 0
 160 58 335 67 0.99699 0 0 0 0 0 0 0 0 0
 192 43 362 53 0.998 0 0 0 0 0 0 0 0 0
 224 34 389 45 0.994047 0 0 0 0 0 0 0 0 0

我只需要前四列。我创建了一个将数字存储到2D数组中的函数。我想将指向该函数的指针传递给指向数组第一个元素地址的main。

在main函数中,我尝试增加指针索引以再次从数组中获取值,但我无法获得正确的数字。很明显,我对2D数组指针的理解是有限的,我很感激任何帮助。

我的代码在这里:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <typeinfo>
#include <string>
#include <cstring>
using namespace std;

///////////////////////////////////////////////////////////////////////////////
int lines_in_pos_obj_file(int i = 0)
{
    string pos_obj_file_line;

    ifstream pos_obj_file("correspondencePOS000_sphere.txt");
    if(pos_obj_file.is_open())
    {
        while(getline(pos_obj_file, pos_obj_file_line))
        {
            i++;
        }
    }

    else cout << "Unable to open file";
    return(i);
}
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
int *get_pos_obj_positions(int k)
{
    int pos_obj_positions[k][4];
    int i = 1;

    string pos_obj_file_line;

    ifstream pos_obj_file("correspondencePOS000_sphere.txt");
    if(pos_obj_file.is_open())
    {

        pos_obj_file >> pos_obj_positions[0][0] >> pos_obj_positions[0][1] >> pos_obj_positions[0][2] >> pos_obj_positions[0][3];

        while(getline(pos_obj_file, pos_obj_file_line))
        {

            pos_obj_file >> pos_obj_positions[i][0] >> pos_obj_positions[i][1] >> pos_obj_positions[i][2] >> pos_obj_positions[i][3];

            i++;
        }
    }

    else cout << "Unable to open file";

    int (*pos_obj_pointer)[4] = &pos_obj_positions[0];
    return(pos_obj_pointer[0]);
}
///////////////////////////////////////////////////////////////////////////////

int main()
{   
    int pos_obj_lines;
    int *pos_obj_positions; 

    pos_obj_lines = lines_in_pos_obj_file(0);
    pos_obj_positions = get_pos_obj_positions(pos_obj_lines);

    for(int l = 0; l < pos_obj_lines; l++)
    {
    cout << pos_obj_positions[l] << endl;
    }

    return 0;
}

这是输出:

153
0
1670302324
32656
-1690506736
1
3
0
6
0
1672415896
32656

2 个答案:

答案 0 :(得分:3)

您正在返回指向局部变量int pos_obj_positions[k][4];的指针。函数完成后,变量将被销毁。

main中分配数组并将其传递给get_obj_pos_positions函数。

答案 1 :(得分:2)

我建议您将2D数组作为输入参数传递而不是返回值。或者更好的是使用矢量。

void get_pos_obj_positions(int k, int pos_obj_positions[][4])
{
    int i = 1;

    string pos_obj_file_line;

    ifstream pos_obj_file("correspondencePOS000_sphere.txt");
    if(pos_obj_file.is_open())
    {

        pos_obj_file >> pos_obj_positions[0][0] >> pos_obj_positions[0][1] >> pos_obj_positions[0][2] >> pos_obj_positions[0][3];

        while(getline(pos_obj_file, pos_obj_file_line))
        {

            pos_obj_file >> pos_obj_positions[i][0] >> pos_obj_positions[i][1] >> pos_obj_positions[i][2] >> pos_obj_positions[i][3];

            i++;
        }
    }

    else cout << "Unable to open file";
}

int main()
{   
    int pos_obj_lines;
    int **pos_obj_positions; 

    pos_obj_lines = lines_in_pos_obj_file(0);

    pos_obj_positions = new int[ pos_obj_lines ];
    for ( int I = 0; I < pos_obj_lines; I++ ) {
      pos_obj_positions = new int[4];
    }
    get_pos_obj_positions(pos_obj_lines,pos_obj_positions);

    return 0;
}