You are not logged in.

#1 2011-10-23 17:28:29

ickabob
Member
Registered: 2010-06-21
Posts: 9

[SOLVED]C pointer question

I'm having a terrible time tracking down what exactly the issue is here in my code.  To me, at first it seemed like a simple missed assignment, but after tinkering in gdb I realize there is something deeper going on here that I don't understand. 

Here is the local function in which the problem occurs.

static void 
CountryStorageinsert(void *self, void *data){
  if (self = 0)
    return;
  CountryStorage *rs = (CountryStorage *) self;
  Record *r = (Record *) data;
  
  rs->countries[r->id] = r;
  if (r->id > rs->maxid)
    rs->maxid = r->id;
  rs->nrecords++;
}

The issue is that no matter what the value the void *self is passed in with, the assignment to *rs is ALWAYS zero.  Additionally in my project there are three such functions, CountryStorageinit, CountryStorageinsert, CountryStoragebackup which all have similar constructs all have the same problem.  I can not seem to pass in a void pointer and cast it to a compound structure pointer.

What is even more puzzling is I can manually, in gdb, get the behaviour I originally expected this code to have.  That is I can "(gdb) set rs = (CountryStorage *) self" and then examine rs, and vioala it has the address that self contained in the function parameters.
I would really appreciate it if someone could tell me if my thoughts on how pointer coercion works are wrong.
Thanks.

Last edited by ickabob (2011-10-23 17:55:11)

Offline

#2 2011-10-23 17:41:37

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [SOLVED]C pointer question

Classic C mistake;

  if (self = 0)

as opposed to

 if (self == 0)

Offline

#3 2011-10-23 17:45:11

ickabob
Member
Registered: 2010-06-21
Posts: 9

Re: [SOLVED]C pointer question

I am so ashamed!

OMGOMGOMG!!!! THANK YOU SO MUCH I WOULD HAVE NEVER NOTICED.  I just startled my roomated with obscenities.

Thanks Turtle.

Offline

Board footer

Powered by FluxBB