c - In which situation does function prototype have to include parameter names? -


i studying c programming language k&r , there exercise below:

exercise 3-5: write function itob(n, s, b) converts integer n base b character representation in string s. in particular, itob(n, s, 16) formats n hexadecimal integer in s.

so wrote program this:

#include <stdio.h> #include <string.h> #define maxline 1000  void reverse(char ); void itob(int , char , int );  main() {     char line[maxline];     int n, i;     printf("integer: ");     scanf_s("%d",&n);     printf("base: ");     scanf_s("%d",&i);     itob(n, line, i);     printf("%s", line); }  void itob(int n, char s[], int b) {     int i, sign;      if ((sign = n) < 0)         n = -n;     = 0;          {          n % b < 10 ? (s[i++] = n % b + '0') : (s[i++] = n % b + '7');     }     while ((n /= b) > 0);     if (sign < 0)         s[i++] = '-';     s[i] = '\0';     reverse(s); }  void reverse(char s[]) {     int c, i, j;       (i = 0, j = strlen(s)-1; < j; i++, j--)     {         c = s[i];         s[i] = s[j];         s[j] = c;     } } 

i thought ok. when ran program, got error after input integer , base values, below:

unhandled exception @ 0x10022050 (msvcr110d.dll) in exercise 3-5.exe: 0xc0000005: access violation reading location 0x000000ec.

so program had trouble function itob call.

and after 2 hour struggle debugging, found cause function prototype of reverse need include parameter name this:

void reverse(char s[]); void itob(int , char , int ); 

the change made added s[] in function prototype of reverse , program worked properly.

i confused because learned parameter names of function prototype optional. why parameter name have include in function prototype of reverse? , why function prototype of itob not need parameter name , there not pop-up error? ide visual studio 2012.

thanks everyone's time , help.

the code in question has number of bugs, many of compiler should have @ least warned about.

when copied code , compiled using gcc, without additional arguments, first error message was:

c.c: in function ‘main’: c.c:16:5: warning: passing argument 2 of ‘itob’ makes integer pointer without cast [enabled default]      itob(n, line, i);      ^ c.c:6:6: note: expected ‘char’ argument of type ‘char *’  void itob(int , char , int );       ^ 

the error message correct. declared itob second parameter of type char, called second parameter of type char* (resulting implicit conversion of array line).

later, define itob second parameter of type char[] (which, since it's parameter, of type char*). incompatible earlier declaration, , should have been flagged compiler.

to answer question in title, in standalone prototype (one that's not part of function definition), parameter names optional. these 2 prototypes equivalent , valid:

void itob(int, char, int); 

and

void itob(int x, char y, int z); 

personally specify parameter names because makes code clearer.

for prototype that's part of function definition, names required, because that's how parameter objects defined.

but first, fix errors in code.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -