Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data structure in a given function prototype

Status
Not open for further replies.

sedawk

Programmer
Feb 5, 2002
247
US
I have a given function prototype in which I need to design a data structure to complete it:

Code:
typedef struct {
      int x, y;
} locationT;

typedef enm{North, South, East, West} directionT;

int OccupiedOrNot(locationT loc, directionT dir);

The function OccupiedOrNot() will return 1 if there is something at the direction of location loc, otherwise 0 is returned. Of course, at some locations something, say 'X' have been marked already.

In order to do this, I define another data structure:
Code:
typedef struct {
       locationT locT;
       char ch;
} elementT;

// then something like this:
int OccupiedOrNot(locationT loc, directionT dir)
{
      elementT eleT;
      // read in marked locations to eleT by calling  
      // another function void readlocation(filename)
      if (eleT.ch == 'X') return(1);
      else return(0);
}

But for every given location, OccupiedOrNot() needs to read the file to decide if marked or not. If I want the new defined structure elementT to store all marked location globally in advance, then I don't know how to pass it to OccupiedOrNot().

I think this approach is not good enough. The key is the first code snippet can't not be changed. You are free to modify the data structure in the second code snippet.
 


How big is your grid? If it is relatively small, say 256x256, you could have an array, say

#define XMAX 256
#define YMAX 256
char grid[XMAX][YMAX];

Initialize with 0

memset (grid, 0, sizeof (grid));

Then, in your function add a switch statement to compute the coordinate to be tested and check it in the grid. There isn't any need for another data structure. Different story if your grid is 10000x10000. You'll probably need to cache some of it to disk in that case.
 
Pass a pointer to a structure:
Code:
int OccupiedOrNot(locationT* ploc, directionT dir)
{
  elementT eleT;
  if (!ploc) /* Pass 0 if no need in elementT value */
     ploc = &eleT;
  /* read in marked locations to eleT by calling  */
  /* another function void readlocation(filename) */
  return ploc->ch == 'X';
}
Now you can store structure value in a callee context if you wish. See also true C-style returned value calculation...
 
I can't use either of them since the given prototype has to be maintained. Therefore, an extra data structure is needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top