Skip to content

Commit aa9db6a

Browse files
committed
clib
1 parent eb25e09 commit aa9db6a

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

Functions/clib/clib.h

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,42 +87,41 @@
8787
(void)! scanf(format, &matrix[i][j]);
8888

8989
// macro for reading input from stdin as long as it lasts
90-
// creates a new array of the given type and format,
91-
// sets the parameter size to the number of elements read
90+
// creates a new array of the given type and format, and
91+
// a new variable size with the number of elements read
9292
// and sets the last element to '\0'
9393
// Examples: READ(int, "%d", myInts, size);
9494
// READ(double, "%lf", myDbls, size);
9595
// READ(char, "%c", myChrs, size);
9696
#define READ(type, arr, format, size) \
9797
type *arr = safeCalloc(100, sizeof(type)); \
98-
int arr##Len = 0; type arr##var; \
98+
size_t size = 0; type arr##var; \
9999
while (scanf(format, &arr##var) == 1) { \
100-
arr[arr##Len++] = arr##var; \
101-
if (arr##Len % 100 == 0) {\
102-
arr = safeRealloc(arr, (arr##Len + 100) * sizeof(type)); \
103-
memset(arr + arr##Len, 0, 100 * sizeof(type)); \
100+
arr[size++] = arr##var; \
101+
if (size % 100 == 0) { \
102+
arr = safeRealloc(arr, (size + 100) * sizeof(type)); \
103+
memset(arr + size, 0, 100); \
104104
} \
105105
} \
106-
size = arr##Len;\
107-
arr[arr##Len] = '\0';
106+
arr[size] = '\0';
108107

109-
// macro for reading input from stdin until a given
108+
// macro for reading input from stdin until a given
110109
// delimiter is encountered
111-
// returns a new array of the given type
112-
// and sets size to the number of chars read
110+
// returns a new array of the given type and creates a
111+
// new variable size with the number of elements read
113112
// Examples: READ_UNTIL(char, myString, "%c", '\n', size);
114113
// READ_UNTIL(int, myInts, "%d", '.', size);
115114
#define READ_UNTIL(type, arr, format, delim, size) \
116115
type *arr = safeCalloc(100, sizeof(type)); \
117-
size_t arr##Len = 0; type arr##var; \
116+
size_t size = 0; type arr##var; \
118117
while (scanf(format, &arr##var) == 1 && arr##var != delim) { \
119-
arr[arr##Len++] = arr##var; \
120-
if (arr##Len % 100 == 0) { \
121-
arr = safeRealloc(arr, (arr##Len + 100) * sizeof(type)); \
122-
memset(arr + arr##Len, 0, 100); \
118+
arr[size++] = arr##var; \
119+
if (size% 100 == 0) { \
120+
arr = safeRealloc(arr, (size + 100) * sizeof(type)); \
121+
memset(arr + size, 0, 100); \
123122
} \
124123
} \
125-
arr[arr##Len] = '\0'; size = arr##Len;\
124+
arr[size] = '\0';\
126125
(void) ! scanf("%*c");
127126

128127
//::::::::::::::::::::::::: INTEGERS.C :::::::::::::::::::::::::://

0 commit comments

Comments
 (0)