Thread: Why do I get conflicting types on this header file?

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    96

    Why do I get conflicting types on this header file?

    Hi guys!

    I get conflicting types, but I can't figure out why.

    in "main.c" I have 2 header files defined.
    compress.h
    ReadFromFiles.h

    Inside of "compress.h" I am using 1 function from "ReadFromFiles.h".
    When I press "build" I get conflicting types. This is on the "ReadFileIntoArray" function.

    This is the contents of "ReadFromFiles.h":
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
                                                            /* Prototype declarations. */
    char ReadFromFiles (char InputFileName[], char *InMode);/* char *InMode = "r". */
    char ReadFileIntoArray(char InputFileName[], char *InMode);
    
    char ReadFileIntoArray(char InputFileName[], char *InMode)
    {
      unsigned int i;                                                  /* Counter. */
      char Array[5000];
      FILE *ifp;
                                        /* Initialization. *//* "i" is initialized in the "for" cycle. */
      ifp = fopen(InputFileName, InMode);
    
      if (ifp == NULL)/* This checks the filename in the global variable "ofn.lpstrFile" and if its "0" displays the down message. */
      {
            printf("The dialog was canceled by the user, the file doesn't exists or doesn't have the right permissions!");                                      /* If the file can not be opened, quit. */
            return -9998;
      }
    
      else
      {
        for(i=0; (feof(ifp)); i++)/* Main block. */
            Array[i]=fgetc(ifp);
      }
        Array[i+1]='\n';
    
        return Array;
    }
    
    char ReadFromFiles (char InputFileName[], char *InMode)/* char *InMode = "r". */
    {
      unsigned int i;                                                  /* Counter. */
      char c;
      FILE *ifp;
    
      ifp = fopen(InputFileName, InMode);
    
      if (ifp == NULL)/* This checks the filename in the global variable "ofn.lpstrFile" and if its "0" displays the down message. */
      {
            printf("The dialog was canceled by the user, the file doesn't exists or doesn't have the right permissions!");                                      /* If the file can not be opened, quit. */
            return -9998;
      }
    
      else
      {
          return fgetc(ifp);
      }
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    An C header file should not normally contain the bodies of functions.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Aside from stahta01's concerns, I note that you declared ReadFileIntoArray as returning a char, but within it you have one return statement that returns -9998 (i.e., an int almost certainly outside of the range of char) and another that returns Array (i.e., an array of 5000 char, so it will be converted to a pointer to its first element, i.e., a pointer to char).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    The "int" part I did not notice. The pointer type I know, on the other side a pointer is waiting to take the array. But I can't figure out why I get conflicting types. If the header file should not contain the body, what should?

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    I figured out the problem. The definition of the function(the "#include file") should be before "compress.h" or I am calling the function before I declare it. The third post from here helped:
    arrays - Getting "conflicting types for function" in C, why? - Stack Overflow

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I have done ".jpeg" compression in C(DSP TigerShark processors)
    I'm astonished by your accomplishment, given how truly awful your approach to reading files is.

    Code:
    char ReadFileIntoArray(char InputFileName[], char *InMode)
    {
      unsigned int i;                                                  /* Counter. */
      char Array[5000];
      FILE *ifp;
                                        /* Initialization. *//* "i" is initialized in the "for" cycle. */
      ifp = fopen(InputFileName, InMode);
     
      if (ifp == NULL)/* This checks the filename in the global variable "ofn.lpstrFile" and if its "0" displays the down message. */
      {
            printf("The dialog was canceled by the user, the file doesn't exists or doesn't have the right permissions!");                                      /* If the file can not be opened, quit. */
            return -9998;
      }
     
      else
      {
        for(i=0; (feof(ifp)); i++)/* Main block. */
            Array[i]=fgetc(ifp);
      }
        Array[i+1]='\n';
     
        return Array;
    }
    1. feof() does not work how you think it does. You will end up with (char)EOF stored in your array.
    2. There is no range checking of your array subscripts.
    3. You're returning a pointer to a local variable (Array)
    4. You're leaking open file handles.



    Your ReadFromFiles is even worse.
    One leaked file handle for every char read.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ArakelTheDragon
    The definition of the function(the "#include file") should be before "compress.h" or I am calling the function before I declare it.
    No, you got that all wrong. This is what you should do:
    • Create a header file with header inclusion guards.
    • In the header file, within the inclusion guards, write the forward declaration (i.e., prototype) of the function.
    • Create a corresponding source file. Though it is not required, include the header file at the top of the source file. In the source file, write the function definition (i.e., implementation).
    • In the other source file containing the main function, include the header before all the function definitions, typically after you include the standard and system headers.
    • Compile all the source files and and link to form your executable program (or library file, if you didn't have the main function).

    You don't actually need a separate source file to correspond with the header, but it can be useful to organise your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    I alredy did that, thanks Laserlight.

    The rest:
    1. feof() does not work how you think it does. You will end up with (char)EOF stored in your array. How should I use "feof()"?
    2. There is no range checking of your array subscripts. How should I do this?
    3. You're returning a pointer to a local variable (Array), I understand its a local variable and its should be cleared after I exit the function, but it really isn't, I don't know why.
    4. You're leaking open file handles. How to fix this?

  9. #9
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    Normally I program microcontrollers, the approach on "OS'" is different.

    EDIT:
    I think its time for the "main" code as I began to get a lot of "0xC0000005". Before with header files everything was working. The interesting part is why does "strupr("a");" lead to this error?

    Code:
    #include "../OS_Library/Functions.h"
                            /* Standard libraries. */
    #include <stdio.h>        /* Standard IN/OUT library. */
    #include <stdint.h>        /* Standard extended....... library. */
    /* #include <io.h> */         /* This library is not present in this compiler. */
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    #include <math.h>
                                            /* Custom libraries. */
    #include "../OS_Library/CharacterCount.c"
    #include "../OS_Library/Compare2Strings.c"
    #include "../OS_Library/WriteToFiles.c"
    #include "../OS_Library/StartProgmrasFromAList.c"
    #include "../OS_Library/AnyNumberToAll.c"
    #include "../OS_Library/CharacterToInteger.c"
    #include "../OS_Library/Report.c"
    #include "../OS_Library/ReturnASCIIOfChar.c"
    #include "../OS_Library/Calculator.c"
    #include "../OS_Library/Encrypt.c"
    #include "../OS_Library/ReadFromFiles.c"
    #include "../OS_Library/Compress.c"
    
    
                            /* Function prototype definitions. */
    int main ()
    {
                                            /* Local variables. */
        unsigned int Option;
        char Buffer[50];
    
        printf ("\nPlease select an operation:\n");
        printf ("0. Character count.\n");
        printf ("1. Compare 2 strings.\n");
        printf ("2. Return quotient or remainder.\n");
        printf ("3. Any number format to all formats(binary, decimal, hexadecimal, octal).\n");
        printf ("4. Report.\n");
        printf ("5. Return the ASCII value of a char.\n");
        printf ("6. Convert to upper case.\n");
        printf ("7. Calculator.\n");
        printf ("8. Encrypt/Decrypt.\n");
        printf ("9. Start all programs from a list.\n");
        printf ("10. Compress, decompress a '.zip' file.\n");
        printf ("9999. Exit\n");
        scanf  ("%d", &Option);
    
        switch (Option)
        {
        case 0:
            CharacterCount ();
            break;
        case 1:
            Compare2Strings();
            break;
        case 2:
            QuotinentRemainder(5,5, "Quotient");
            break;
        case 3:
            AnyNumberToAll(0xFFFE);/* Prints the binary, hex, decimal, octal version. In "C" the number format does not matter. */
            break;
        case 4:
            Report("Comment", itoa(0x10, Buffer, 10), itoa(0x10, Buffer, 10), "Comment1");/* If the measured and rated value are "int", they must be converted to "char". */
        case 5:
            ReturnASCIIOfChar('A');
        case 6:
            strupr("a");
            break;
        case 7:
            Calculator();
            break;
        case 8:
            Encryptmain();
            break;
        case 9:
            StartProgmrasFromAList("Files.t");
        case 10:
            Compressmain();
            break;
        case 9999:
            exit (0);
        default:
            printf ("Invalid option! Pick a valid option please.\n");
            break;
    
        /* WriteToFiles("T23.test", "Test_asd");*//* '0' or Filename, '0' or text to be written in the file, '0' or use external content flag == 1. */
        }
    }
    Last edited by ArakelTheDragon; 03-16-2019 at 04:06 AM. Reason: Add more information.

  10. #10
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    I solved the problem with the error "0xC0000005".

    Can you please explain about "feof()", I see in the tutorial that they use it in the same way.

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Long story short, you can't modify "a": It is a String Literal - As a microcontroller guy, you'll appreciate why you'd put a string in programme memory to save a bit of RAM

    You'll need to create a variable and then you'll be able to change it (as long as you don't declare it constant)


    As for you question about feof, consider what happens when fgetc returns EOF because it is at the end of file - The first thing that your code does is put it in Array[i].


    Be aware that there is a "gotcha" with putting feof in a control loop - FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    The better way is to check to see if EOF is returned from fgetc, and also check to see if i<ARRAYSIZE


    You'll need to learn a bit about scope for your variables -
    C Scope Rules
    Array is a local variable and as soon as you exit the function, your computer is free to use that memory for something else.

    There are a few ways around it, but the easiest way to get you started is to declare your array as static.


    When you open a file, you'll need to close it - FAQ > Work with files (C) - Cprogramming.com
    Fact - Beethoven wrote his first symphony in C

  12. #12
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    Thanks for the help!

    I know the static command and the const command. I used a global variable instead, but your approach is better and it opens up possibilities, I remember reading it in the Kernighan and Ritchie C 2nd edition.

    As for the control loop(I assume you mean "for" is wrong, "while" is not), I need to change "i++" to "++i" in the "for cycle"?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ArakelTheDragon
    I know the static command and the const command. I used a global variable instead, but your approach is better and it opens up possibilities
    You should neither make the array a static local variable nor a global variable. Rather, provide a pointer parameter such that the array can be passed as an argument (converted to a pointer to its first element). You should also provide a size_t parameter to denote the size of the array so as to avoid buffer overflow, e.g., by including a check in the loop condition when looping over the array to write to it.

    You might then return (as a size_t) the number of array elements that were written to, or have a pointer to size_t parameter to function as an out parameter so the caller can determine how many elements are in use.

    Alternatively, it looks like you might be trying to read the file as a string, in which case you can just null terminate the string (which you do not appear to have done).

    Quote Originally Posted by ArakelTheDragon
    As for the control loop(I assume you mean "for" is wrong, "while" is not), I need to change "i++" to "++i" in the "for cycle"?
    Whether you use for or while doesn't matter so much in this case: some people might prefer a while loop because they think that as the loop condition is not dependent on the array index, a while loop is more appropriate; other people might prefer a for loop because there is an array index that is incremented on each iteration.

    The point is that you should not use feof to control the loop in this way, but rather should make use of the return value of fgetc, comparing it to EOF. This also means that you need to store the return value of fgetc in an int rather than in Array[i], and only copy this int to Array[i] if the loop does not terminate on that iteration.

    Other than spelling, there is no difference between i++ and ++i when used as a standalone expression in a statement.
    Last edited by laserlight; 03-17-2019 at 07:07 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    Can you give an example about "i++" and "++i", since I am a young padawan.

    c - What is the difference between ++i and i++? - Stack Overflow

  15. #15
    Registered User
    Join Date
    Dec 2016
    Posts
    96
    Here is my new code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
                                                            /* Prototype declarations. */
                            /* Global variables. */
    extern char Array[5000];/* Can be replaced by a static local variable, which is not erased on function close. */
    
    char ReadFileIntoArray(char InputFileName[], char *InMode)
    {
      unsigned int i;                                                  /* Counter. */
      char c;
      FILE *ifp;
                                        /* Initialization. *//* "i" is initialized in the "for" cycle. */
      ifp = fopen(InputFileName, InMode);
    
      if (ifp == NULL)/* This checks the filename in the global variable "ofn.lpstrFile" and if its "0" displays the down message. */
      {
            printf("The dialog was canceled by the user, the file doesn't exists or doesn't have the right permissions!");                                      /* If the file can not be opened, quit. */
            return 1;
      }
    
      else
      {
            i=0;
            while(1)
            {
                c = fgetc(ifp);
                if( feof(ifp) )
                    break ;
    
                printf("%c", c);
                Array[i]=fgetc(ifp);
                i++;
            }
      }
        Array[i+1]='\0';
        fclose(ifp);/* All files must be closed. */
        return 0;
    }
    
    char ReadFromFiles (char InputFileName[], char *InMode)/* char *InMode = "r". */
    {                                                  /* Counter. */
      FILE *ifp;
    
      ifp = fopen(InputFileName, InMode);
    
      if (ifp == NULL)/* This checks the filename in the global variable "ofn.lpstrFile" and if its "0" displays the down message. */
      {
            printf("The dialog was canceled by the user, the file doesn't exists or doesn't have the right permissions!");                                      /* If the file can not be opened, quit. */
            return 1;
      }
    
      else
      {
        if(feof(ifp))
        {
        fclose(ifp);/* All files must be closed. */
            return '\0';
        }
        else
            return fgetc(ifp);
      }
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conflicting Types
    By TKEsquad in forum C Programming
    Replies: 5
    Last Post: 05-08-2015, 08:31 AM
  2. Replies: 7
    Last Post: 01-22-2014, 09:48 AM
  3. conflicting types
    By Sotiris Kaniras in forum C Programming
    Replies: 27
    Last Post: 01-01-2013, 09:18 AM
  4. Conflicting types???
    By kwikness in forum C Programming
    Replies: 11
    Last Post: 10-07-2007, 11:53 PM
  5. conflicting types for...
    By dudinka in forum C Programming
    Replies: 3
    Last Post: 05-14-2005, 07:03 AM

Tags for this Thread