Thread: conflicting types

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    54

    conflicting types

    Hello everyone.. I need your help! I want to make a header file for the prototype of function sum and two .c files. One for main and one for the implementation of the function. So, when I try to compile the implementation file.c it says:

    linux01:/home/users/-----------/Exerc3>gcc -o himplementation.o himplementation.c
    himplementation.c:5: error: conflicting types for 'sum'
    hfiles.h:1: error: previous declaration of 'sum' was here

    What's the problem?



  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post 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

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    main.c

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "himplementation.h"
    
    
    int main(int argc, char *argv[])
    {
        int n, m, k, j;
    
    
        scanf("%d %d", &n, &m);
        int inp[n][m];
    
    
        for (k = 0; k < n; k++) {
            for (j = 0; j < m; j++) {
                scanf("%d", &inp[k][j]);
            }
        }
        printf("\n");
    
    
        for (k = 0; k < n; k++) {
            for (j = 0; j < m; j++) {
                sum(n, m, k, j, inp);
            }
        }
    
    
    //  for (k = 0; k < n; k++)
    //      free(*(input+k));
    //  free(input);
    
    
        return 0;
    }
    function implementation.c

    Code:
    #include <stdio.h>
    #include "hfiles.h"
    
    
    int sum(int n, int m, int k, int j, int input[n][m])
    {
        int maxsum, max;
    
    
        max = 0;
        maxsum = 0;
        if (k == n-1) {
            while (j < m-1) {
                if (input[k][j] > max) {
                    max = input[k][j];
                }
                j++;
            }
            maxsum = input[k][j] + max;
            max = 0;
        }
        else if (k < n && j == 0) {
            if (input[k+1][0] > max)
                max = input[k+1][0];
            printf("max1 = %d, input[i+1][0] = %d\n", max, input[k+1][0]);
            if (input[k+1][1] > max)
                max = input[k+1][1];
            printf("max2 = %d, input[i+1][1] = %d\n", max, input[k+1][1]);
            if (input[k+1][m-1] > max)
                max = input[k+1][m-1];
            printf("max3 = %d, input[i+1][m] = %d\n", max, input[k+1][m-1]);
    
    
            maxsum = input[k][j] + max;
            max = 0;
        }
        else if (k < n && (j > 0 && j < m-1)) {
                if (input[k+1][j-1] > max)
                    max = input[k+1][j-1];
                if (input[k+1][j] > max)
                    max = input[k+1][j];
                if (input[k+1][j+1] > max)
                    max = input[k+1][j+1];
    
    
                maxsum = max + input[k][j];
                max = 0;
        }
        else if (k < n && j == m-1) {
                if (input[k+1][m-2] > max)
                    max = input[k+1][m-2];
                if (input[k+1][m-1] > max)
                    max = input[k+1][m-1];
                if (input[k+1][0] > max)
                    max = input[k+1][0];
    
    
                maxsum = max + input[k][j];
                max = 0;
        }
        printf("%d, max = %d, maxsum = %d\n", input[k][j], max, maxsum);
        //sum(n, m, i, j+1, input);
    }
    prototype of function:

    Code:
    int sum(int, int, int, int, int);

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look at your function's prototype:
    Code:
    int sum(int, int, int, int, int);
    Look at your function's definition:
    Code:
    int sum(int n, int m, int k, int j, int input[n][m])
    Clearly, the fifth parameter does not have the same type.

    By the way, I suggest that you leave your parameter names in the function prototype.
    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

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Ok.. So, what I have to do?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Change your prototype.
    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

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Into what?

  8. #8
    Registered User
    Join Date
    Jun 2010
    Location
    In a house
    Posts
    15
    integer array

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Like this?
    Code:
    int sum(int n, int m, int, int, int input[n][m]);

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Copy and paste line 5 and put a semi-colon on the end. That's the best way to do it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    When I do so, it says:
    /usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib/crt1.o: In function `_start':
    (.text+0x18): undefined reference to `main'
    collect2: ld returned 1 exit status

  12. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Quote Originally Posted by Sotiris Kaniras View Post
    When I do so, it says:
    /usr/lib/gcc/i486-linux-gnu/4.2.4/../../../../lib/crt1.o: In function `_start':
    (.text+0x18): undefined reference to `main'
    collect2: ld returned 1 exit status
    Well?

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    undefined reference to `main'
    Wrong command on the command line or your code is missing a main function.

    I would say wrong command is the most likely; Edit the "-c" option is likely needed.

    Tim S.
    Last edited by stahta01; 12-30-2012 at 01:46 PM.
    "...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

  14. #14
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    I just wrote "gcc -o himplementation.o himplementation.c"

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Sotiris Kaniras View Post
    I just wrote "gcc -o himplementation.o himplementation.c"
    Likely need the "-c" option to compiler to object code.
    Code:
    gcc -c -o himplementation.o himplementation.c
    "...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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conflicting types for strdup
    By thames in forum C Programming
    Replies: 3
    Last Post: 12-06-2012, 12:09 PM
  2. conflicting types in functions
    By prince777 in forum C Programming
    Replies: 3
    Last Post: 12-19-2011, 09:07 AM
  3. Conflicting types error
    By mastrxplodr in forum C Programming
    Replies: 4
    Last Post: 04-12-2011, 11:56 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