Reputation: 381
I was experimenting the behaviors of pointer in C. I wrote the following program:
#include <stdio.h>
main(){
int a = 1;
printf("%p\n", &&a);
}
I knew that this program would give error. Because an address of address would be ambiguous and this would become extremely recursive. But when I tried to compile the program, it gave an error which I couldn't understand.
error: label ‘a’ used but not defined
printf("%p\n", &&a);
What does it mean by label ‘a’ used but not defined
?
Upvotes: 3
Views: 449
Reputation: 6465
&&
is real gcc
extension, but not for variables. Its for labels.
&&a
is used to get the address of a label defined in the current function.
In your case compiler deduced that a
, followed by &&
operator, is label
, but you haven't any label
a
defined in your code, so that is the error.
Upvotes: 6
Reputation: 68013
it quite logical the address is not something which exists somewhere physical in the memory - so it cant have the address itself. That is the reason the &&
operator could be used for other purposes.
if you try
int x ;
printf("%d\n", &x); // valid
printf("%d\n", &(&x)); // error lvalue required as the address is not the object
Upvotes: 1
Reputation: 1
This isn't really a direct answer to your question. It's more for anyone reading wondering why you can't take an address of an address when such things as char **ptr;
can be defined.
An address doesn't have an address. Only actual variables in memory have an address. You can put an address in a pointer variable (as noted in other answers here), and then the pointer variable will have an address, but the address value held in the pointer doesn't inherently have its own address.
An address is effectively "where something is". Trying to find the address of an address is like trying to ask "Where is where is my car?"
So char **ptr;
isn't the address of an address, it's a variable that contains the address of a pointer variable.
Upvotes: 4
Reputation: 988
& is just to get the adresse of the memory box try to use pointers .
https://www.tutorialspoint.com/cprogramming/c_pointers.htm
Upvotes: 0
Reputation: 873
You are not allowed to take the address of an rvalue-temporary returned by the address operator. && is a gcc-extension: https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html
Upvotes: 2