c - Why does some characters change? -


i'm programming c language in codeblocks gnu gcc compiler.i writing function create link list consisting token nodes.for example, below text file:

main ( ) { int ; int b ; } 

the link list of tokens be

main -> ( -> ) -> { -> int -> -> ; -> int -> b -> ; -> }

for delimiter space character. decided make other link list called line. each line consisting successive tokens separated space finished ; character. example , @ same text file relevant tokens lines be:

main ( ) { int ; -> int b ;-> }

you see code below:

//including related header files typedef struct token {     char *tok;     struct token *next;     int tp; } token;   typedef struct line {     char *ls;     struct line *next;     int parent; } line;  token *start; line *lstart;  void addline (line * a); void showline (void); void setline (void);  int main (void ) {      int = 0;      // next 4 lines allocates space start(pointer of type token)     // , lstart(pointer of type line) first node in link     // list.the first meaningful data of each type stored in nodes     // after start , lstart node       start = (token *) malloc (sizeof (token));     start->next = null;      lstart = (line *) malloc (sizeof (line));     lstart->next = null;      file *p;      p = fopen ("sample.txt", "r+");     if (p == null) {         printf ("can not open file");         exit (1);     }     //calling fnuction making link list of tokens text     //file      setline ();     showline ();      return 0;  }  // relevant add functions adds new token or  // link list @ end of list void showline () {     line *p;     p = lstart->next;     while (p != null) {         printf ("%s\n", p->ls);         p = p->next;     } }  void setline (void) {     int parent;     token *p;     p = start->next;      line *q;      q = (line *) malloc (sizeof (line));      q->ls = null;      while (p != null) {         if (p == null) {             break;         }          q->ls = strdup (p->tok);         strcat (q->ls, " ");         p = p->next;          while ((p != null)) {             if (strcmp (p->tok, ";") == 0) {                 strcat (q->ls, "; ");                 p = p->next;                 break;             }             strcat (q->ls, p->tok);             strcat (q->ls, " ");             p = p->next;         }          printf ("%s\n", q->ls);         addline (q);         q->ls = null;     } } 

and stored data in text file "sample.txt" :

#include <something.h>  int , b ; main ( ) { int ; int b ; } 

i expected lines made correctly strange happened when called showline() function (this function used , can seen in main ):in lines there strange characters.for example ls of second line node expected int b ; happened înt b ; in usual i character turned î(a strange character) .which mistake did make when working strings?

one of problematic places:

q->ls=strdup(p->tok); strcat(q->ls," "); 

the strdup function allocates enough space p->tok only, there's no space append after copies string. calling strcat of course write out of bounds , have undefined behavior.

if want append more characters, need allocate (using malloc or calloc) size need, , manually copy initial string.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -