Monday, April 4, 2016

What is pointer?

The pointer is unfamous for its difficulty to understand. The pointer is used to perform dynamic memory allocation in C for example.

To understand what the pointer is, you need to know what the variable is; the variable is a memory location, and the memory location has its address.

#include <stdio.h>

int main () {

   int  var1;
   char var2[10];

   printf("Address of var1 variable: %x\n", &var1  );
   printf("Address of var2 variable: %x\n", &var2  );

   return 0;
}
This is cited from tutorialpoint: http://www.tutorialspoint.com/cprogramming/c_pointers.htm

The memory location's adress can be accessed by an operator "&". We can see the variables has the operator "&" in the left side.

By exercuting this code, we get the following result:
Address of var1 variable: bff5a400
Address of var2 variable: bff5a3f6
This is cited from tutorialpoint: http://www.tutorialspoint.com/cprogramming/c_pointers.htm


These things "bfff5a400" and "bff5a3f6" are the address of the variables in the memory.

Then, what is the pointer? The pointer is a variable, but its value is an address of other variable, i.e., direct address of a memory location.

The general form of a pointer declaration form is:
type *var-name;
cited from tutirialpoint: http://www.tutorialspoint.com/cprogramming/c_pointers.htm