0

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?

Aiden Choi
  • 142
  • 7
  • 1
    `for (i=0; i==3; ++i)` you probably meant `for (i=0; i<3; ++i)` – AndersK May 24 '21 at 23:16
  • (float(volume+165) => (float)(volume+165) – 0___________ May 24 '21 at 23:16
  • Plenty small errors. You need to be more careful when programming – 0___________ May 24 '21 at 23:17
  • printf("Enter %c:\t", *vars[i]); wrong – 0___________ May 24 '21 at 23:17
  • Question title has nothing in common with the problem – 0___________ May 24 '21 at 23:18
  • @0___________ `also, %c prints out only the first character of vars[i] so the output is like this: enter h: . why is this?` is a problem with the format specification, no? – Aiden Choi May 24 '21 at 23:19
  • 1
    You have `printf("Enter %c:\t", *vars[i]);` — you probably want the entire word, and should therefore use `printf("Enter %s:\t", vars[i]);` – Jonathan Leffler May 24 '21 at 23:28
  • In your revised code, you have no variable `volume` so you can't print `(volume + 165) / 166.0`, with or without the cast. You either need to repeat the volume calculation in the `printf()` argument list or you need to reintroduce the variable `volume` so you don't repeat yourself. The latter is probably better. – Jonathan Leffler May 24 '21 at 23:31
  • 1
    The casting form `float(expression)` is valid in C++, but not in C. It must be written `(float)expression` or `(float)(expression)` (depending on what's in `expression`) in C. If you're using a C++ compiler to compile your C code, you might get away with the faulty C syntax (that is valid C++ syntax). But the two languages are different in so many ways that you should be using a C compiler to compile your C code. – Jonathan Leffler May 24 '21 at 23:35
  • @jonathanleffler `printf("Enter %s:\t", vars[i]);` got program to do what i wanted. how come `vars[i]` should be dereferenced when using `%c` and not when `%s`? how does this work? – Aiden Choi May 25 '21 at 00:42
  • There's a difference between `*vars[i]` and `vars[i]`. The first is a single character, the first in the array element; the second is a pointer to the first character in the array element. And the formats are correspondingly different: `%c` prints a single character and `%s` prints a (null-terminated) string. That's why my suggested revision made two changes — `%c` to `%s` and dropping the `*` from `*var[i]`. – Jonathan Leffler May 25 '21 at 00:46
  • @jonathanleffler why is it that `*vars[i]` will get a single character? how does this work? – Aiden Choi May 25 '21 at 00:51
  • The declaration is `char *vars[] = { … };` which means that when you write `vars[i]` the value is a `char *` and when you write `*vars[i]` the result is a `char`. This is the 'declaration mimics use' feature of C syntax. See [comp.lang.c FAQs](http://c-faq.com/decl/cdecl1.html) and [In C, what is the correct syntax for declaring pointers?](https://stackoverflow.com/questions/3280729/in-c-what-is-the-correct-syntax-for-declaring-pointers) and may other SO questions. – Jonathan Leffler May 25 '21 at 01:35

1 Answers1

0

volume is not declared... small mistake made while trying to get code to work. line 13 should have been:

printf("Volume: %d\nWeight: %f\n", (height*length*width), ((float)((height*length*width)+165) / 166.0))

line 10:

printf("Enter %s:\t", vars[i]); 
Aiden Choi
  • 142
  • 7