I am doing C programming from a couple of years ago and finally I convinced myself and grabbed all my guts to write an article on Pointers. Now, one could easily question Why? Basically, Pointers are the most useful part of C and usually the most difficult to understand. Note: I don't want to discourage my readers, that learning about pointers can be a difficult but that's what most of the programmers say and according to me its the most exciting part of C Programming. For the scope of this tutorial we'll only be looking at how to declare a pointer? , and the most basic pointer operators. What is a Pointer A pointer is a variable that contains the address in memory of another variable. We can have pointers for any data type in C, for example a long pointer , int pointer, char pointer etc. We also have a void pointer which does not have a type, thus it can be used to point to any data-type or even any kind of data, for the scope this tutorial we'll be not looking into any operations on void pointers. Void pointers are a special type of pointers in C and they need to be used in a different way which is out of the scope of this tutorial. Operators The unary operator '&' gives the adress of a variable. Demonstration :- Code: #include<stdio.h> int main() { int x = 0; printf("Address of x: %p\n", &x); } Output :- Code: Address of x: 0xbf912a5c Which would obviously give out a different address on different executions (Due to ASLR). The dereference operator i.e * gives the contents of the object pointed by a pointer. Declaring a pointer :- To declare a pointer we follow the following format :- Code: [TYPE] *[NAME] of course that's not a strict format and there are a number of ways to declare a pointer but for the scope of this tutorial, we'll be sticking to this one. example :- Code: int *ptr The above code declares an int pointer named “ptr”. Note: Same as any C variable pointer also points to a garbage value (i.e address in case of pointers), and should always be initialized before using. Using uninitialized pointers can cause reading memory from an arbitrary address which will eventually result in a Segmentation fault i.e Invalid Reads or Writes depending on the usage. Working with Pointers example.c Code: #include<stdio.h> int main() { int *ptr; int x = 0; ptr = &x; printf("x : %d\n", *ptr); } Note: We must associate the pointer to its same type, i.e A long pointer should be associated with a long variable, and not with any other data type. Consider a set of strict boxes, in which only a specific type of item can be contained, for instance a square box cannot hold a rectangular object. Similar is the case with the pointers. (Again I am not taking Void pointers into consideration as of this tutorial.) Before reading the explanation below or before compiling, try to figure out what should be the output of the above program. Output :- Code: x : 0 Explanation :- 5th line: Here we declare an int pointer named ptr. 6th line: Here we declare an int variable x and initialise it to 0. 7th line: Here we use the unary operator '&' to get the address of x and store it in ptr. Now the pointer ptr contains the address of x, now the value of x can be accessed using the dereferencing operator. 9th line: Here we use the derefrencing operator which gives the value of the variable pointed by our pointer i.e x. Now that might be a little difficult to grasp at first but believe me its easier than it looks. Exercises :- In this tutorial we were inroduced to basic usage of pointers in C, now I'd like the viewers to write some basic programs to test their knowledge. Write a program to print the value of a long variable using pointers. Write a program to print the value of a char variable using pointers.
Me too get every time confused with pointers. Thats good at-least after long time you successfully understand about pointers. Theoretically they can be understood easily but at time of practical I became dumb.
I feel happy to be here again, well as I know pointer is nothing but a variable which contain the address of others variables.
I am not sure about the task, but i can try. If you find any mistake, please let me know with explanation. Code: #include<stdio.h> void main() { long *ptr; //declaration of pointer variable int x=0; //declaration and initialization of a variable ptr=&x; printf("The value of x is:" *ptr); }