Archived

This topic is now archived and is closed to further replies.

Array Question

This topic is 5011 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have used OO in my code. I want to pass the address of a two diminsion array to a function which reads in from the file and places the contents in that array. my array is char HighScoreNames[10][200]; so i want to pass the address of this out of one function and into another one. But i seem to be doing it wrong could anyone help Please Ructions

Share this post


Link to post
Share on other sites
The address of the array
char HighScoreNames[10][200]
is
HighScoreNames

use it like this:

char HighScoreNames[10][200];

void donothing(char **thearray){
}

void callthefunction(){
donothing(HighScoreNames);
}

Share this post


Link to post
Share on other sites
Well what doesnt really help.
What i am doing in my init function is

File.HighScoreInput(FileName,
GameVariables.ReturnHighScoreNamesAddress());

so i am passing the address of the array from the GameVariables Class to the File Class.

But i can seen to get it working.

How would you implement this and in the HighScoreInput function how do you assign the strings from the file to the array

ie
i am using gets();
strcpy();

Share this post


Link to post
Share on other sites
you can''t just pass a double pointer & expect the function to know what to do with it, because essentially... de-referencing a double pointer requires that the method knows all but one field of the array size. What de-referencing does is this, ex:

say you have this:
int num[10][10]; //double array of ints
//& you do this
int variable = num[3][5];

this means that variable = *(*num + 3*10 + 5)

or something like that... basically, it has to know that the first index goes up to ten. So if you just pass it a double pointer, you canNOT use it like this:

stringToCopyTo[10][200];
function((char**)stringtoCopyTo);
function(char** stringToCopyTo){
int nameNum = 4; //arbitrarily chose 4 here
.
.
strcpy( stringToCopyTo[nameNum], stringFromFile );
.
.
}

because it wouldn''t know that its suppose to advance 4 * 200 characters ahead in memory, your compiler shouldn''t allow you do this. You have to do 1 of 2 things, treat it as a single array in the function & just KNOW that its actually a double array of 10 groups of 200 chars, so something like this:
if(nameNum<10){
strcpy( &((*stringToCopyTo)[nameNum*200]), stringFromFile);
}

or you can pass it into the function like this, as a pointer to char arrays of a certain size, but NOT a double pointer:

//FYI, its something LIKE this, I''m writing this from memory, so
//the syntax might be a little off, its a cheap hack I think
function( char *[200] names)
{
.
.
strcpy( names[nameNum],fromFile);
.
}

Share this post


Link to post
Share on other sites
Assuming you''re talking C++ here, it might just be easier to wrap your high score array in a high score class and do operations on the class instead. e.g.:

class HighScores {
public:
void ReadFromFile(const char * file_name);

char HighScoreNames[10][200];
};


(Even for C it might help to wrap the array in a struct and pass the struct around.)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
why not use strings, instead of char. easier to use and very functional. here is a simple example.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string scores[10];

void load_scores()
{
fstream tmp;
tmp.open("../scores.txt" );
int i = 0;
while(!tmp.eof()){
tmp >> scores[i];
i++;
}
tmp.close();
}

void show_scores()
{
int i = 0;
while(scores[i]!=""){
cout << scores[i] << endl;
i++;
}
}

int main()
{
for(int i=0;i<10;i++){ scores[i] = ""; } // clear array.

load_scores();
show_scores();
return 0;
}

Share this post


Link to post
Share on other sites