Jump to content

  • Log In with Google      Sign In   
  • Create Account


Pointers -ARGH-


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 wiseGuy   Members   -  Reputation: 122

Like
Likes
Like

Posted 08 January 2000 - 12:33 PM

Okay folks, here''s the dilemma. I have two cpp source files in my project so far. One is main game routines and one has pathfinding algorithms. My problem is that I want to pass a pointer to an array to the FindPath() function, so that the function fills it out with directions. I moved findPath back into the main file, and tried this prototype: int findPath(int x1, int y1, int x2, int y2, int *directions[100], int *numDirections) and this code: if (.....) { directions[current]=whatever; //note there isn''t a * then later, outside the findPath function findPath(6,6,5,5, my_directions[100], &numDirs); //note there is no & /////// This is fine when both functions are in the same file, but when I seperate the files, i get problems and the program keeps crashing. I have to add *''s and add &''s which weren''t needed when the pathfinding function was in the same file as the main program. Is there another solution, such as references? I also tried using player_directions[100] as a global, but when it was changed in one file, it wasn''t changed in the other (i think). Anyway, if anyone has had the time to read this longgg message and knows what I am doing wrong, the I would be very grateful for your advice! Thanks all, wiseGuy

Sponsor:

#2 mason   Members   -  Reputation: 128

Like
Likes
Like

Posted 08 January 2000 - 12:56 PM

Which file a function's in shouldn't matter.

Forgive me if you know this already, but if a function is in a different file, you've gotta declare it. For example

<>
void func_a(void) { /* body of func a */ }

<>
// if file B wants to use something in file A, it must have
// a declaration:
void func_a(void);

Obviously, the declaration should have the same parameters (and in the same order) as the actual function.

Any good C book should have more info on how declarations work and why you need them.

BTW, you should really consider putting your functions in a class. A good C++ program has NO functions that aren't inside a class.

Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com

Edited by - mason on 1/8/00 6:57:31 PM

#3 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 08 January 2000 - 01:06 PM

You also don''t really want to pass the size of the array or use a pointer to an array.

An array is just a prettied up pointer. So in this context you''re using a pointer to a pointer

array[100]

is the same as

*(array + 100)

and int *array[100] (like you have) is *(*(array))
pretty ugly.

when you pass an array in a function you should just use the pointer. If you do something like

void function(int array[])
{

// now this works
array[100];
}

and then call function

function(sendThisArray[]);

things should work out right. I''m not exactly sure of where to use those brackets, I try to avoid arrays when possible, but you get the jist.

If you''re worried about the size of the array changing over two files, use something like

#define MAX_ARRAY_SIZE 200

in a header and use this for limit checking.

Hope this helps.


#4 logistix   Members   -  Reputation: 122

Like
Likes
Like

Posted 08 January 2000 - 01:07 PM

OOPS- That anonymous poster is me.

#5 PoesRaven   Members   -  Reputation: 122

Like
Likes
Like

Posted 08 January 2000 - 03:27 PM

that should be:
int Get100(int* Array)
{
return Array[100];
}

void func()
{
int my_array[200];
int i = func(my_array);
}

#6 RPGElite   Members   -  Reputation: 122

Like
Likes
Like

Posted 08 January 2000 - 03:55 PM

Note to Mason:

A good C++ program has 1 function not in a class, it might look familiar to you:

int main(int argc,char **argv);

-or-

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)

(note : my main() definition might be off by a bit, since it''s been a long time since I''ve done DOS stuff)

gameguru4@yahoo.com


#7 Notwen   Members   -  Reputation: 122

Like
Likes
Like

Posted 08 January 2000 - 05:52 PM

I would suggest something like the following prototype (c style..)


typedef struct {
int x;
int y;
} POINT, *PPOINT;

/*
FindPath fills the array pointed to by DirArray with directions to get from PointA to PointB. When called, the variable Size should point to an integer that contains the max size of DirArray. After FindPath returns, Size will be filled with the number of directions actually placed into the array.

If DirArray is not large enough to hold the complete list of instructions, CompletePath will be set to FALSE.

FindPath returns TRUE if it was able to compute a path (even if DirArray is not large enough to hold the complete path..) If it is unable to compute a path, FindPath returns FALSE.
*/

BOOL FindPath (POINT PointA, POINT PointB, int * DirArray, int * Size, BOOL * CompletePath);

So, you would call your function like this:
int size;
int ary[MAX_DIR_ARRAY];
BOOL fullpath;
POINT pointA, pointB;

/*
Fill in pointA i& pointB info with data..
*/

size = MAX_DIR_ARRAY;

if (FindPath (pointA, pointB, ary, &size, &fullPath)) {
//
// Do stuff with our computed path...
//

}






#8 CJ   Members   -  Reputation: 122

Like
Likes
Like

Posted 08 January 2000 - 09:50 PM

Mason
A good C++ program doesn't need to have all their functions in one class. Like, for myself, I have one file with all the initialization, for my window and DirectX stuff. All the rest of it is done in classes. Since I only use the DirectDraw stuff, I do the rest by myself. And the program is still good, at least as good as the rest of my stuff done with all classes.

Dance with me......

http://members.xoom.com/CJdeVos/index.htm

Edited by - CJ on 1/9/00 3:57:42 AM





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS