Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Label used but not defined when using && operator [closed]

int main()
{
        int i = 0;
        int *p = &i;
        int *q = &&i;
        return 0;
}

When compiling this using gcc on Linux, I am getting the error

addr.c: In function ‘main’:
addr.c:6:2: error: label ‘i’ used but not defined

Why is the compiler treating int i as label and not integer? When do we use && operator?

EDIT: Okay, I can somewhat understand the answers, but can you explain the below macro definition from "arch/arm/include/asm/processor.h". It doesn't says anything about label, but the comment says, it can return the "program counter"

/*
 * Default implementation of macro that returns current
 * instruction pointer ("program counter").
 */
#define current_text_addr() ({ __label__ _l; _l: &&_l;})
like image 343
manav m-n Avatar asked Nov 28 '22 14:11

manav m-n


2 Answers

What && operator? There is no unary && operator in C++. GCC has an extension that allows computed goto statements, and that extension uses && to get the address of a label.

like image 78
R. Martinho Fernandes Avatar answered Dec 05 '22 01:12

R. Martinho Fernandes


Here && is the GNU C label address operator.

http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html

int *q = &&i;

i must be a label. You have no label i in your program.

Example of a label:

int main(void)
{
    i:
    (void) 0;

     int i = 0;
     int *p = &i;
     int *q = &&i;
     return 0;
}

I added the (void) 0; statement as labels in C can only put before statements and not before declarations.

like image 35
ouah Avatar answered Dec 05 '22 02:12

ouah