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?
Expand|Select|Wrap|Line Numbers
- #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;
- }
Angus