0

plz solve ...

int main()
{
    const int m=10;
    int &n=m;
    n=11;
    printf("%d%d",m,n);
}

can w declare &n...its not an error...

Edited by Narue: Added code tags

4
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by himgar
0

Are you using a C compiler, or a C++ compiler?

In C++, &n would be a reference to n. I'm unsure how a C++ compiler would handle that reference, in a declaration.

In C, a declaration of &n is an error. These are the errors I get from my Pelles C compiler:

C:\Users\adak\Documents\Pelles C Projects\sortingX\sortingX.c(11): error #2014: Empty declaration.
C:\Users\adak\Documents\Pelles C Projects\sortingX\sortingX.c(11): error #2001: Syntax error: expected ';' but found '&'.
C:\Users\adak\Documents\Pelles C Projects\sortingX\sortingX.c(11): error #2048: Undeclared identifier 'n'.
C:\Users\adak\Documents\Pelles C Projects\sortingX\sortingX.c(11): error #2168: Operands of '=' have incompatible types 'int *' and 'int'.
C:\Users\adak\Documents\Pelles C Projects\sortingX\sortingX.c(11): error #2088: Lvalue required.
*** Error code: 1 ***

Edited by Adak: n/a

0

I compile this code on gcc compiler and get this error

error: lvalue required as left operand of assignment

When you use & before a variable you are pointing to the variable address not the content of that so when you declare &n =m it seems meaningless because you are saying that the address of the n should be the same as the content of the m that maybe change rapidly.

0

&n is a reference variable. We can't use ref variables in C. However, it can be used in C++.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.