About the complier

This is a discussion on About the complier within the C Programming forums, part of the General Programming Boards category; Rcently,i come across a problem,i wrote a code as following: Code: #include <stdio.h> //#include <stdlib.h> void ex(int &a,int &b) { ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    About the complier

    Rcently,i come across a problem,i wrote a code as following:
    Code:
    #include <stdio.h>
    //#include <stdlib.h>
    void ex(int &a,int &b)
    {
     int  t;
     t = a;
     a = b;
     b = t;
    }
    int main()
    {
     int a,b;
     a = 1;
     b = 2;
     printf("%d%d\n",a,b);
     ex(a,b);
     printf("%d%d\n",a,b);
        //printf("Hello world!\n");
        return 0;
    }
    but when i copy it into codeblocks and wDEV c++,then i rebuild it ,it could not success,but when i rebuild it in c-free,it works!~~nothing wrong.
    could someone tell me why?
    Thank you !

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What error messages did you get?

    In programming it's all about error messages. They tell you, well, what errors you make. (Which is a good thing, btw)

  3. #3
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Right. Are you assigning an address to an integer on purpose?
    Code:
    void ex(int &a,int &b)
    {
     int  t;
     t = a;    // Because you're doing that here.
     a = b;
     b = t;
    }

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,289
    Wrong, msh. The code is actually C++ (the arguments of ex() are references, sometimes known as aliases, not pointers). The assignments are not of pointers.

    The problem is probably related to errors in installation of the compilers or development environments, or settings in the particular project. As CommonTater said, the error messages will be key to identifying the problem.

    If the code is being compiled as C, it will not compile unless the compiler goes well beyond the C standard - C does not support reference arguments.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    /facepalms

    Thanks.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    the cobeblocks tell me:
    ||=== Exchg3, Debug ===|
    /media/installation/CodeBlocks/Examples/Exchg3/main.c|4|error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token|
    /media/installation/CodeBlocks/Examples/Exchg3/main.c||In function ‘main’:|
    /media/installation/CodeBlocks/Examples/Exchg3/main.c|17|warning: implicit declaration of function ‘ex’|
    ||=== Build finished: 1 errors, 1 warnings ===|

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,289
    Clearly you didn't bother to read - or at least put effort into understanding - previous posts.

    The first error message is a consequence of compiling C++ code as C. The & in the first two arguments of ex() are not valid in C, and the compiler expects to find something else - as the error message says.

    The second warning message is a side effect of the first error. Because of the first error, the compiler has no visibility of a function named ex() and, in C, an attempt to call an undeclared function implicitly declares it (the warning occurs because such implicit declaration of a function is deprecated - scheduled for removal from a future version of the C standard).

    The simplest fix is probably to rename main.c as main.cc (as .cc files are compiled as C++ by default). Alternatively, change project settings for main.c so it is compiled using g++ (the C++ compiler driver) rather than gcc (which will compile a .c file as C, not C++).
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Borland C++ Complier Error Missing cox32.obj
    By Bill Agnor in forum C++ Programming
    Replies: 1
    Last Post: 05-04-2010, 12:18 PM
  2. DOS C complier.
    By curious in forum C Programming
    Replies: 15
    Last Post: 06-22-2008, 11:50 PM
  3. Which complier?
    By sreenadh in forum C++ Programming
    Replies: 6
    Last Post: 03-18-2006, 01:56 AM
  4. complier with bios.h
    By neuflex in forum C Programming
    Replies: 6
    Last Post: 03-08-2005, 06:27 AM
  5. complier design
    By condorx in forum C Programming
    Replies: 1
    Last Post: 04-06-2002, 08:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21