Skip to main content
gamedev.net
🔒 Locked

Array Question

Started by Ructions 4 replies 600+ views
Original Post
Ructions
Ructions
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
Tree Penguin
Tree Penguin
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);
}
Ructions
Ructions
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();
Luke Miklos
Luke Miklos
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);
.
}
Whatsoever you do, do it heartily as to the Lord, and not unto men.
SiCrane
SiCrane
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.)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.