as i was going through an example from a book:
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int height, length, width, volume, weight;
6
7 printf("Enter height of box: ");
8 scanf("%d", &height);
9 printf("Enter length of box: ");
10 scanf("%d", &length);
11 printf("Enter width of box: ");
12 scanf("%d", &width);
13
14 volume = height*length*width;
15 weight = (volume + 165) / 166;
16
17 printf("Volume: %d\n", volume);
18 printf("Dimensional weight: %d\n", weight);
19
20 return 0;
21 }
i wanted to make it shorter like so:
1 #include <stdio.h>
2
3 int main(void)
4 {
5 int i, height, length, width;
6 char *vars[] = {"height", "length", "width"};
7 int *vals[] = {&height, &length, &width};
8
9 for (i=0; i==3; ++i) {
10 printf("Enter %c:\t", *vars[i]);
11 scanf("%d", vals[i]);
12 }
13 printf("Volume: %d\nWeight: %f\n", (height*length*width), (((float(volume+165)) / 166.0)));
14 }
this gives compile error and i have no clue why:
└─$ gcc dweight.c
dweight.c: In function ‘main’:
dweight.c:13:72: error: expected ‘)’ before ‘volume’
13 | printf("Volume: %d\nWeight: %f\n", (height*length*width), (((float(volume+165)) / 166.0)));
| ^~~~~~
| )
also, %c
prints out only the first character of vars[i]
so the output is like this: enter h:
. why is this?