Use of pointers in a typedef struct which contains a string of characters












1















I want to write a function called "substitution" that passes as parameters:




  1. a linked list "l" which has a character on each node ("lchar").

  2. a string of characters "CHG"

  3. a character "CH"

  4. a "vec" structure that contains the final string.


The function scans each character at time on the linked list, and writes on "vec" the characters of the linked list which are different from the character CH passed as a parameter, but when it scans the exact CH character on the linked list, it writes on "vec" the string "CHG".



The following is the solution given by my textbook. I seem to understand the overall program, but I can't understand the passage when the function scans the character CH:



svec->v[cur]=''; strcat(svec->v,CHG);



What does it do exactly? Why are we using pointers for "svec"? Why it is written svec->v[cur]=l->lchar; since we are handling a string of characters?



Solution:



struct el {
struct el *next;
char lchar;
};
struct el *l=NULL;

typedef struct {
int size;
char *v;
} vec;
vec svec;

int substitution (struct el *l, char *CHG, char CH, vec *svec) {
\ initialize svec in the dynamic memory
int cur=0;
while (l!=NULL) {
if (l->lchar !=CH) {
svec->v[cur]=l->lchar;
cur++;
l=l->next;
} else {
svec->v[cur]='';
strcat(svec->v,CHG);
cur=cur+strlen(CHG);
l=l->next;
}
}
svec->v[cur]='';
return 1;
}









