summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <smadurange@users.noreply.github.com>2022-01-18 19:58:13 +0800
committerSadeep Madurange <smadurange@users.noreply.github.com>2022-01-18 19:58:13 +0800
commitea5d3be32de1acb35a95d8043c81dafc033e3474 (patch)
treeab4d69d8ccb3dfb6e4bfde22274fcd440a1f6fdf
parentb514be2b8b81162921ad166afc59aeba15c3ffe2 (diff)
downloadk&r-exercises-ea5d3be32de1acb35a95d8043c81dafc033e3474.tar.gz
5.11
-rw-r--r--5/11.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/5/11.c b/5/11.c
index e40a051..0399526 100644
--- a/5/11.c
+++ b/5/11.c
@@ -3,11 +3,13 @@
#define TABSIZE 8
#define MAXLEN 1000
+#define COLCOUNT 100
void entab(char *s, char *t, int *tablist, int tablistc);
int main(int argc, char *argv[]) {
- int colc, *tablist, *tablistp;
+ int colsize;
+ int tablistc, *tablist, *tablistp;
char *s, *col, *colp, *colstr;
s = malloc(sizeof(char) * MAXLEN);
@@ -18,19 +20,27 @@ int main(int argc, char *argv[]) {
tablist = malloc(sizeof(int));
tablist[0] = 8;
} else {
- tablist = malloc(sizeof(int) * 100);
+ tablist = malloc(sizeof(int) * COLCOUNT);
tablistp = tablist;
- for (colstr = argv[1]; *colstr != 0; colstr++) {
+ for (colstr = argv[1], tablistc = 0; *colstr != 0; colstr++) {
if (*colstr == '"')
continue;
if (*colstr == ',' || *colstr == ' ') {
- // todo: realloc based on colc
- colc++;
+ tablistc++;
+ if (tablistc != 1 && tablistc % COLCOUNT == 1) {
+ tablist = realloc(tablist, sizeof(int) * COLCOUNT);
+ if (tablist == NULL) {
+ printf("error: realloc\n");
+ free(tablist);
+ return 1;
+ }
+ }
*colp++ = 0;
colp = col;
*tablistp = atoi(col);
tablistp++;
} else {
+ // todo: realloc based on colc
*colp++ = *colstr;
}
}