Question

I can't figure out why the below code returns a pointer 0x1. The stock node is found fine, as the following output shows:

ticket_name passed comparison.
ticket_type passed comparison.
ticket_zone passed comparison.

But the returned pointer is 0x1 and I'm trying to compare that pointer to pointers in a linked list to delete a node, which is obviously failing. There is a warning when compiling about the function too:

tm_options.c:85: warning: assignment makes pointer from integer without a cast
tm_options.c:190: warning: assignment makes pointer from integer without a cast

As far as I'm aware this warning is because a function lacks a function prototype, and the default return type for functions without prototypes is int. But I have a function prototype in tm_utility.h

EDIT: heres a pastebin of my Makefile just incase that's something to do with it http://pastebin.com/UFw9B4Hd

struct stock_node * find_ticket(tm_type_ptr tm, char *, char, char *);

The following is the function which is returning the error value:

struct stock_node * find_ticket(tm_type * tm, char * ticketName, char ticketType, char * ticketZone) {

  struct stock_node * curr;
  BOOLEAN found = FALSE;

  curr = tm->stock->head_stock;

  while (curr != NULL && found == FALSE) {

    printf("curr: %p\n", (void *) curr);
    printf("curr->ticket_name: %s\n", curr->data->ticket_name);
    printf("curr->ticket_type: %c\n", curr->data->ticket_type);
    printf("curr->ticket_zone: %s\n", curr->data->ticket_zone);

      if (strncmp((char *)curr->data->ticket_name, ticketName, TICKET_NAME_LEN + 1) == 0) {

          printf("ticket_name passed comparison.\n");

          if ((char) curr->data->ticket_type == ticketType) {

              printf("ticket_type passed comparison.\n");

              if (strncmp((char *)curr->data->ticket_zone, ticketZone, TICKET_ZONE_LEN + 1) == 0) {

        printf("ticket_zone passed comparison\n");

        found = TRUE;
                return curr;

          }

       }

    } else {

       curr = curr->next_node;

    }

 }

 return NULL;

}

Any help would be greatly appreciated. If you need more information please don't hesistate to ask me.

The calling code is:

struct stock_node * stockNode;
((stockNode = find_ticket(tm, ticketName, ticketType, ticketZone) != NULL))

As requested: struct stock_node definition:

typedef struct stock_node
{
    struct stock_data * data;
    struct stock_node * next_node; 
} stock_node;

Also, tm_type_ptr:

typedef struct tm * tm_type_ptr;
Was it helpful?

Solution

I had an issue with a assignment statement in an if block that also had a comparison statement.

((foundPtr = find_ticket(tm, ticketName, ticketType, ticketZone) != NULL));

As opposed to

((foundPtr = find_ticket(tm, ticketName, ticketType, ticketZone)) != NULL);

Crazy error. Simple solution. Thanks for your help guys

OTHER TIPS

What seems most likely is that you get stack corruption, and are actually returning the value of the 'found' variable, which is set to true the line before returning (which is probably defined as '1'). The source of the stack corruption is probably elsewhere. I'd say, run in a debugger or in Valgrind.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top