Thread: NULL returns every time?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    NULL returns every time?

    Hi,

    I have a code which takes some arrays and pointers for a linked list but returns every time NULL for head. I couldn't find a solution for it.

    Can you help me about this?

    Thanks.

    (I added my function)

    Code:
    int read_score(struct all_answers *curr, struct all_answers *head_t, struct all_answers *tail, int pick[], int guess[]){
      
      pm_unit pm_unit_0;
          
      pm_unit_0 = score(pick, guess);
      
      count++;
      printf("%d %d %d %d %d %d => +%d -%d\n", count, guess[0], guess[1], guess[2], guess[3], guess[4], pm_unit_0.plus, pm_unit_0.minus);
      
      answer[5] = pm_unit_0.plus; answer[6] = pm_unit_0.minus;
      answer[0] = guess[0]; answer[1] = guess[1]; answer[2] = guess[2]; answer[3] = guess[3]; answer[4] = guess[4];
      
      curr = (struct all_answers *)malloc(sizeof(struct all_answers));
      
      curr->answer_list[6] = answer[6];
      curr->answer_list[5] = answer[5];
      curr->answer_list[4] = answer[4];
      curr->answer_list[3] = answer[3];
      curr->answer_list[2] = answer[2];
      curr->answer_list[1] = answer[1];
      curr->answer_list[0] = answer[0];
      
      printf("curr->answer_list[4]: %d\n", curr->answer_list[4]);
      
      if(head_t == NULL){
        printf("NULL returned inside read_score for head\n");
        head_t = curr;
        curr->prev = NULL;
        if(head_t == NULL)
        printf("head returned NULL after assignment\n");
      }
      else{
        tail->next = curr;
        curr->prev = tail;
      }
      tail = curr;
      curr->next = NULL;
      
    
      
      printf("tail->answer_list[4]: %d\n", tail->answer_list[4]);
      
      return pm_unit_0.plus;
      
    }
    Attached Files Attached Files
    Last edited by Salem; 05-26-2011 at 04:25 AM. Reason: Inline the code

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You cannot pass anything to a function and expect to change that thing's value. You have to use a pointer to that type. So if you want your function to fill up the head pointer if it's NULL, then you need a pointer to that type:
    Code:
    void fill( struct foo **x )
    {
        if( x )
            *x = malloc( ... );
    }
    ...
    struct foo *head = NULL;
    foo( &head );
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you want the head to change (and likewise the tail), you need to pass a pointer to a pointer.

    Code:
    void foo ( int **p, int *q ) {
      *p = q;
    }
    int main ( ) {
      int myInt;
      int *p = NULL;
      foo( &p, &myInt );
      return 0;
    }
    Passing head (by value, as you are at the moment) means your local changes inside the function do NOT affect the caller.


    Edit:
    What he said ^^^
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setwindowshookex returns null
    By cloudy in forum Windows Programming
    Replies: 17
    Last Post: 04-07-2011, 10:23 AM
  2. mysql_use_result() returns NULL problem
    By Nordmar in forum C++ Programming
    Replies: 3
    Last Post: 11-15-2010, 07:41 AM
  3. mysql_use_result() returns NULL
    By mirekgn in forum C Programming
    Replies: 5
    Last Post: 03-28-2010, 01:05 PM
  4. fgets returns null
    By strider1974 in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 05:08 AM
  5. Function returns null value instead of zero
    By drdepoy in forum C Programming
    Replies: 3
    Last Post: 10-23-2005, 03:51 PM