Thread: Error with a pointer

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    46

    Error with a pointer

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void menu();
    
    // struct containing recipe
    typedef struct {
     char recipeName[40];
     char recipeHowTo[1000];
     int howManyPerson;
     int level;
     int timeToEat;
    } recipe;
    
    // struct for list
    struct element {
     recipe data;
     struct element *elemPtr;   
    };
    
    // prototypes
    void menu(int choice, struct element *listPtr);
    struct element *insertRecipe(struct element *listPtr);
    void listRecipes(struct element *listPtr);
    
    int main() {  
      int choice;
      struct element *list = NULL;  
      
      menu(choice,list);
      
      
      system("PAUSE");	
      return 0;
    }
    
    void menu(int choice, struct element *listPtr){
        printf("----Recipes for fun\n");
        printf(".1 Search Recipe");
        printf(".2 View Recipes");
        printf(".3 Insert Recipe");
        printf(".4 Edit Recipe");
        printf(".5 Delete Recipe");
        printf(".8 Set measure system");
        printf(".9 Email Recipes"); 
        printf("Insert the number corrispondant to what you need and press ENTER!   ");
        scanf("%d",choice);
     
     if ( choice == 1 ) { }
     else if ( choice == 2 ) { listRecipes(listPtr); }
     else if ( choice == 3 ) { listPtr = insertRecipe(listPtr); }
     else if ( choice == 4 ) { }
     else if ( choice == 5 ) { }
     else if ( choice == 8 ) { }
     else if ( choice == 9 ) { }
     else { }
    }
    
    // INSERT RECIPE
    struct element *insertRecipe(struct element *listPtr){
        
        //var declairation
        char recipeName[40];  
        char recipeHowTo[1000];
        int howManyPerson;
        int level;
        int timeToEat;
        recipe x;
        struct element *xPtr;
        
        //filling info
        printf("Insert the Recipe name:\n");
        scanf("%s\n",recipeName);
        strcopy(x.recipeName,recipeName);
        printf("Insert the how to:\n");
        scanf("%s\n",recipeHowTo);
        strcopy(x.recipeHowTo,recipeHowTo);
        printf("Insert for how many person is the recipe");
        scanf("%d\n",howManyPerson);
        x.howManyPerson = howManyPerson;
        printf("Insert the level of the recipe from 1 to 5");
        scanf("%d\n",level);
        x.level = level;
        printf("Insert the time needed to prepare the recipe");
        scanf("%d\n",timeToEat);
        x.timeToEat = timeToEat;
        
        if (*listPtr != NULL) {
             xPtr = (struct element *)malloc(sizeof(struct element)); //allocate mem
             xPtr->data = x; //put x in data of the pointer
             xPtr->elemPtr = listPtr; //put xptr on top list
        }
        else {
            listPtr = (struct element *)malloc(sizeof(struct element));
            listPtr->data = x;
            listPtr->elemPtr = NULL; //listPtr points to end list
            listPtr->xPtr;
        }
        return(listPtr);    
        
    }
    
    //LIST RECIPES
    void listRecipes(struct element *listPtr){
        if ( listPtr == NULL ) { printf("No recipes in the database"); }
        else {
         while ( listPtr != NULL ) {
             printf("%s\n",listPtr->data.recipeName); 
             listPtr = listPtr->elemPtr;  
            }   
        }
    }
    this is the error on compiling

    gcc.exe -c MainRecipe.c -o Objects/MingW/MainRecipe.o -I"C:/Program Files/Dev-Cpp/Include"

    MainRecipe.c: In function `insertRecipe':

    MainRecipe.c:88: error: invalid operands to binary !=
    MainRecipe.c:97: error: structure has no member named `xPtr'


    cant understand what is wrong

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by xphoenix View Post

    this is the error on compiling

    gcc.exe -c MainRecipe.c -o Objects/MingW/MainRecipe.o -I"C:/Program Files/Dev-Cpp/Include"

    MainRecipe.c: In function `insertRecipe':

    MainRecipe.c:88: error: invalid operands to binary !=
    MainRecipe.c:97: error: structure has no member named `xPtr'


    cant understand what is wrong
    your compiler is very specific on the above

    Code:
     if (*listPtr != NULL) {
    Remove the asterisk
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    dont but it has error the same...

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    One problem you have is that you need to be passing a reference to your pointer (**listPtr) so that you can MODIFY your pointer, not the list. Next, you'll need to assign the head of the list to that pointer (*listPtr = xPtr) at the end. You have two cases for inserting: when *listPtr == NULL, and when *listPtr != NULL.

    Handle those two cases and you're done.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    can u correct directly the code plz? i cant understand well... i passed the head of the list to the pointer...

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by xphoenix View Post
    can u correct directly the code plz? i cant understand well... i passed the head of the list to the pointer...
    what you do is like this:

    Code:
    void assign_int(int x)
    {
       x = 5;
    }
    
    void test()
    {
       int x = 0;
       assign_int(x);
    }
    this function makes changes to x, but they are invisible outside the function

    to make it work code should look like


    Code:
    void assign_int(int* x)
    {
       *x = 5;
    }
    
    void test()
    {
       int x = 0;
       assign_int(&x);
    }

    in your case you pass some variable into insert function, this function assigns new value to var, but as in the first sample - these changes as invisible outside the function insert
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    mmm it look i put the *... where i missed it?

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    The problem is really down to this point in your code as others said. You need a pointer-pointer reference.

    Code:
    if (*listPtr != NULL) {
    Your de-referencing the listptr pointer to check if its null, when you should really be checking the pointer it self. What you need is this

    Code:
    if (listPtr != NULL) {
    But that wouldn't work for you in this case. To correct the code

    Code:
    struct element *insertRecipe(struct element **listPtr){
    
    if (*listPtr != NULL) {
    And also check out the kennedy's comment. That will give you some more idea on what you need to do next.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    sorry i'm not getting it... can't understand

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void menu();
    
    // struct containing recipe
    typedef struct {
     char recipeName[40];
     char recipeHowTo[1000];
     int howManyPerson;
     int level;
     int timeToEat;
    } recipe;
    
    // struct for list
    struct element {
     recipe data;
     struct element *elemPtr;   
    };
    
    // prototypes
    void menu(int choice, struct element *listPtr);
    struct element *insertRecipe(struct element *listPtr);
    void listRecipes(struct element *listPtr);
    
    int main() {  
      int choice = "2" ;
      struct element *list = NULL;  
      
      menu(choice,list);
      
      
      system("PAUSE");	
      return 0;
    }
    
    void menu(int choice, struct element *listPtr){ 
        printf("----Recipes for fun\n");
        printf(".1 Search Recipe\n");
        printf(".2 View Recipes\n");
        printf(".3 Insert Recipe\n");
        printf(".4 Edit Recipe\n");
        printf(".5 Delete Recipe\n");
        printf(".8 Set measure system\n");
        printf(".9 Email Recipes\n"); 
        printf("Insert the number corrispondant to what you need and press ENTER!   ");
        scanf("%d",&choice); 
     
     if ( choice == 1 ) { }
     else if ( choice == 2 ) {  listRecipes(listPtr); }
     else if ( choice == 3 ) {  listPtr = insertRecipe(listPtr); }
     else if ( choice == 4 ) {  }
     else if ( choice == 5 ) {  }
     else if ( choice == 8 ) {  }
     else if ( choice == 9 ) {  }
     else {  }
    }
    
    // INSERT RECIPE 
    struct element *insertRecipe(struct element *listPtr){ //declare funct that needs a element Pointer as argument
        
        //var declairation
        char recipeName[40];  
        char recipeHowTo[1000];
        int howManyPerson;
        int level;
        int timeToEat;
        recipe x; 
        struct element *xPtr; 
        
        //filling info
        printf("Insert the Recipe name:\n");
        scanf("%s\n",recipeName);
        strcopy(x.recipeName,recipeName);
        printf("Insert the how to:\n");
        scanf("%s\n",recipeHowTo);
        strcopy(x.recipeHowTo,recipeHowTo);
        printf("Insert for how many person is the recipe");
        scanf("%d\n",howManyPerson);
        x.howManyPerson = howManyPerson;
        printf("Insert the level of the recipe from 1 to 5");
        scanf("%d\n",level);
        x.level = level;
        printf("Insert the time needed to prepare the recipe");
        scanf("%d\n",timeToEat);
        x.timeToEat = timeToEat;
        
        if (listPtr != NULL) { // if list is not empty
             xPtr = (struct element *)malloc(sizeof(struct element)); //allocate mem
             xPtr->data = x; //put x in data of the pointer
             xPtr->elemPtr = listPtr; //put xptr on top list
        }
        else {
            listPtr = (struct element *)malloc(sizeof(struct element));
            listPtr->data = x;
            listPtr->elemPtr = NULL; //listPtr points to end list
            listPtr->xPtr;
        }
        return(listPtr);    
        
    }
    
    //LIST RECIPES
    void listRecipes(struct element *listPtr){
        if ( listPtr == NULL ) { printf("No recipes in the database\n"); }
        else {
         while ( listPtr != NULL ) {
             printf("%s\n",listPtr->data.recipeName); 
             listPtr = listPtr->elemPtr;  
            }   
        }
    }
    this is the code with suggested correction...

    in this part

    struct element *insertRecipe(struct element *listPtr){
    i declare a function that contain the pointer to the "nextlist"element

    in this void listRecipes(struct element *listPtr){
    i have a function that contain the pointer to the nestlist element

    why the second is not with error?

    can u plz directly correct the code? i'll better understand

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    btw it says that my error is in xPtr

    structure has no member named xPtr

  11. #11
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    my mistake is corrected!!

    xPtr = listPtr; this was the only thing i needed... not the other things... ( now it works )
    i had the contrary before...


    but i have a lil problem too

    strcopy(x.recipeName,recipeName); he find me errors in here

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    That's exactly right. Which is why you should immediately infer that you are trying to access some member xPtr of the struct. If I was to bet between you and the compiler being right, I would bet on the compiler 99.9999% of the time, and if you know what is good for you so would you.

    Check this line:
    Code:
     else {
            listPtr = (struct element *)malloc(sizeof(struct element));
            listPtr->data = x;
            listPtr->elemPtr = NULL; //listPtr points to end list
            listPtr->xPtr;
        }
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  13. #13
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    loool yeah i had the opposite now i noticed the error D

    btw i have a problem yet on this program...

    during the filling info he ask the first 2 question, accept values, and then he ask all together without accepting the scanf

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Not sure what you said but I am assuming you have a problem with the fact that on the second run the enter key from the last scanf is read as a new string from the stdin stream therefore not allowing you time to read another string. You should read the FAQ on this site, it explains the problem in detail and explores several solutions and workarounds to "eat up" that \n.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  15. #15
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    no, i tried with removing the \n but it's the same, i'm going to read the faq but i dont think that it depends from the new line

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to a function pointer
    By @nthony in forum C Programming
    Replies: 3
    Last Post: 05-30-2010, 05:13 PM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM