ptr = &x;
Let us think about where these value variables and the pointer variables are located in memory. Some times in our school days we may image that a location in memory is marked as x and a value 5 is tored in it. Yeh .. now we know that it the Symbol Table that tackles the issue. That is the compiler creates a data structure called symbol table and in it the variables/symbol names along with the addess of the memory location is maintained. For a pointer variable the size is usally 4 bytes. And here the memory location assigned for ptr will hold the address of memory location assigned for the value variable x.The pointer arithmetic is dependent on the data type of the pointer.
As we use the expression ptr = &x; … the address of value variable ‘x’ is extracted from symbol table and stored in ptr. Thus ptr holds the address of x, and we can access the content of ‘x’ using ptr. Eg: printf(“%d”,*x);
Pointer to an array
arr = x;
Yeh .. true because compiler considers array as pointer. What happens when we say like … arr++. The content of arr is incremented obviously and in this case it is incremented by the size of data type i.e 2 for 16 bit O.S.
Array of pointer is generally used to hold a bunch of addresses that has some logical relation.
Pointer to structure can be used to allocate a block that contains sub elements and the block can be released at will.
struct Node *n;
The pointer variable ‘n’ initally occupies only 4 bytes and the block corresponding to structure is allocated only when the programmer applies memory allocation function.
Eg: n = (struct Node*)malloc(sizeof(struct Node));
After use we can release at will by using free(n);
Thus this helps in dynamic memory management which yields better efficiency.
Pointer to a charecter
Pointer can be used to point to a charecter sequence. Eg: char *msg ;
msg = (char *)malloc(20);
In this case msg points to the first byte or the zeroth element of the block of memory. By using msg++, we can point to the next successive byte and so on.
*(msg)++ in this case would increment the content of the memory location that it is pointing to. For example if it is pointing to ‘e’ then the value is incremented to ‘f’.
character pointer to pointer is usually y used for referring an array of string or something like a two dimensional character array.
Eg: char **words = new char;
( I have used C++ style of allocation to mention its resemblance to two dimensional character arrays)