C newbie array question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • angusmiller
    New Member
    • Oct 2008
    • 2

    #1

    C newbie array question

    Hi,

    Can anbody tell me why the below code outputs abbrev for all the arrays values in vals? What is the correct way to assign values so that when I iterate through the array I will get the seperate values?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    const char *toksplit(const char *src, /* Source of tokens */
    char tokchar, /* token delimiting char */
    char *token, /* receiver of parsed token */
    size_t lgh) /* length token can receive */
    /* not including final '\0' */
    {
    if (src) {
    while (' ' == *src) *src++;
    
    while (*src && (tokchar != *src)) {
    if (lgh) {
    *token++ = *src;
    --lgh;
    }
    src++;
    }
    if (*src && (tokchar == *src)) src++;
    }
    *token = '\0';
    return src;
    } /* toksplit */
    
    #define ABRsize 6 /* length of acceptable token abbreviations */
    
    char *vals[4];
    
    int main(void)
    {
    char teststring[] = "This is a test, ,, abbrev, more";
    
    const char *t, *s = teststring;
    int i;
    char token[ABRsize + 1];
    
    puts(teststring);
    t = s;
    for (i = 0; i < 4; i++) {
        t = toksplit(t, ',', token, ABRsize);
        putchar(i + '1'); putchar(':');
        vals[i] = token;
        puts(token);
    }
    
    puts("-----------------------------------");
    
    for (i = 0; i < 4; i++) {
        putchar(i + '1'); putchar(':');
        puts(vals[i]);
    }
    
    return 0;
    }
    Thanks
    Angus
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    Your vals array is an array of pointers to char. They all point to the same place in memory, the beginning of your token array.

    You can see that you only allocate your 6+1 bytes for tokens once (char token[ABRsize + 1]), so you cannot expect to have 4 tokens in memory - on each call to toksplit, you are passing the pointer to your one and only token.

    So the answer is:
    1. Get rid of token
    2. Allocate 4 tokens when allocating vals:
    Code:
    char vals[4][ABRsize + 1];
    3. When calling toksplit, pass vals[i] directly:
    Code:
    t = toksplit(t, ',', vals[i], ABRsize);

    Comment

    • angusmiller
      New Member
      • Oct 2008
      • 2

      #3
      Thanks worked a charm! one more thing I need to be able to "reset" the vals array, can I do this without having to loop through the array like below?

      Code:
      for (i = 0; i < 4; i++) {
          strcpy(vals[i],"");
      }
      Thanks

      Comment

      • vekipeki
        Recognized Expert New Member
        • Nov 2007
        • 229

        #4
        If you want to set all items of your vals array to an empty string, this is the way to do it. You could even do it at the beginning of your program to ensure that all your strings are null-terminated.

        Comment

        • Tassos Souris
          New Member
          • Aug 2008
          • 152

          #5
          You can also use the memset() function:
          Code:
           memset( destination, value, size );

          Comment

          Working...