share|improve this question



























    1















    I want to write a function called "substitution" that passes as parameters:




    1. a linked list "l" which has a character on each node ("lchar").

    2. a string of characters "CHG"

    3. a character "CH"

    4. a "vec" structure that contains the final string.


    The function scans each character at time on the linked list, and writes on "vec" the characters of the linked list which are different from the character CH passed as a parameter, but when it scans the exact CH character on the linked list, it writes on "vec" the string "CHG".



    The following is the solution given by my textbook. I seem to understand the overall program, but I can't understand the passage when the function scans the character CH:



    svec->v[cur]=''; strcat(svec->v,CHG);



    What does it do exactly? Why are we using pointers for "svec"? Why it is written svec->v[cur]=l->lchar; since we are handling a string of characters?



    Solution:



    struct el {
    struct el *next;
    char lchar;
    };
    struct el *l=NULL;

    typedef struct {
    int size;
    char *v;
    } vec;
    vec svec;

    int substitution (struct el *l, char *CHG, char CH, vec *svec) {
    \ initialize svec in the dynamic memory
    int cur=0;
    while (l!=NULL) {
    if (l->lchar !=CH) {
    svec->v[cur]=l->lchar;
    cur++;
    l=l->next;
    } else {
    svec->v[cur]='';
    strcat(svec->v,CHG);
    cur=cur+strlen(CHG);
    l=l->next;
    }
    }
    svec->v[cur]='';
    return 1;
    }









    share|improve this question

























      1












      1








      1








      I want to write a function called "substitution" that passes as parameters:




      1. a linked list "l" which has a character on each node ("lchar").

      2. a string of characters "CHG"

      3. a character "CH"

      4. a "vec" structure that contains the final string.


      The function scans each character at time on the linked list, and writes on "vec" the characters of the linked list which are different from the character CH passed as a parameter, but when it scans the exact CH character on the linked list, it writes on "vec" the string "CHG".



      The following is the solution given by my textbook. I seem to understand the overall program, but I can't understand the passage when the function scans the character CH:



      svec->v[cur]=''; strcat(svec->v,CHG);



      What does it do exactly? Why are we using pointers for "svec"? Why it is written svec->v[cur]=l->lchar; since we are handling a string of characters?



      Solution:



      struct el {
      struct el *next;
      char lchar;
      };
      struct el *l=NULL;

      typedef struct {
      int size;
      char *v;
      } vec;
      vec svec;

      int substitution (struct el *l, char *CHG, char CH, vec *svec) {
      \ initialize svec in the dynamic memory
      int cur=0;
      while (l!=NULL) {
      if (l->lchar !=CH) {
      svec->v[cur]=l->lchar;
      cur++;
      l=l->next;
      } else {
      svec->v[cur]='';
      strcat(svec->v,CHG);
      cur=cur+strlen(CHG);
      l=l->next;
      }
      }
      svec->v[cur]='';
      return 1;
      }









      share|improve this question














      I want to write a function called "substitution" that passes as parameters:




      1. a linked list "l" which has a character on each node ("lchar").

      2. a string of characters "CHG"

      3. a character "CH"

      4. a "vec" structure that contains the final string.


      The function scans each character at time on the linked list, and writes on "vec" the characters of the linked list which are different from the character CH passed as a parameter, but when it scans the exact CH character on the linked list, it writes on "vec" the string "CHG".



      The following is the solution given by my textbook. I seem to understand the overall program, but I can't understand the passage when the function scans the character CH:



      svec->v[cur]=''; strcat(svec->v,CHG);



      What does it do exactly? Why are we using pointers for "svec"? Why it is written svec->v[cur]=l->lchar; since we are handling a string of characters?



      Solution:



      struct el {
      struct el *next;
      char lchar;
      };
      struct el *l=NULL;

      typedef struct {
      int size;
      char *v;
      } vec;
      vec svec;

      int substitution (struct el *l, char *CHG, char CH, vec *svec) {
      \ initialize svec in the dynamic memory
      int cur=0;
      while (l!=NULL) {
      if (l->lchar !=CH) {
      svec->v[cur]=l->lchar;
      cur++;
      l=l->next;
      } else {
      svec->v[cur]='';
      strcat(svec->v,CHG);
      cur=cur+strlen(CHG);
      l=l->next;
      }
      }
      svec->v[cur]='';
      return 1;
      }






      c function pointers linked-list typedef






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 18:20









      KevinKevin

      1318




      1318
























          1 Answer
          1






          active

          oldest

          votes


















          1














          svec->v[cur]=''; strcat(svec->v,CHG);


          This replace the char at [cur] position on the svec->v char* with a terminating null byte in order to use strcat on it.



          From man strcat :




          The strcat() function appends the src string to the dest string, overwriting the terminating
          null byte ('') at the end of dest, and then adds a terminating null byte.




          If the "dest char *" of strcat lacks terminating null-byte, strcat behavior is unpredictable. It serves as a security.



           svec->v[cur]=l->lchar;


          It is written like that because "svec" and "l" are pointers, hence, you need to deference them in order to access their data.



          Hope it helps !






          share|improve this answer
























          • Thank you! Much appreciated :)

            – Kevin
            Jan 19 at 19:12











          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%2f54270065%2fuse-of-pointers-in-a-typedef-struct-which-contains-a-string-of-characters%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          svec->v[cur]=''; strcat(svec->v,CHG);


          This replace the char at [cur] position on the svec->v char* with a terminating null byte in order to use strcat on it.



          From man strcat :




          The strcat() function appends the src string to the dest string, overwriting the terminating
          null byte ('') at the end of dest, and then adds a terminating null byte.




          If the "dest char *" of strcat lacks terminating null-byte, strcat behavior is unpredictable. It serves as a security.



           svec->v[cur]=l->lchar;


          It is written like that because "svec" and "l" are pointers, hence, you need to deference them in order to access their data.



          Hope it helps !






          share|improve this answer
























          • Thank you! Much appreciated :)

            – Kevin
            Jan 19 at 19:12
















          1














          svec->v[cur]=''; strcat(svec->v,CHG);


          This replace the char at [cur] position on the svec->v char* with a terminating null byte in order to use strcat on it.



          From man strcat :




          The strcat() function appends the src string to the dest string, overwriting the terminating
          null byte ('') at the end of dest, and then adds a terminating null byte.




          If the "dest char *" of strcat lacks terminating null-byte, strcat behavior is unpredictable. It serves as a security.



           svec->v[cur]=l->lchar;


          It is written like that because "svec" and "l" are pointers, hence, you need to deference them in order to access their data.



          Hope it helps !






          share|improve this answer
























          • Thank you! Much appreciated :)

            – Kevin
            Jan 19 at 19:12














          1












          1








          1







          svec->v[cur]=''; strcat(svec->v,CHG);


          This replace the char at [cur] position on the svec->v char* with a terminating null byte in order to use strcat on it.



          From man strcat :




          The strcat() function appends the src string to the dest string, overwriting the terminating
          null byte ('') at the end of dest, and then adds a terminating null byte.




          If the "dest char *" of strcat lacks terminating null-byte, strcat behavior is unpredictable. It serves as a security.



           svec->v[cur]=l->lchar;


          It is written like that because "svec" and "l" are pointers, hence, you need to deference them in order to access their data.



          Hope it helps !






          share|improve this answer













          svec->v[cur]=''; strcat(svec->v,CHG);


          This replace the char at [cur] position on the svec->v char* with a terminating null byte in order to use strcat on it.



          From man strcat :




          The strcat() function appends the src string to the dest string, overwriting the terminating
          null byte ('') at the end of dest, and then adds a terminating null byte.




          If the "dest char *" of strcat lacks terminating null-byte, strcat behavior is unpredictable. It serves as a security.



           svec->v[cur]=l->lchar;


          It is written like that because "svec" and "l" are pointers, hence, you need to deference them in order to access their data.



          Hope it helps !







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 19 at 18:51









          Antoine. FAntoine. F

          262




          262













          • Thank you! Much appreciated :)

            – Kevin
            Jan 19 at 19:12



















          • Thank you! Much appreciated :)

            – Kevin
            Jan 19 at 19:12

















          Thank you! Much appreciated :)

          – Kevin
          Jan 19 at 19:12





          Thank you! Much appreciated :)

          – Kevin
          Jan 19 at 19:12


















          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%2f54270065%2fuse-of-pointers-in-a-typedef-struct-which-contains-a-string-of-characters%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

          Callistus III

          Ostreoida

          Plistias Cous