Thread: getting "conflicting types for Array" and "previous declaration of Array" errors...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    66

    getting "conflicting types for Array" and "previous declaration of Array" errors...

    please help me understand what I did wrong.

    Code:
    ssma-imac:ENG-3211 ssma$ gcc -g hw_5_2.c -o hw_5_2hw_5_2.c:61: warning: conflicting types for ‘ReadArray’
    hw_5_2.c:43: warning: previous declaration of ‘ReadArray’ was here
    hw_5_2.c:69: warning: conflicting types for ‘SearchArray’
    hw_5_2.c:44: warning: previous declaration of ‘SearchArray’ was here
    that is the error on the following code:

    Code:
    #include<stdlib.h>#include<stdio.h>
    #include<unistd.h>
    #include<math.h>
    #define SIZE 10
    
    
    // 1. assume that the target has not been found
    // 2. start with initial array element
    // 3. repeat while the target is not found and there are more array elements
    //	4. if the current element matches the target
    //		5. set a flag to indicate that the target has been found
    //		else
    //		6. advance to the next array element
    // 7. if the target was found
    //	8. return the target index as the search result and display an appropriate message
    //	else
    //	9. return -1 as the search result and display an appropriate message
    // * this algorithm is known as the Linear Search Algorithm
    
    
    void ReadArray(int A[], int size);
    void SearchArray(int A[], int target, int size);
    
    
    int main(void)
    {
    	int A[SIZE], target, size;
    
    
    	ReadArray(A, SIZE);
    
    
    	printf("Please enter the value to search for: ");
    	scanf("%d", &target);
    
    
    	SearchArray(A, target, SIZE);
    
    
    	return(0);
    }
    
    
    ReadArray(int A[], int size)
    {
    	int i;
    	printf("Please enter %d integer numbers separated by spaces:\n", size);
    	for (i = 0; i < size; i++)
    		scanf("%d", &A[i]);
    }
    
    
    SearchArray(int A[], int target, int size)
    {
    	int i;
    	for (i = 0; i < size; i++)
    	{
    		if ( target == A[i])
    		
    			printf("Element %d contains the target %d", i, target);
    		else 
    			printf("Target %d was not found", target);
    			break;
    		
    	}
    }
    I have everything declared as int, so i dont get previous declaration, and what does conflicting type mean?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,850
    You forward declared the functions as returning void, but then forgot about the return type when you defined the functions.

    By the way, in SearchArray, your for loop runs at most once. Remember to use braces to have a block of code if you want the body of an if/else to span more than one statement.
    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 HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Fixed

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <math.h>
    #define SIZE 10
    
    
    void ReadArray(int A[], int size);
    void SearchArray(int A[], int target, int size);
    
    
    
    
    int main( )
    {
        int A[SIZE], target;
    
    
    
    
        ReadArray(A, SIZE);
    
    
    
    
        printf("Please enter the value to search for: ");
        scanf("%d", &target);
    
    
    
    
        SearchArray(A, target, SIZE);
    
    
    
    
        return(0);
    }
    
    
    
    
    void ReadArray(int A[], int size)
    {
        int i;
        printf("Please enter %d integer numbers separated by spaces:\n", size);
        for (i = 0; i < size; i++)
            scanf("%d", &A[i]);
    }
    
    
    
    
    
    void SearchArray(int A[], int target, int size)
    {
        int i;
        for (i = 0; i < size; i++)
        {
            if ( target == A[i])
    
    
                printf("Element %d contains the target %d", i, target);
            else
                printf("Target %d was not found", target);
                break;
    
    
        }
    }
    Code:
    ReadArray(int A[], int size)
    Just because you declare a function at the top of your code, doesn't mean you don't have to declare the return value again as void. Your function declaration must be the same as its' definition.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,850
    Quote Originally Posted by lieb
    Fixed
    For the first part that I mentioned, yes, though I don't understand why you introduced all those unnecessary blank lines that were not in lieb's code. In my opinion, they make the code harder, not easier, to read.
    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
    May 2013
    Posts
    66
    ok its late for me, sorry im not following what your saying. i declared them as void yes, and when i create the function they are type void in such that they have no return type. obviously that is bad, but my brain is not working. can you give a bit more detail please and thank you.

  6. #6
    Registered User
    Join Date
    May 2013
    Posts
    66
    duh, ok im blonde i see my error and what you were saying Laswerlight. ty. now i need to figure out why my logic is not working...

    Code:
    ssma-imac:ENG-3211 ssma$ ./hw_5_2Please enter 10 integer numbers separated by spaces:
    1 2 3 4 5 6 7 8 9 10
    Please enter the value to search for: 11
    Target 11 was not found
    
    
    ssma-imac:ENG-3211 ssma$ ./hw_5_2
    Please enter 10 integer numbers separated by spaces:
    1 2 3 4 5 6 7 8 9 10
    Please enter the value to search for: 7
    Target 7 was not found
    so something is wrong in my search function...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,850
    Quote Originally Posted by lieb
    i declared them as void yes, and when i create the function they are type void in such that they have no return type.
    A return type of void indicates that the function has no return value. However, the function still has a return type, i.e., void. I think I mentioned to you previously that if you omit the return type when declaring (or defining) a function, the return type defaults to int, though this practice is discouraged.

    As such, this declares ReadArray as having a return type of void, and thus ReadArray does not return a value:
    Code:
    void ReadArray(int A[], int size);
    Whereas this defines ReadArray as having a return type of int, and thus ReadArray returns an int value, even though the return statement is missing (therefore potentially leading to undefined behaviour):
    Code:
    ReadArray(int A[], int size)
    {
        int i;
        printf("Please enter %d integer numbers separated by spaces:\n", size);
        for (i = 0; i < size; i++)
            scanf("%d", &A[i]);
    }
    Consequently, the forward declaration of ReadArray and its definition do not match, hence the error.

    As showed by HelpfulPerson in post #3, the correct thing to do here is to define ReadArray with a return type of void:
    Code:
    void ReadArray(int A[], int size)
    {
        int i;
        printf("Please enter %d integer numbers separated by spaces:\n", size);
        for (i = 0; i < size; i++)
            scanf("%d", &A[i]);
    }
    EDIT:
    Quote Originally Posted by lieb
    now i need to figure out why my logic is not working...
    Read my post #2 again.
    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
    May 2013
    Posts
    66
    hmm added some code just to verify that my array and search "target" are being read properly by the compiler, they are but it is still not finding the target value even though it is clearly in the array:

    Code:
    ssma-imac:ENG-3211 ssma$ ./hw_5_2Please enter 10 integer numbers separated by spaces:
    1 2 3 4 5 6 7 8 9 10
    Please enter the value to search for: 7
    The target value is 7
    
    
    The array [0] elements consists of the following 1
    
    
    The array [1] elements consists of the following 2
    
    
    The array [2] elements consists of the following 3
    
    
    The array [3] elements consists of the following 4
    
    
    The array [4] elements consists of the following 5
    
    
    The array [5] elements consists of the following 6
    
    
    The array [6] elements consists of the following 7
    
    
    The array [7] elements consists of the following 8
    
    
    The array [8] elements consists of the following 9
    
    
    The array [9] elements consists of the following 10
    Target 7 was not found
    Code:
    void SearchArray(int A[], int target, int size)
    {
    	int i;
    	printf("The target value is %d\n", target);
    
    
    	for (i = 0; i < size; i++)
    	printf("\nThe array [%d] elements consists of the following %d\n", i , A[i]);
    
    
    	for (i = 0; i < size; i++)
    	{
    		if ( target == A[i])
    		
    			printf("Element %d contains the target %d\n\n", i+1, target);
    		else 
    			printf("Target %d was not found\n\n", target);
    			break;
    		
    	}
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,850
    This for loop:
    Code:
        for (i = 0; i < size; i++)
        {
            if ( target == A[i])
             
                printf("Element %d contains the target %d\n\n", i+1, target);
            else
                printf("Target %d was not found\n\n", target);
                break;
             
        }
    is equivalent to:
    Code:
        for (i = 0; i < size; i++)
        {
            if ( target == A[i])
            {
                printf("Element %d contains the target %d\n\n", i+1, target);
            }
            else
            {
                printf("Target %d was not found\n\n", target);
            }
    
            break;
        }
    Do you see the problem now? This is why I prefer to use braces to denote blocks for if statements and loops even when they are unnecessary.
    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

  10. #10
    Registered User
    Join Date
    May 2013
    Posts
    66
    Quote Originally Posted by laserlight View Post
    By the way, in SearchArray, your for loop runs at most once. Remember to use braces to have a block of code if you want the body of an if/else to span more than one statement.
    yes that is what i thought, but when i add the squiggly brackets i get the following error:

    now i see my bracketing error thank you for reminding of the proper way to bracket if statements. now to figure out my logic error

    Code:
    ssma-imac:ENG-3211 ssma$ gcc -g hw_5_2.c -o hw_5_2hw_5_2.c: In function ‘SearchArray’:
    hw_5_2.c:81: error: expected expression before ‘else’
    from:

    Code:
    void SearchArray(int A[], int target, int size){
        int i;
        printf("The target value is %d\n", target);
    
    
        for (i = 0; i < size; i++)
        printf("\nThe array [%d] elements consists of the following %d\n", i , A[i]);
    
    
        for (i = 0; i < size; i++)
        {
            if ( target == A[i])
            {
                printf("Element %d contains the target %d\n\n", i+1, target);
            else 
                printf("Target %d was not found\n\n", target);
                break;
            }
        }
    }
    now that error to me is saying that i dont have any kind of function after my if and before my else. is not print f a function?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,850
    The error is due to incorrect syntax. My previous post contains an example of the correct syntax, though you need to move the break to the right place.
    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

  12. #12
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by laserlight View Post
    For the first part that I mentioned, yes, though I don't understand why you introduced all those unnecessary blank lines that were not in lieb's code. In my opinion, they make the code harder, not easier, to read.
    What, I did? Huh, I don't remember doing that... My IDE must've screwed it up.

  13. #13
    Registered User
    Join Date
    May 2013
    Posts
    66
    Quote Originally Posted by laserlight View Post
    The error is due to incorrect syntax. My previous post contains an example of the correct syntax, though you need to move the break to the right place.
    you are very patient with me, thank you much for all of the help.

    im making progress seeing little things at a time as im falling asleep at the keys

    i have it working now, but i wonder how to get the system to stop printing out the failed condition every time the for loop happens and only print if the for loop fails to locate the target:

    Code:
    ssma-imac:ENG-3211 ssma$ ./hw_5_2Please enter 10 integer numbers separated by spaces:
    1 2 3 4 5 6 7 8 9 10
    Please enter the value to search for: 7
    The target value is 7
    
    
    The array [0] elements consists of the following 1
    
    
    The array [1] elements consists of the following 2
    
    
    The array [2] elements consists of the following 3
    
    
    The array [3] elements consists of the following 4
    
    
    The array [4] elements consists of the following 5
    
    
    The array [5] elements consists of the following 6
    
    
    The array [6] elements consists of the following 7
    
    
    The array [7] elements consists of the following 8
    
    
    The array [8] elements consists of the following 9
    
    
    The array [9] elements consists of the following 10
    Target 7 was not found
    
    Target 7 was not found
    
    Target 7 was not found
    
    Target 7 was not found
    
    Target 7 was not found
    
    Target 7 was not found
    
    
    Element 6 contains the target 7
    i see why they are printing because each time through the for loop if the condition fails it is supposed to print that message. so i need it to only print that if it reaches the end of the array and fails to locate the target... hmmm

    that is the next step.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,850
    Instead of printing on each iteration of the loop, use a variable (a "flag") to record whether or not the target has been found. After the loop, check this flag to determine what to print.
    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

  15. #15
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by lleb View Post
    you are very patient with me, thank you much for all of the help.

    im making progress seeing little things at a time as im falling asleep at the keys

    i have it working now, but i wonder how to get the system to stop printing out the failed condition every time the for loop happens and only print if the for loop fails to locate the target:

    Code:
    ssma-imac:ENG-3211 ssma$ ./hw_5_2Please enter 10 integer numbers separated by spaces:
    1 2 3 4 5 6 7 8 9 10
    Please enter the value to search for: 7
    The target value is 7
    
    
    The array [0] elements consists of the following 1
    
    
    The array [1] elements consists of the following 2
    
    
    The array [2] elements consists of the following 3
    
    
    The array [3] elements consists of the following 4
    
    
    The array [4] elements consists of the following 5
    
    
    The array [5] elements consists of the following 6
    
    
    The array [6] elements consists of the following 7
    
    
    The array [7] elements consists of the following 8
    
    
    The array [8] elements consists of the following 9
    
    
    The array [9] elements consists of the following 10
    Target 7 was not found
    
    Target 7 was not found
    
    Target 7 was not found
    
    Target 7 was not found
    
    Target 7 was not found
    
    Target 7 was not found
    
    
    Element 6 contains the target 7
    i see why they are printing because each time through the for loop if the condition fails it is supposed to print that message. so i need it to only print that if it reaches the end of the array and fails to locate the target... hmmm

    that is the next step.
    That's easy,

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <math.h>
    #define SIZE 10
    
    
    void ReadArray(int A[], int size);
    void SearchArray(int A[], int target, int size);
    
    
    int main()
    {
    	int A[SIZE], target;
    	ReadArray(A, SIZE);
    	printf("Please enter the value to search for: ");
    	scanf("%d", &target);
    
    
    	SearchArray(A, target, SIZE);
    
    
    	return(0);
    }
    
    
    void ReadArray(int A[], int size)
    {
    	int i;
    	printf("Please enter %d integer numbers separated by spaces:\n", size);
    	for (i = 0; i < size; i++)
    		scanf("%d", &A[i]);
    }
    
    
    void SearchArray(int A[], int target, int size)
    {
    	int i;
    	for (i = 0; i < size; i++)
    	{
    		if ( target == A[i] )
    		{
    			printf("Element %d contains the target %d", i, target);
    			break;
    		}
    		else if ( i == size - 1 )
    		{
    			printf("Target %d was not found", target);
    			break;
    		}
    
    
    	}
    }
    All I did was add another break statement and test if i was equal to size minus one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot figure out this bug "error: conflicting types"
    By alter.ego in forum C Programming
    Replies: 3
    Last Post: 08-29-2012, 04:22 AM
  2. Replies: 4
    Last Post: 07-17-2012, 09:02 AM
  3. Please help! I get a "conflicting types for" error.
    By hotshotennis in forum C Programming
    Replies: 7
    Last Post: 02-24-2012, 07:31 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM