Thread: code error

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    21

    code error

    [tag]
    Code:
    int main(){int i;int*p;i=5;int &r=i;printf("%d",r);}
    [\tag]friends when i try to compile this code i get this errorerror: expected identifier or ‘(’ before ‘&’ token

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Not everything has to be on one line ._.

    And I'm assuming you have no idea how to use pointers, given that you're using
    Code:
    int &r=i;
    instead of
    Code:
    int *r = &i;

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It would be better if you formatted your code such that each statement is on a separate line, e.g.,
    Code:
    int main()
    {
        int i;
        int*p;
        i=5;
        int &r=i;
        printf("%d",r);
    }
    Now, observe this line:
    Code:
    int &r=i;
    This is invalid C syntax. It is valid C++ syntax, but C++ is not C.
    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
    Jan 2009
    Posts
    1,485
    Code:
    int &r = i;
    Is valid in C++ which has reference variables, not in C.

    And yes, format your code like a normal person.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    also, if you are not using a C99 compiler, you will get an error if you have a declaration after executable code.
    i=5;int &r=i; (even if &r was legal)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error in code
    By zeybek258388 in forum C Programming
    Replies: 3
    Last Post: 12-31-2011, 03:34 PM
  2. error in c code...
    By am_1989 in forum C Programming
    Replies: 12
    Last Post: 08-04-2011, 09:23 AM
  3. error in code
    By cole in forum C Programming
    Replies: 6
    Last Post: 09-26-2008, 09:51 PM
  4. i have an error in this code help?
    By ssjnamek in forum C++ Programming
    Replies: 12
    Last Post: 01-26-2002, 10:48 PM
  5. Need help with one error in my code
    By RJstuff1 in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2001, 04:13 AM