how to make a pointer to an array? or how to return an array?

Hello I am currently making a program that reads from a text file and a bunch of questions and answers and puts them on screen for a practice test for my class.
My teacher is lazy and for my ISP had me make a program that he can test future classes with.

anyways I have a class that reads the file and I am having trouble returning the array of strings that I get from reading the file. I have tried making a pointer to the array so that I can access it from my main function but I am having problems.

When I try this it gives me an error.. cannot convert int[3] to *int
which obviously means I cant make a pointer to the array. But I am wondering if I am doing it wrong or there is a different way.

This is just some practice code.. not my program :p

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main()
{
    int fish[3] = {5,2,3};
    cout<<fish[0]<<','<<fish[1]<<','<<fish[2]<<endl;
    int *tuna = &fish;
    cout<<tuna<<endl;
}



when you make a pointer to an array you should define the element, if you dont the pointer points to the first element of array, in your case :->
1
2
 int *tuna = fish;
 cout<<*tuna<<endl;


if you want to access other elements you may use tuna[ index ] or *( tuna + index )
Last edited on
I want to be able to read the array from my main function. when the array was made in a different class. without having to make a for/while loop or returns. and was wondering if this was possible.
Last edited on
Can you give a more concrete example of what you talking about? Like a function that has an array you want to do something with?
ok this is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Answers::getString()
{

    string str[getLine()];
    int counter = 0;
    fi.open("test.txt");
    if(fi.good())
    {
        while(!fi.eof())
        {
            getline(fi,str[counter]);
            cout<<str[counter]<<endl;
            counter++;
        }
    }
    fi.close();
}


the getLine() function gets the number of lines in a file.

I want to return the array str to my main function so that I can display it on screen. or do somthing with it in my main function.
Last edited on
This definition

string str[getLine()];

is invalid because C++ requires that size of array be a constant expression. You can use such construction provided that your function was declared with constexpr specifier.

As for your problem you can allocate dynamically your array and return a pointer to it. The better approach is to use std::vector<std::ctring> defined in the main and pass it by reference to your function.
Last edited on
it compiles fine and works fine... so its not invalid...
getLine() returns an int. this is the size of the array.
each index in the array represents a line.

heres the code for getLine().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int Answers::getLine()
{
    int lineCount = 0;
    string str;
    fi.open("test.txt");
    if(fi.good())
    {
        while(getline(fi,str))
        {
            lineCount++;
        }
    }
    fi.close();
    return lineCount;
}


could you explain that second part more clearly please. maybe some example code?
The easies way to solve your problem is to use stl collection, such as vector where you can add new element or remove an element.
a simple example for your case ->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

void Answers::getString( vector<string>& list)
{

   
  String str;
    fi.open("test.txt");
    if(fi.good())
    {
        while(!fi.eof())
        {
            getline(fi,str);
            cout<<str<<endl;
            list.push_back(str);
        }
    }
    fi.close();
}


your main may be some thing like ->
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(){
  vector<string> list;
  getString(list); 

 // here you can do whatever you wish with your list of strings
// for instance
        for(int i = 0 ; i < list.size() ; i++){
         cout<<list.at(i)<<endl;
       }
 // or use iterator to do other things

}
it compiles fine and works fine... so its not invalid...


It is incorrect C++. Your compiler is not quite a C++compiler and lets it through. It's not necessarily a problem, but you should know when you're coding in C++ and when you're not.

i use the GNU GCC compiler what would you call a c++ compiler?

Also i have never used the vector class before.. is there documentation of it on MSDN? or somewhere?
Last edited on
Topic archived. No new replies allowed.