Update cell in struct list and return the list





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm looking for a way to update a cell in a struct list.
I have a struct list:



struct _list {
int startNode,
targetNode,
substitute;
unsigned char letter;
struct _list *next;
}


And a function that enables me to update a cell.



struct _list *insertSubstitute(struct _list *list, int startNode,
unsigned char letter, int substitute) {
while (list != NULL) {
if (list -> letter == letter && list -> startNode == startNode) {
list -> substitute = substitute;
return list;
}
list = list -> next;
}
return NULL;
}


My problem is that this function do not return the whole list. How could I proceed to do so?










share|improve this question























  • insertSubstitute gets a pointer to a struct, so you are modifying the source. Why do you want to return it?

    – Jose
    Jan 3 at 10:21











  • Changed your function prototype from 'struct _list*' list to 'struct _list**', you cannot modify a pointer and return it back but you can modify a pointer when you pass a pointer to the pointer, you will need to change internal references to list to '(*list)->, if you do this then you can get rid of the return and change the function return to void.

    – SPlatten
    Jan 3 at 10:29




















0















I'm looking for a way to update a cell in a struct list.
I have a struct list:



struct _list {
int startNode,
targetNode,
substitute;
unsigned char letter;
struct _list *next;
}


And a function that enables me to update a cell.



struct _list *insertSubstitute(struct _list *list, int startNode,
unsigned char letter, int substitute) {
while (list != NULL) {
if (list -> letter == letter && list -> startNode == startNode) {
list -> substitute = substitute;
return list;
}
list = list -> next;
}
return NULL;
}


My problem is that this function do not return the whole list. How could I proceed to do so?










share|improve this question























  • insertSubstitute gets a pointer to a struct, so you are modifying the source. Why do you want to return it?

    – Jose
    Jan 3 at 10:21











  • Changed your function prototype from 'struct _list*' list to 'struct _list**', you cannot modify a pointer and return it back but you can modify a pointer when you pass a pointer to the pointer, you will need to change internal references to list to '(*list)->, if you do this then you can get rid of the return and change the function return to void.

    – SPlatten
    Jan 3 at 10:29
















0












0








0








I'm looking for a way to update a cell in a struct list.
I have a struct list:



struct _list {
int startNode,
targetNode,
substitute;
unsigned char letter;
struct _list *next;
}


And a function that enables me to update a cell.



struct _list *insertSubstitute(struct _list *list, int startNode,
unsigned char letter, int substitute) {
while (list != NULL) {
if (list -> letter == letter && list -> startNode == startNode) {
list -> substitute = substitute;
return list;
}
list = list -> next;
}
return NULL;
}


My problem is that this function do not return the whole list. How could I proceed to do so?










share|improve this question














I'm looking for a way to update a cell in a struct list.
I have a struct list:



struct _list {
int startNode,
targetNode,
substitute;
unsigned char letter;
struct _list *next;
}


And a function that enables me to update a cell.



struct _list *insertSubstitute(struct _list *list, int startNode,
unsigned char letter, int substitute) {
while (list != NULL) {
if (list -> letter == letter && list -> startNode == startNode) {
list -> substitute = substitute;
return list;
}
list = list -> next;
}
return NULL;
}


My problem is that this function do not return the whole list. How could I proceed to do so?







c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 10:15









Thomas DI GREGORIOThomas DI GREGORIO

31




31













  • insertSubstitute gets a pointer to a struct, so you are modifying the source. Why do you want to return it?

    – Jose
    Jan 3 at 10:21











  • Changed your function prototype from 'struct _list*' list to 'struct _list**', you cannot modify a pointer and return it back but you can modify a pointer when you pass a pointer to the pointer, you will need to change internal references to list to '(*list)->, if you do this then you can get rid of the return and change the function return to void.

    – SPlatten
    Jan 3 at 10:29





















  • insertSubstitute gets a pointer to a struct, so you are modifying the source. Why do you want to return it?

    – Jose
    Jan 3 at 10:21











  • Changed your function prototype from 'struct _list*' list to 'struct _list**', you cannot modify a pointer and return it back but you can modify a pointer when you pass a pointer to the pointer, you will need to change internal references to list to '(*list)->, if you do this then you can get rid of the return and change the function return to void.

    – SPlatten
    Jan 3 at 10:29



















insertSubstitute gets a pointer to a struct, so you are modifying the source. Why do you want to return it?

– Jose
Jan 3 at 10:21





insertSubstitute gets a pointer to a struct, so you are modifying the source. Why do you want to return it?

– Jose
Jan 3 at 10:21













Changed your function prototype from 'struct _list*' list to 'struct _list**', you cannot modify a pointer and return it back but you can modify a pointer when you pass a pointer to the pointer, you will need to change internal references to list to '(*list)->, if you do this then you can get rid of the return and change the function return to void.

– SPlatten
Jan 3 at 10:29







Changed your function prototype from 'struct _list*' list to 'struct _list**', you cannot modify a pointer and return it back but you can modify a pointer when you pass a pointer to the pointer, you will need to change internal references to list to '(*list)->, if you do this then you can get rid of the return and change the function return to void.

– SPlatten
Jan 3 at 10:29














3 Answers
3






active

oldest

votes


















1














Add a new var whole_list.



The following code could work:



struct _list *insertSubstitute(struct _list *list,
int startNode,
unsigned char letter,
int substitute) {

struct _list* whole_list = list;

while (list != NULL) {
if (list -> letter == letter && list -> startNode == startNode) {
list -> substitute = substitute;
break;
}
list = list -> next;
}
return whole_list;
}





share|improve this answer































    0














    you need just to use a temporary pointer to your list to check the elements since you are using the list it self its causing you the problem



    struct _list *insertSubstitute(struct _list *list, int startNode,
    unsigned char letter, int substitute) {
    struct _list *temp=list;
    while (temp!= NULL) {
    if (temp-> letter == letter && temp-> startNode == startNode) {
    temp -> substitute = substitute;
    break;
    }
    temp= temp-> next;
    }
    return list;
    }





    share|improve this answer































      0














      Try this:



          void insertSubstitute(struct _list** list
      ,int startNode
      ,unsigned char letter
      ,int substitute) {
      if ( list == NULL ) {
      //Do nothing list is invalid
      return;
      }
      while (*list != NULL) {
      if ((*list) -> letter == letter && (*list) -> startNode == startNode) {
      (*list) -> substitute = substitute;
      return;
      }
      (*list) = (*list) -> next;
      }
      }


      When you call this version of the function, you need to pass the address of your list.



      Or another version of the original:



          struct _list*insertSubstitute(struct _list* list
      ,int startNode
      ,unsigned char letter
      ,int substitute) {
      if ( list == NULL ) {
      //Do nothing list is invalid
      return;
      }
      struct _list* rc = list;
      while (rc != NULL) {
      if (rc -> letter == letter && rc -> startNode == startNode) {
      rc -> substitute = substitute;
      return rc;
      }
      rc = rc -> next;
      }
      return NULL;
      }





      share|improve this answer
























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020214%2fupdate-cell-in-struct-list-and-return-the-list%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        Add a new var whole_list.



        The following code could work:



        struct _list *insertSubstitute(struct _list *list,
        int startNode,
        unsigned char letter,
        int substitute) {

        struct _list* whole_list = list;

        while (list != NULL) {
        if (list -> letter == letter && list -> startNode == startNode) {
        list -> substitute = substitute;
        break;
        }
        list = list -> next;
        }
        return whole_list;
        }





        share|improve this answer




























          1














          Add a new var whole_list.



          The following code could work:



          struct _list *insertSubstitute(struct _list *list,
          int startNode,
          unsigned char letter,
          int substitute) {

          struct _list* whole_list = list;

          while (list != NULL) {
          if (list -> letter == letter && list -> startNode == startNode) {
          list -> substitute = substitute;
          break;
          }
          list = list -> next;
          }
          return whole_list;
          }





          share|improve this answer


























            1












            1








            1







            Add a new var whole_list.



            The following code could work:



            struct _list *insertSubstitute(struct _list *list,
            int startNode,
            unsigned char letter,
            int substitute) {

            struct _list* whole_list = list;

            while (list != NULL) {
            if (list -> letter == letter && list -> startNode == startNode) {
            list -> substitute = substitute;
            break;
            }
            list = list -> next;
            }
            return whole_list;
            }





            share|improve this answer













            Add a new var whole_list.



            The following code could work:



            struct _list *insertSubstitute(struct _list *list,
            int startNode,
            unsigned char letter,
            int substitute) {

            struct _list* whole_list = list;

            while (list != NULL) {
            if (list -> letter == letter && list -> startNode == startNode) {
            list -> substitute = substitute;
            break;
            }
            list = list -> next;
            }
            return whole_list;
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 3 at 10:24









            Yunbin LiuYunbin Liu

            1,2762515




            1,2762515

























                0














                you need just to use a temporary pointer to your list to check the elements since you are using the list it self its causing you the problem



                struct _list *insertSubstitute(struct _list *list, int startNode,
                unsigned char letter, int substitute) {
                struct _list *temp=list;
                while (temp!= NULL) {
                if (temp-> letter == letter && temp-> startNode == startNode) {
                temp -> substitute = substitute;
                break;
                }
                temp= temp-> next;
                }
                return list;
                }





                share|improve this answer




























                  0














                  you need just to use a temporary pointer to your list to check the elements since you are using the list it self its causing you the problem



                  struct _list *insertSubstitute(struct _list *list, int startNode,
                  unsigned char letter, int substitute) {
                  struct _list *temp=list;
                  while (temp!= NULL) {
                  if (temp-> letter == letter && temp-> startNode == startNode) {
                  temp -> substitute = substitute;
                  break;
                  }
                  temp= temp-> next;
                  }
                  return list;
                  }





                  share|improve this answer


























                    0












                    0








                    0







                    you need just to use a temporary pointer to your list to check the elements since you are using the list it self its causing you the problem



                    struct _list *insertSubstitute(struct _list *list, int startNode,
                    unsigned char letter, int substitute) {
                    struct _list *temp=list;
                    while (temp!= NULL) {
                    if (temp-> letter == letter && temp-> startNode == startNode) {
                    temp -> substitute = substitute;
                    break;
                    }
                    temp= temp-> next;
                    }
                    return list;
                    }





                    share|improve this answer













                    you need just to use a temporary pointer to your list to check the elements since you are using the list it self its causing you the problem



                    struct _list *insertSubstitute(struct _list *list, int startNode,
                    unsigned char letter, int substitute) {
                    struct _list *temp=list;
                    while (temp!= NULL) {
                    if (temp-> letter == letter && temp-> startNode == startNode) {
                    temp -> substitute = substitute;
                    break;
                    }
                    temp= temp-> next;
                    }
                    return list;
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 3 at 10:25









                    SpinkooSpinkoo

                    1,7701216




                    1,7701216























                        0














                        Try this:



                            void insertSubstitute(struct _list** list
                        ,int startNode
                        ,unsigned char letter
                        ,int substitute) {
                        if ( list == NULL ) {
                        //Do nothing list is invalid
                        return;
                        }
                        while (*list != NULL) {
                        if ((*list) -> letter == letter && (*list) -> startNode == startNode) {
                        (*list) -> substitute = substitute;
                        return;
                        }
                        (*list) = (*list) -> next;
                        }
                        }


                        When you call this version of the function, you need to pass the address of your list.



                        Or another version of the original:



                            struct _list*insertSubstitute(struct _list* list
                        ,int startNode
                        ,unsigned char letter
                        ,int substitute) {
                        if ( list == NULL ) {
                        //Do nothing list is invalid
                        return;
                        }
                        struct _list* rc = list;
                        while (rc != NULL) {
                        if (rc -> letter == letter && rc -> startNode == startNode) {
                        rc -> substitute = substitute;
                        return rc;
                        }
                        rc = rc -> next;
                        }
                        return NULL;
                        }





                        share|improve this answer




























                          0














                          Try this:



                              void insertSubstitute(struct _list** list
                          ,int startNode
                          ,unsigned char letter
                          ,int substitute) {
                          if ( list == NULL ) {
                          //Do nothing list is invalid
                          return;
                          }
                          while (*list != NULL) {
                          if ((*list) -> letter == letter && (*list) -> startNode == startNode) {
                          (*list) -> substitute = substitute;
                          return;
                          }
                          (*list) = (*list) -> next;
                          }
                          }


                          When you call this version of the function, you need to pass the address of your list.



                          Or another version of the original:



                              struct _list*insertSubstitute(struct _list* list
                          ,int startNode
                          ,unsigned char letter
                          ,int substitute) {
                          if ( list == NULL ) {
                          //Do nothing list is invalid
                          return;
                          }
                          struct _list* rc = list;
                          while (rc != NULL) {
                          if (rc -> letter == letter && rc -> startNode == startNode) {
                          rc -> substitute = substitute;
                          return rc;
                          }
                          rc = rc -> next;
                          }
                          return NULL;
                          }





                          share|improve this answer


























                            0












                            0








                            0







                            Try this:



                                void insertSubstitute(struct _list** list
                            ,int startNode
                            ,unsigned char letter
                            ,int substitute) {
                            if ( list == NULL ) {
                            //Do nothing list is invalid
                            return;
                            }
                            while (*list != NULL) {
                            if ((*list) -> letter == letter && (*list) -> startNode == startNode) {
                            (*list) -> substitute = substitute;
                            return;
                            }
                            (*list) = (*list) -> next;
                            }
                            }


                            When you call this version of the function, you need to pass the address of your list.



                            Or another version of the original:



                                struct _list*insertSubstitute(struct _list* list
                            ,int startNode
                            ,unsigned char letter
                            ,int substitute) {
                            if ( list == NULL ) {
                            //Do nothing list is invalid
                            return;
                            }
                            struct _list* rc = list;
                            while (rc != NULL) {
                            if (rc -> letter == letter && rc -> startNode == startNode) {
                            rc -> substitute = substitute;
                            return rc;
                            }
                            rc = rc -> next;
                            }
                            return NULL;
                            }





                            share|improve this answer













                            Try this:



                                void insertSubstitute(struct _list** list
                            ,int startNode
                            ,unsigned char letter
                            ,int substitute) {
                            if ( list == NULL ) {
                            //Do nothing list is invalid
                            return;
                            }
                            while (*list != NULL) {
                            if ((*list) -> letter == letter && (*list) -> startNode == startNode) {
                            (*list) -> substitute = substitute;
                            return;
                            }
                            (*list) = (*list) -> next;
                            }
                            }


                            When you call this version of the function, you need to pass the address of your list.



                            Or another version of the original:



                                struct _list*insertSubstitute(struct _list* list
                            ,int startNode
                            ,unsigned char letter
                            ,int substitute) {
                            if ( list == NULL ) {
                            //Do nothing list is invalid
                            return;
                            }
                            struct _list* rc = list;
                            while (rc != NULL) {
                            if (rc -> letter == letter && rc -> startNode == startNode) {
                            rc -> substitute = substitute;
                            return rc;
                            }
                            rc = rc -> next;
                            }
                            return NULL;
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 3 at 10:33









                            SPlattenSPlatten

                            2,23531953




                            2,23531953






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020214%2fupdate-cell-in-struct-list-and-return-the-list%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                Npm cannot find a required file even through it is in the searched directory