Lance_P
Lance_P

Reputation: 399

C Program Error "Conflicting types for 'function'"

Working on a project, and trying to understand why I am getting the errors that I am getting. I'm still new to C and it has been a struggle for me. I've opened literally 40 or 50 tabs on this subject to try and fix my issue, but to no avail.

My error message lies before main where I prototype a function that I have been provided with to use in this project. The function is called utf8_to_codepoint and it well.. converts utf8 values to Unicode codepoint values.

Currently the only error I am getting lies with my prototype statement: "conflicting types for 'utf8_to_codepoint'"

I have tried many different things, involving pointers, not involving pointers, including the 'const' and 'unsigned' keywords, etc, and nothing has made the errors go away.

I am still struggling with pointers, and I am unsure why this function takes pointers as its' parameters. From what I can see, the function only works with one character at a time (u), and I don't understand what the second parameter does.

Below is my prototyping to the function, my call to the function, and the code to the utf8_to_codepoint function.

int utf8_to_codepoint(char, int *);

int main(int argc, char **argv)
{
    char sampleText[3000];
    int lenptr = 3000;
    ...


    for(int i = 0; fgets(sampleText, 3000, fp) != NULL; i++)
    {
        int temp;
        temp = utf8_to_codepoint(sampleText[i], &lenptr);
        ...
    }

UTF8_To_Codepoint Function

 extern unsigned int utf8_to_codepoint(const unsigned char *u,
                                      int *lenptr) {
   // Returns 0 if something goes wrong
   // Passes back the length
   unsigned int cp = 0;

   *lenptr = 0;
   if (u) {
      if (*u < 0xc0) {
         cp = (unsigned int)*u;
         *lenptr = 1;
      } else {
         *lenptr = isutf8(u);
         if (*lenptr == 0) {
            return 0;
         }
         switch (*lenptr) {
            case 2:
                 cp = (u[0] - 192) * 64 + u[1] - 128;
                 break;
            case 3:
                 cp = (u[0] - 224) * 4096
                    + (u[1] - 128) * 64 + u[2] - 128;
                 break;
            default:
                 cp = (u[0] - 240) * 262144
                     + (u[1] - 128) * 4096
                     + (u[2] - 128) * 64 + u[3] - 128;
                 break;
         }
      }
   }
   return cp;
}

Upvotes: 0

Views: 683

Answers (2)

Elmar Peise
Elmar Peise

Reputation: 15473

The types of the functions clearly don't match:

The prototype is

int utf8_to_codepoint(char, int *);

while the function you define is

extern unsigned int utf8_to_codepoint(const unsigned char *u, int *lenptr);

specifically unsigned int != int and const unsigned char * != char.

You need to figure out which of these is the right declaration and replace the other.

Upvotes: 1

dbush
dbush

Reputation: 225362

You declare your function like this:

int utf8_to_codepoint(char, int *);

But define it like this:

unsigned int utf8_to_codepoint(const unsigned char *u,
                                  int *lenptr) {

The return type doesn't match, and the type of the first argument doesn't match.

Change the declaration to this:

unsigned int utf8_to_codepoint(const unsigned char *u, int *lenptr);

And change the call:

lenptr = 0;
....
for(int i = 0; fgets(sampleText, 3000, fp) != NULL; i += lenptr)
{
    int temp;
    temp = utf8_to_codepoint(&sampleText[i], &lenptr);
    ...
}

Upvotes: 3

Related Questions