Ok, this is guaranteed to be something silly which I'm missing, but I cannot see it at the moment.
Code:
char line[] = "00:name:type:ssid:secretsecretsecretsecretsecretsecret\n";
csflsplit2(line, sizeof(line)/sizeof(char), ':');
void csflsplit2(char csfline2[], int len, char sep) {
char connarr[5][64];
int i, y;
int x = 0;
for (i=0 ; i<len ; i++) {
if (csfline2[i] == sep) {
y = i-1;
printf("%s\n", strndup(csfline2+x, (y+1)-x));
strcpy(connarr[i], strndup(csfline2+x, (y+1)-x));
x =+(y+2);
}
printf("i: %i\n", i);
}
printf("%s", strndup(csfline2+x, (y+1)-x));
//strcpy(connarr[i+1], strndup(csfline2+x, (y+1)-x));
}
If I run this WITHOUT the red line, which copies the "substrings" into the connarr[][] array as it goes along, the output is fine. The substrings are written, and the i counter runs up to the array length and the for loop quits.
Howver, if I INCLUDE the red line, copying the "substrings" into the connarr[][] array, the whole thing goes in a never-ending loop. After the i counter reaches 56, it's reset to zero and it all starts over again.
Output WIHTOUT the strcpy line:
i: 0
i: 1
00
i: 2
...
i: 6
name
i: 7
...
i: 11
type
i: 12
...
i: 16
ssid
i: 17
...
i: 55
secretsecretsecretsecretsecretsecret
Output INCLUDING the strcpy line:
i: 0
i: 1
00
i: 2
...
i: 6
name
i: 7
...
i: 11
type
i: 12
...
i: 16
ssid
i: 17
...
i: 55
secretsecretsecretsecretsecretsecret
i: 0
i: 1
00
i: 2
...
etc.
I know - there's obviously some basic logic which I'm missing here, but could someone a bit more "awake" than me point it out? I'd be really grateful :-)