Another C programming problem

Discussion in 'Embedded Systems and Microcontrollers' started by oussama123443, Dec 2, 2014.

  1. oussama123443

    Thread Starter New Member

    Nov 2, 2014
    24
    0
    hi all i'm asked to make a function in c that returns the repeated digits when 5 values are entered by the user, i did the program but the compiler(Code Blocks in Mac) is giving me a warning saying that "Control end at non void function" , the program is assuming that i'm not returning a value to the function call . any ideas how to avoid that please and fix it ??? here is my program below:

    Code (Text):
    1.  
    2. #include <stdio.h>
    3.  
    4. int repeat(int fA, int fB, int fC, int fD, int fE);
    5.  
    6. int main (){
    7.  
    8.     int a, b, c, d, e;
    9.  
    10.     printf("Enter numbers: ");
    11.  
    12.     scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
    13.  
    14.     printf("\n%d\n", repeat(a, b ,c, d, e));
    15.  
    16.     return 0;
    17.  
    18. }
    19.  
    20. int repeat(fA, fB, fC, fD, fE) {
    21.  
    22.     int k,j, array[] = {fA, fB, fC, fD, fE};
    23.  
    24.     for(k = 0; k <= 3; ++k) {
    25.  
    26.         for( j = k + 1; j <= 4; ++j) {
    27.  
    28.             if(array[k] == array[j]) {
    29.  
    30.                 return array[k];
    31.  
    32.             }
    33.  
    34.         }
    35.  
    36.     }
    37.  
    38. }
    39.  
    Edited by mod: added code tags, changed variable i to variable k
     
    Last edited by a moderator: Dec 2, 2014
  2. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,360
    A thread belongs to the Thread Starter. Please do not hijack someone else's thread.
    Now you have your own.

    Also please use code tags to enclose program code.
     
  3. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,360
    You cannot return an array like that. Why not make the array global instead?
     
  4. NorthGuy

    Active Member

    Jun 28, 2014
    603
    121
    To avoid this error you need a "return" at the very end of the "repeat" function. The compiler must know what to return if all the comaprisons fails and you get out of the for loops.
     
  5. WBahn

    Moderator

    Mar 31, 2012
    17,737
    4,789
    The compiler is probably choking on the fact that your function declaration and your function definition do not match.

    Your function declaration (prototype) says:

    int repeat(int fA, int fB, int fC, int fD, int fE);

    which indicates that the function takes five arguments all of which are of type int.

    But your function definition fails to declare the type of the variables. Most compilers will throw an error on this.

    Also, note that using scanf() to get input is a REALLY bad thing to do. You should use fgets(). Some compilers also have safe versions of the many unsafe functions in the standard libraries.

    Your function needs to be written so that it ALWAYS returns a value of the appropriate type. What if your if() statement that has your return statement never executes? What does the function return then?
     
  6. WBahn

    Moderator

    Mar 31, 2012
    17,737
    4,789
    But he's not returning an array, he is either returning one element of the array or he is returning nothing at all.

    However, I'm not aware of any C compilers that can handle something like:

    int k,j, array[] = {fA, fB, fC, fD, fE};

    I believe this kind of array initialization must resolve to literal values at compile time. Otherwise, how can the compiler force the array pointer to point to a block of memory that has those variables in that order? In this particular case, that might be possible since the variables are on the stack and are probably in the same order, but what if the order was different between the two or some other parameter was in the argument list.
     
  7. Eric007

    Senior Member

    Aug 5, 2011
    1,044
    33
    Well I may be wrong but I think there is no problem with the matching of the function declaration and function definition. I think it is *good practice* but not necessary to repeat the type ('int' in this case) in the function definition.
    That would be a problem if He/She had put a different 'type' in the function definition.

    Again I might be wrong, also I haven't looked in depth in the code for other potential problem.
     
  8. WBahn

    Moderator

    Mar 31, 2012
    17,737
    4,789
    From my copy of the somewhat ancient C99 draft standard:

    The declarator in a function definition specifies the name of the function being defined
    and the identifiers of its parameters. If the declarator includes a parameter type list, the
    list also specifies the types of all the parameters; such a declarator also serves as a
    function prototype for later calls to the same function in the same translation unit.

    There is an allowance for this to be split and for there to be a separate declarator giving the types, but this is an obsolescent feature that is deprecated and may go away in a future version of the standard. But even so, this code does not provide that declaratory.
     
    JohnInTX likes this.
  9. JohnInTX

    Moderator

    Jun 26, 2012
    2,345
    1,025
    Some older compilers in the transitional period from original K&R to ANSI supported back-compatibility by enforcing type checking on functions whose parameters were declared in the function's parenthesis and not if declared below the function name.

    Code (Text):
    1. myfunc(x,y,z)
    2. {
    3. int x,y,z
    4. blah blah
    5. }// x,y,z not type checked
    6.  
    7.  
    8. myfunc(int x, int y, int z)
    9. {
    10. blah blah
    11. } // x,y,z WERE type checked
    In some discussions I read on C (Reagan was president..!) there were actually programmers that insisted that type-checking parameters was a bad thing - it inhibited the creative process - particularly the habit of using int as pointer. We don't hear much from them any more - my guess is that they are still debugging...
     
Loading...