c - How to initialize array and fill with chars from buffer? -


i have following method:

int create_nodes(source* source, int maxtokens) {     int nodecount = 0;     token* p_tstart = source->tknbuffer;     token* p_tcurrent = source->tknbuffer;      while ((p_tcurrent - p_tstart) < maxtokens && p_tcurrent != null) {         int szword = p_tcurrent->t_l;         char word[szword];         //  void *memset(void *str, int c, size_t n)         memset(word, '\0', sizeof(char)*szword);         //  void *memcpy(void *str1, const void *str2, size_t n)         char* p_t = source->buffer + p_tcurrent->t_s;         memcpy(word, p_t, szword);          if (word == ";") {             ++p_tcurrent;              continue;         }          ++p_tcurrent;         ++nodecount;     } } 

source contains char* buffer. intent of method first count tokens in buffer not ; token (and arrive @ how many nodes need). here buffer using: 11 + 31;. tokens - point - have been created follows:

  • 11
  • +
  • 31
  • ;

i pass in token* contains start (t_s) , length (t_l). so, instance, token representing + char:

  • t->t_s = 3
  • t->t_l = 1

at memcpy(...), debugger jumps ++p_tcurrent meaning nothing copied word - uncaught exception guess. doing wrong initialize word array? , fill specific information specified token?

source.h:

struct source {     const char* filename;     char* buffer;     int buflen;     token* tknbuffer;     node* nodebuffer;     expression* exprbuffer; };   

token.h:

struct token {     int t_s;     int t_l; }; 

this not right.

if (word == ";") { 

that compare 2 pointers , false time.

i suspect meant use:

// test whether first character semicolon if (word[0] == ';') { 

or

// test whether entire word semicolon if (strcmp(word, ";") == 0) { 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -