What is a pointer in C

This is an explanation of pointers for beginners. It seems to be a hard-to-get concept and I hoped this would make it more understandable.

What is a pointer?

It’s short for pointer variable. An int variable, holds an int, a double variable holds a double but a pointer variable actually holds an address. If you are running on a 32-bit operating system, the address will be 32 bits, if on a 64-bit operating system, the address will be 64-bits.

So an int variable will typically occupy 32-bits (= 4 bytes), a double will occupy 64-bit (=8 bytes) and the pointer variable will be either four bytes in size or eight depending on the bitness of the Operating system.

So our pointer variable holds an address. What does that mean?

All variables in a program are held somewhere in RAM when the program starts. When you compile a program, the linker puts all the bits together and leaves information in the executable about how much RAM the data variables need. When it is loaded into memory the operating system knows how much RAM to allocate for the variables. A variable address is just the location of a variable in RAM.

How do you get the address of a variable?

C includes the & operator. So if you have a variable a then &a has a’s address.

 

 

(Visited 242 times, 1 visits today)