#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0

    How am I getting NULL here?


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct {
    	int calories;
    	char name;
    } food;
    
    int compare(const void *a,const void *b)
    {
    	food x = **(food **)a;
    	food y = **(food **)b;
    	printf("%d %d ",x.calories,y.calories); //check the values
    	return x.calories - y.calories;
    }
    
    int main()
    {
    	food apples = {45,'a'};
    	food burgers = {35,'b'};
    	food cereal = {24,'c'};
    	food *data[] = {&burgers,&apples,&cereal};
    	qsort(data,3,sizeof(food),compare);
    	printf("%d",data[0]->calories);
    	getchar();
    	return 0;
    }
    I want it to sort according to how many calories are from smallest to largest. But with this code it seems the always middle value of data seems to be NULL. I'm confused, can somebody please tell me why?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    Oh yeah, forgot to mention. It works when I remove the data type in the structure, the char. When I add it it doesn't work.

    Weird, right?
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,366
    Rep Power
    1870
    You'll have to post screen shots, because I can't see anything wrong, and it seems to work.
    Code:
    $ cat bar.c
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct {
      int calories;
      char name;
    } food;
    
    int compare(const void *a,const void *b)
    {
      food x = **(food **)a;
      food y = **(food **)b;
    //  printf("%d %d ",x.calories,y.calories); //check the values
      return x.calories - y.calories;
    }
    
    int main()
    {
      food apples = {45,'a'};
      food burgers = {35,'b'};
      food cereal = {24,'c'};
      food *data[] = {&burgers,&apples,&cereal};
      qsort(data,3,sizeof(food),compare);
      printf("%d\n",data[0]->calories);
      printf("%d\n",data[1]->calories);
      printf("%d\n",data[2]->calories);
      return 0;
    }
    $ gcc -Wall bar.c
    $ ./a.out 
    24
    35
    45
    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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    First where do you allocate any memory for the following pointer?
    Code:
    food *data[] = {&burgers,&apples,&cereal};
    Looking at your code you don't need that pointer, nor the ampersands.

    Next please explain these lines:
    Code:
    	food x = **(food **)a;
    	food y = **(food **)b;
    What are you actually trying to accomplish?


    Jim
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    101
    Rep Power
    0
    Hmm, Salem's compiler compiles the code correctly but mine doesn't and gives no errors.

    Solution: My compiler sucks. I should install a new one. Yet I probably won't.

    Thanks salem
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,366
    Rep Power
    1870
    > qsort(data,3,sizeof(food),compare);
    Hah - this is wrong.
    It should be
    qsort(data,3,sizeof(food[0]),compare);

    It only worked on my machine because it's 64-bit, where pointers are 8 bytes, and the happy coincidence is the size of the struct is also 8 bytes.

    > It works when I remove the data type in the structure, the char. When I add it it doesn't work.
    Yes, removing the char makes the struct the same size as a pointer on your machine.
    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

IMN logo majestic logo threadwatch logo seochat tools logo