Pointers in C

Last Updated Nov 20, 2015, 07:00:14 PM





Pointers are different from normal variables we use in programming like I said they are different, pointers are a different type of variables that can store or hold the address location of real variables. In simple words pointers store the memomy address location that is being used for variables.

The Wikipedia gives a clear sense about pointers as "a pointer is a programming object whose value refers to or points to another value stored elsewhere in the computer memory using its address"

If you notice the above pointers image you will notice some interesting facts about variable; There is a normal integer variable with value 34 and it has an address location of 1005 in the memory. As we know, this is normal behavior of how memory allocation is used, right?. So what pointers do in this scenario?. Well, in the above, there is a pointer variable which actually stored the address location of the normal variable value .ir.e 34. Also please note that pointer variable do have its own address location as well.

What is a pointer

Declaring Pointers

As we already know, variable declaration, pointer declaration may looks similar, however there is one differece that seperates normal variable and a pointer variable

pointers must be declared using astrisk( * ).

In the above declaration if you notice, we have declared pointer as integer, just like variables we can also delcare pointers of any data type such as int, char, float, double etc Pointer-name specifies the name of the pointer just like a variable name, but it must start with an astrisk( * ).

Example

Try It Now

In the above example, the variable is normal_variable, and the pointer variable is *ptr the normal_variable has some memory location which can be stored as &normal_variable so that location is stored in the pointer as ptr = &normal_variable

So Why Pointers are so Important?

There are significant advanctages of using pointers in c programming. Let us learn some of them

  • Pointers are very helpful to improve the performance for complex and repeatative operatiions
  • Pointers can also hold the address location of entry points such subroutines
  • Pointers allow to access the memory location of both protected and unprotected access to memory address

Dereferencing Pointers

As we already know, pointers to have a memory location, obtaining the specific value stored in that memory location is called as dereferencing



Char Pointer Example

Try It Now

Floating Pointer Example

Try It Now

Pointers with String Example

Try It Now

Other Pointer Topics you might want to learn

Pointers to Arrays in C
Pointers to Functions in C