-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric.c
48 lines (42 loc) · 837 Bytes
/
generic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ASSTR(s) #s
#ifndef GENERIC_SCAN
#define GENERIC_SCAN(TYPE, FRMT, PTR, LEN) {\
size_t SIZE = sizeof(TYPE);\
int cap = 2;\
LEN = 0;\
TYPE v;\
PTR = malloc(SIZE * cap);\
printf("Input "#TYPE" values:\n");\
while( scanf("%"#FRMT, &v) == 1 )\
{\
if(LEN == cap)\
{\
cap *= 2;\
TYPE * temp = PTR;\
PTR = malloc(SIZE * cap);\
memcpy(PTR, temp, LEN * SIZE);\
free(temp);\
}\
PTR[LEN] = v;\
LEN++;\
}\
}
#endif // GENERIC_GET
// populate array with ints from stdin
int main(int argc, char *argv)
{
printf(ASSTR(SCANFRMT));
printf(ASSTR(SCANFRMT));
SCANTYPE* input;
int length;
GENERIC_SCAN(SCANTYPE, SCANFRMT, input, length);
int i;
for (i=0; i<length; ++i)
{
}
printf("\n");
return 0;
}