0

When I am trying to compile the code, I am getting error as follow

main.c:12:15: error: conflicting types for ‘ascii_to_hex’ unsigned char ascii_to_hex(unsigned char* buf)


#include <stdio.h>
#include <stdlib.h>
int main()
{
    
unsigned char str[] = {0x32, 0x35, 0x34, 0x035};
ascii_to_hex(str);

    return 0;
}

unsigned char ascii_to_hex(unsigned char* buf)
{
   unsigned char hundred, ten, unit, value;

   hundred = (*buf-0x30)*100;
   ten = (*(buf + 1)-0x30)*10;
   unit = *(buf+2)-0x30;     

   value = (hundred + ten + unit);
   printf("\nValue: %#04x \n", value);

   return value; 
}

What is wrong that I am doing here?

Danyss
  • 3
  • 3
  • `str` is a `unsigned char[4]`, but `ascii_to_hex` needs a `unsigned char*`. Pass `&str[0]` instead – Raildex Jul 14 '21 at 06:22
  • 1
    There should be more to that error telling you that there is an implicit declaration of the `ascii_to_hex` function. Move the function to be above `main` or declare a a function prototype before calling the function. – kaylum Jul 14 '21 at 06:27
  • 2
    @Raildex: This is not correct. There's nothing wrong with that part of the code, and it will work just fine so long as the function is declared prior to use. – Daniel McLaury Jul 14 '21 at 06:29
  • Thank you all, it worked now without any modification, I moved the function definition above main(). – Danyss Jul 14 '21 at 06:35

1 Answers1

0

You are using ascii_to_hex before declaring it, so the compiler is inferring that it has the "default" signature for a function. I forget what exactly that is, [EDIT: It is apparently

int ascii_to_hex()

-- I looked it up ], but whatever it is, it isn't

unsigned char ascii_to_hex(unsigned char* buf)

What the compiler is telling you is that what it inferred the signature of the function to be didn't match the signature it encountered later. This is lot clearer in the output from gcc:

cc     program.c   -o program
program.c: In function ‘main’:
program.c:11:1: warning: implicit declaration of function ‘ascii_to_hex’ [-Wimplicit-function-declaration]
   11 | ascii_to_hex(str);
      | ^~~~~~~~~~~~
program.c: At top level:
program.c:16:15: error: conflicting types for ‘ascii_to_hex’
   16 | unsigned char ascii_to_hex(unsigned char* buf)
      |               ^~~~~~~~~~~~
program.c:11:1: note: previous implicit declaration of ‘ascii_to_hex’ was here
   11 | ascii_to_hex(str);
      | ^~~~~~~~~~~~
make: *** [<builtin>: program] Error 1

so you may want to look into using a better compiler or IDE or turning off whatever part of your development pipeline is suppressing most of the relevant information.

To fix this, you need to either move the function definition above the definition of main, or give a prototype for the function so the compiler knows its signature at the time it's called.

Daniel McLaury
  • 4,047
  • 1
  • 15
  • 37
  • yes, you are correct!!I moved the function definition above main() without any modification and it worked fine. – Danyss Jul 14 '21 at 06:36