C Tutorial The & operator

Data types in C
Arrays in C
Pointers in C
Pointer Dereferencing
The & Operator

NULL
Uninitialized Pointers
Using Pointers
C Strings
String Code Example
Large Strings
Char type pointers
TypeDef



 
 
 

 

HOME
 

The & operator is one of the ways that pointers are set to point to things. The & operator

computes a pointer to the argument to its right. The argument can be any variable which

takes up space in the stack or heap (known as an "LValue" technically). So &i and

&(f1->numerator) are ok, but &6 is not. Use & when you have some memory, and

you want a pointer to that memory.

void foo() {

int* p;  // p is a pointer to an integer

int i;   // i is an integer

 

p = &i;  // Set p to point to i

*p = 13; // Change what p points to -- in this case i -- to 13

 

// At this point i is 13. So is *p. In fact *p is i.

}

When using a pointer to an object created with &, it is important to only use the pointer so long as the object exists. A local variable exists only as long as the function where it is declared is still executing (we'll see functions shortly). In the above example, i exists

only as long as foo() is executing. Therefore any pointers which were initialized with

&i are valid only as long as foo() is executing. This "lifetime" constraint of local

memory is standard in many languages, and is something you need to take into account

when using the & operator.

 

Want To Know more with

Video ???