Not bad, I had quickly written this up during the course of this topic:
Code:
#include <stdio.h>
#include <string.h>
int main() {
int neighbours[] = { 1600, 1620, 1640, 1660, 1680,
1700, 1720, 1740, 1760, 1780 };
short count = 3;
char *strData = NULL;
int i;
for(i = 0; i < count; i++) {
char buff[6]; // Four digits + ',' + '\0'
sprintf(buff, "%d,",neighbours[i]);
strData = realloc(strData, ((strData == NULL ? 0 : strlen(strData)) +
strlen(buff)) * sizeof(char) + sizeof(char));
/* Alright, that's the size of the main buffer + size of the new buffer + null character */
if(strData == NULL) return 1;
strcat(strData,buff);
}
strData[strlen(strData)-1] = '\0'; // The last extra comma
printf("%s",strData);
free(strData);
return 0;
}
It's a bit neater with memory management but it's very similar to your result.
EDIT: Ok, hopefully this is better, now. Although now the realloc statement is so contrived, I'm not even sure it it's efficient anymore.