From the course: Advanced C Programming: Optimize Performance and Efficiency

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

When to use the ampersand operator

When to use the ampersand operator

- [Tutor] If you really want to get down and dirty with pointers in C, you need to embrace two unary operators, the ampersand and the asterisk. This movie covers the ampersand. A later movie covers the asterisk. Also of the two, the ampersand causes less trouble although it can be confusing. The ampersand is the address of operator. It returns the memory location of a variable and that's the only way it's used, prefixed to a variable like the engine on a train. The variable doesn't even have to be initialized, just declared. Open exercise 05-01_ampersand1. This code declares four variables, a, b, c and d each of a different type. When a variable is declared it's given a location in memory. To fetch that address, you use the ampersand operator. The printf statement displays those addresses. The %p placeholder is used and the ampersand fetches the address. Build and run this code. The addresses you see on…

Contents