Thread: Error with a pointer

  1. #16
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    great! i removed ANY \n and works... tnx a lot

  2. #17
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    when i try to use the program, i first chose option 3 to insert a recipe, then i press 2 to view all recipes, but it says that it has no recipe why???

    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;  
      
      int status = 0;
      while( status != 1 ){
           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] = "default";  
        char recipeHowTo[1000] = "default";
        int howManyPerson = 0;
        int level = 0;
        int timeToEat = 0;
        recipe x; 
        struct element *xPtr; 
        
        //filling info
        printf("Insert the Recipe name:");
        scanf("%s",recipeName);
        strcpy(x.recipeName,recipeName);
        printf("Insert the how to:");
        scanf("%s",recipeHowTo); 
        strcpy(x.recipeHowTo,recipeHowTo);
        printf("Insert for how many person is the recipe ");
        scanf("%d",&howManyPerson);
        x.howManyPerson = howManyPerson; 
        printf("Insert the level of the recipe from 1 to 5 ");
        scanf("%d",&level);
        x.level = level;
        printf("Insert the time needed to prepare the recipe ");
        scanf("%d",&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
            xPtr = listPtr;
        }
        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",listPtr->data.recipeName); 
             listPtr = listPtr->elemPtr;  
            }   
        }   
    }

  3. #18
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Several issues with your code:

    1) Your assignment of string literals "default" to your char arrays is an incorrect way to do what you are trying to do. Use strcpy().

    2) This :

    Code:
     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 {  }
    }
    should be a switch statement for obvious reasons.

    3) On a first glance it looks like you are not actually modifying the list when you are inserting elements. If you wish to do so you may want to pass a **struct instead of a *struct (i.e. a pointer containing the address of the address of the head of the list).
    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.

  4. #19
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    Quote Originally Posted by claudiu View Post
    Several issues with your code:

    1) Your assignment of string literals "default" to your char arrays is an incorrect way to do what you are trying to do. Use strcpy().

    2) This :

    Code:
     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 {  }
    }
    should be a switch statement for obvious reasons.

    3) On a first glance it looks like you are not actually modifying the list when you are inserting elements. If you wish to do so you may want to pass a **struct instead of a *struct (i.e. a pointer containing the address of the address of the head of the list).


    i dont get the 3rd point, i'm following an online example can u explain be better plz?

    this is the example i'm following Guida C

  5. #20
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    heeeelp plz

  6. #21
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The person writing the code you are looking at is obviously also a beginner, so I suggest to stop using that as a reference for anything.

    Inside your insert function it looks to me like you are modifying the pointer passed as parameter so in order to do so successfully you need to pass an address to that pointer (i.e. struct element **head)

    Then you change what *head points to by assigning *head = my_new_ptr, or whatever the new element pointer you allocate memory for is called ( I think xptr).

    What is happening right now in your code is the same as this:

    Code:
    void my_func(int x){
      x = 3;
    }
    
    int main(void){
      int y = 5;
      my_func(y);
      /*THE VALUE OF y is STILL 5 */
      return 0;
    }
    The same thing is happening to your pointer. You are passing a pointer variable, changing it inside the function, and once you exit the function the pointer still has the old value it had (unallocated garbage).

    You need to brush up on pointers a bit. A pointer is essentialy a variable containing an address. In order to change the contents of a variable within a function you need to provide the address of that variable. So in this case, you need to provide the address of a pointer which is of type **. (basically the address of the memory location where the address of the element in the list is located)
    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.

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    He doesn't necessarily need to use a pointer-to-a-pointer. I would suggest doing the same thing in menu that is done in insertRecipe.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #23
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    @imalc can u better explain what u mean plz?

    @claudiu can u show me plz how to put that * ? i tried but always compile error... can u correct the code directly? so i can better understand

    ty

  9. #24
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you understand what the first three specific lines (cut out from you code) below do to update 'listPtr' in 'menu', and recognise that the last lines (also cut out from you code) indicate you are not doing the same thing to update 'list' in 'main', then you'll have worked out what you need to do to .
    Code:
     else if ( choice == 3 ) {  listPtr = insertRecipe(listPtr); }
    
    struct element *insertRecipe(struct element *listPtr){ //declare funct that needs a element Pointer as argument
    
        return(listPtr);
    Code:
           menu(choice,list);
    
    void menu(int choice, struct element *listPtr){
    If that hint doesn't help then I can only conclude that you did not write the code to begin with.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #25
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    Quote Originally Posted by iMalc View Post
    If you understand what the first three specific lines (cut out from you code) below do to update 'listPtr' in 'menu', and recognise that the last lines (also cut out from you code) indicate you are not doing the same thing to update 'list' in 'main', then you'll have worked out what you need to do to .
    Code:
     else if ( choice == 3 ) {  listPtr = insertRecipe(listPtr); }
    
    struct element *insertRecipe(struct element *listPtr){ //declare funct that needs a element Pointer as argument
    
        return(listPtr);
    Code:
           menu(choice,list);
    
    void menu(int choice, struct element *listPtr){
    If that hint doesn't help then I can only conclude that you did not write the code to begin with.
    i got ur hint, but it doesnt run

    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
    struct element *menu(int choice, struct element *list);
    struct element *insertRecipe(struct element *listPtr);
    void listRecipes(struct element *listPtr);
    void saveRecipes(struct element *listPtr);
    
    int main() {  
      int choice = 2 ;
      struct element *list = NULL;  
      
      int status = 0;
      while( status != 1 ){
         list = menu(choice,list);
     }
      
      system("PAUSE");	
      return 0;
    }
    
    struct element *menu(int choice, struct element *list){ 
       
        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(list); }
     else if ( choice == 3 ) {  list = insertRecipe(list); }
     else if ( choice == 4 ) {  }
     else if ( choice == 5 ) {  }
     else if ( choice == 8 ) {  }
     else if ( choice == 9 ) {  saveRecipes(list); }
     else {  }
    
    return(list);
    }
    
    // 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 = 0;
        int level = 0;
        int timeToEat = 0;
        recipe x; 
        struct element *xPtr; 
        
        //filling info
        printf("Insert the Recipe name:");
        scanf("%s",recipeName);
        strcpy(x.recipeName,recipeName);
        printf("Insert the how to:");
        scanf("%s",recipeHowTo); 
        strcpy(x.recipeHowTo,recipeHowTo);
        printf("Insert for how many person is the recipe ");
        scanf("%d",&howManyPerson);
        x.howManyPerson = howManyPerson; 
        printf("Insert the level of the recipe from 1 to 5 ");
        scanf("%d",&level);
        x.level = level;
        printf("Insert the time needed to prepare the recipe ");
        scanf("%d",&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
           // xPtr = listPtr;
        }
        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",listPtr->data.recipeName); 
             listPtr = listPtr->elemPtr;  
            }   
        }   
    }
    
    //SAVE TO TXT
    void saveRecipes(struct element *listPtr){
        FILE *stream = fopen("recipes.txt","w");
        
        while(listPtr != NULL) {
         fprintf(stream,"%s\n%s\n%d%d%d\n\n",listPtr->data.recipeName,listPtr->data.recipeHowTo,listPtr->data.howManyPerson,listPtr->data.level,listPtr->data.timeToEat);   
         listPtr = listPtr->elemPtr;
        }
        fflush(stream);
        fclose(stream);
    }

  11. #26
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I see two prototypes for menu. One of them that should be deleted is:
    Code:
    void menu();
    "it doesnt run" doesn't tell us much. Always be specific. Is it because it does not compile (you'd show the compile errors if that were the case), or does it produce the wrong output when executed (you'd show the incorrect vs the expected output).
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #27
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    ty

    i'm using the program, when i insert recipes all is ok, but when i go to see what it is inserted, i saw only the first recipe, no other inserted, why?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // 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
    struct element *menu(int choice, struct element *list);
    struct element *insertRecipe(struct element *listPtr);
    struct element *editRecipe(struct element *listPtr);
    void listRecipes(struct element *listPtr);
    void saveRecipes(struct element *listPtr);
    struct element *loadRecipes(struct element *listPtr);
    
    int main() {  
      int choice = 2 ;
      struct element *list = NULL;  
      
      int status = 0;
      while( status != 1 ){
         list = loadRecipes(list);   
         list = menu(choice,list);
     }
      
      system("PAUSE");	
      return 0;
    }
    
    struct element *menu(int choice, struct element *list){ 
       
        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(list); }
     else if ( choice == 3 ) {  list = insertRecipe(list); }
     else if ( choice == 4 ) {  list = editRecipe(list); }
     else if ( choice == 5 ) {  }
     else if ( choice == 8 ) {  }
     else if ( choice == 9 ) {  saveRecipes(list); }
     else {  }
    
    return(list);
    }
    
    // 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 = 0;
        int level = 0;
        int timeToEat = 0;
        recipe x; 
        struct element *xPtr; 
        
        //filling info
        printf("Insert the Recipe name:");
        scanf("%s",recipeName);
        strcpy(x.recipeName,recipeName);
        printf("Insert the how to:");
        scanf("%s",recipeHowTo); 
        strcpy(x.recipeHowTo,recipeHowTo);
        printf("Insert for how many person is the recipe ");
        scanf("%d",&howManyPerson);
        x.howManyPerson = howManyPerson; 
        printf("Insert the level of the recipe from 1 to 5 ");
        scanf("%d",&level);
        x.level = level;
        printf("Insert the time needed to prepare the recipe ");
        scanf("%d",&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
            xPtr = listPtr;
        }
        return(listPtr);    
        
    }
    
    //EDIT RECIPE
    struct element *editRecipe(struct element *listPtr){
        
        struct element *originalValue = listPtr;
        int i = 1;
        int cont = 1;
        int editValue = 0;
        
        if ( listPtr == NULL ) { printf("No recipes in the database\n"); }
        else {
         int i = 1;   
         while ( listPtr != NULL ) {
             printf("%d %s\n",i,listPtr->data.recipeName); i++;
             listPtr = listPtr->elemPtr;  
            }   
        }   
        
        listPtr = originalValue;
        
        printf("Insert the number of the value to edit: ");
        scanf("%d",&editValue);
        
        for(cont=1;cont<i;cont++){
            if (editValue == cont) {            
                printf("Change the Recipe name:");
                scanf("%s",listPtr->data.recipeName);
                printf("Change the how to:");
                scanf("%s",listPtr->data.recipeHowTo); 
                printf("Change for how many person is the recipe ");
                scanf("%d",listPtr->data.howManyPerson);
                printf("Change the level of the recipe from 1 to 5 ");
                scanf("%d",listPtr->data.level);
                printf("Change the time needed to prepare the recipe ");
                scanf("%d",listPtr->data.timeToEat);          
            }
            listPtr = listPtr->elemPtr;
        }
        return(originalValue);
    }
    
    //LOAD RECIPES
    struct element *loadRecipes(struct element *listPtr){
    
    FILE *filePtr;
    recipe x;
    struct element *xPtr;
    
       if(( filePtr = fopen("recipes.txt","rb"))== NULL){
           printf("Error in file opening");
       }
       while( fread(&x,sizeof(recipe),1,filePtr) != NULL ){
           if(listPtr == NULL){
               listPtr = (struct element *) malloc(sizeof (struct element));
               strcpy(listPtr->data.recipeName,x.recipeName);
               strcpy(listPtr->data.recipeHowTo,x.recipeHowTo);
               //strcpy(listPtr->data.howManyPerson,x.howManyPerson);
               //strcpy(listPtr->data.level,x.level);
               //strcpy(listPtr->data.timeToEat,x.t);
               listPtr->elemPtr = NULL;
           }else{
               xPtr = (struct element *) malloc(sizeof (struct element));
               strcpy(xPtr->data.recipeName,x.recipeName);
               strcpy(xPtr->data.recipeHowTo,x.recipeHowTo);
               //strcpy(xPtr->data.howManyPerson,x.howManyPerson);
               //strcpy(xPtr->data.level,x.level);
               //strcpy(xPtr->data.timeToEat,x.t);
               xPtr->elemPtr = xPtr;
               listPtr = xPtr;
           }
           }fclose(filePtr);  
       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;  
            }   
        }   
    }
    
    //SAVE TO TXT
    void saveRecipes(struct element *listPtr){
        FILE *stream = fopen("recipes.txt","w");
        
        while(listPtr != NULL) {
         fprintf(stream,"%s\n%s\n%d%d%d\n\n",listPtr->data.recipeName,listPtr->data.recipeHowTo,listPtr->data.howManyPerson,listPtr->data.level,listPtr->data.timeToEat);   
         listPtr = listPtr->elemPtr;
        }
        fflush(stream);
        fclose(stream);
    }
    Last edited by xphoenix; 07-05-2010 at 07:44 AM.

  13. #28
    Registered User
    Join Date
    Jun 2010
    Posts
    46
    it doesnt charge the list at the start

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