Server Help

ASSS Questions - baffled by this coding

Reaem - Wed Mar 12, 2008 4:13 pm
Post subject: baffled by this coding
Code: Show/Hide
void Cbuyship(const char *tc, const char *params, Player *p, const Target *target)
{
   int ship = atoi(params);
   if (ship < 1 || ship > 8)
      return;
   
   ship--; //for convenience
   
   //cost checking
   if (!Spend_Pts(p, ship_costs[ship]))
      return;
   else
   {
      void *v = HashGetOne(docks, p->name);
      dock d;
      d = (dock)*v;
      
      if (!d)
      {
         d = {0,0,0,0,0,0,0,0};
         d[ship] = 1;
         HashReplace(docks, p->name, d);
      }
      else
      {
         d[ship] = d[ship] + 1;
      }
   }
}


I get the following errors:
../RTZ/RTZ.c: In function `Cbuyship':
../RTZ/RTZ.c:190: warning: dereferencing `void *' pointer
../RTZ/RTZ.c:190: error: cast specifies array type
../RTZ/RTZ.c:194: error: syntax error before '{' token

../RTZ/RTZ.c:198: error: syntax error before "else"

190 is the line with d = (dock)*v;

The problem is, I'm not sure what to do with the void pointer to cast it into the dock object (If I even need to do that).
dock is just a simple: typedef int dock[8];

I've been programming for awhile, but I'm not certified or even a real c veteran and the void pointer cast to dock type is really owning me.
Thanks in advance,
-R
Dr Brain - Wed Mar 12, 2008 4:26 pm
Post subject:
what's wrong with d = v?

You can't initialize non-static arrays like that on like 194.
Anonymous - Wed Mar 12, 2008 6:44 pm
Post subject:
d=v doesn't work, I've tried countless variations of castings and allocation orders and nothing I do can make this work.
Same incompatible types error
Bak - Wed Mar 12, 2008 6:58 pm
Post subject:
Code: Show/Hide

void *v = HashGetOne(docks, p->name);
  int* d = (int*)v;
  int x;
 
  if (!d)
  {
    d = new int[8];
   
    for (x = 0; x < 8; ++x)
      d[x] = 0;
   
    d[ship] = 1;
    HashReplace(docks, p->name, d);
  }
  else
  {
    d[ship] = d[ship] + 1;
  }


just make sure the memory gets freed somewhere
Samapico - Wed Mar 12, 2008 11:56 pm
Post subject:
how about
Code: Show/Hide

d = (int*)v;


d is a dock object, or a pointer to an int (int*).
So you want to convert a void* to a int*
(int*)v should do just that.

And the array initialization doesn't work either. If you really need the values to be 0, you could use ZeroMemory
something like:
Code: Show/Hide
ZeroMemory(d,8*sizeof(int) );

Smong - Thu Mar 13, 2008 9:16 am
Post subject:
Code: Show/Hide
d = (dock)*v;

This might work:
Code: Show/Hide
d = *(dock*)v;

But as pointed out before the rest of your code is a little iffy anyway.
Anonymous - Thu Mar 13, 2008 5:19 pm
Post subject:
Yeah, the void pointers and arrays by themselves I can deal with, but putting them together scrambles my brain.
I eventually just decided to make it a struct similar to BotData with an array in it =( Maybe I can optimize this later on but I want to get a working server soon.
Thx everyone
tcsoccerman - Thu Mar 13, 2008 6:58 pm
Post subject:
if this is in devcpp, i've had problems where i forget to make the projecta "c" project instead of a "c++" project. i have spent countless hours trying to figure out the problem..
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group