Introduction to Pointers

Submitted by: 

Introduction to Pointers

In this part you will learn:
1. C syntax
2. Pointers
3. Pointers declaration
4. Showing output

What is a Pointer?
Whenever we declare variable our computer stores it in at a memory location in the memory. A unique address is assigned to every variable made by the user either its on run time or compile time. We use pointers to access those address whenever we want to and use the value that is stored on that particular address.
Pointer is a special kind of variable. Pointers are designed to store the memory address i.e. the address of another variable. Declaring a pointer is same as declaring any variable the difference is that between the type of variable and the name of the variable we place a asterisk ‘*’. The asterisk can be written either with the type or the name of the variable. Like this.
int* p or int *p (they both are the same).
When we write this statement this means that we declared a integer type pointer that will point to an integer type variable.
In C, especially when we are talking about pointer we call this symbol as dereference operator. Because when write this operator with any pointer that means that we are accessing the value of the address that is stored in that certain pointer.
When studying pointer you should also know about another symbol called ampersand ‘&’. You would have seen and used this symbol when you write the scanf() function. This symbol is used to access the address of any variable.

Swapping With Pointers
Open Dev C++ then File > new > source file and start writing the code below.

  1. #include<stdio.h>
  2. #include<conio.h>

These two are the most common and basic header files. The first one stdio.h is used to grant us most of the basic functions of C like Input and Output functions. The second one conio.h provides us functions like getch() which is used to pause our screen until we press a button.

  1. nt main()
  2. {
  5. int a,b;
  7. printf("Enter the value of a:");
  8. scanf("%d",&a);
  10. printf("Enter the value of b:");
  11. scanf("%d",&b);
  13. int *p=&a,*q=&b;
  15. printf("Original values:");
  16. printf("\na=%d\nb=%d",*p,*q);

Now in this program we are swapping the values of two integers that were entered by the user. We are doing this with the help of variables. Firstly we declared two variables of integer type and took input from the user. We stored the input in a and b respectively.
Then we declared the two pointers of integer type and gave them the address of a and b respectively. We did this using the ampersand sign. This assigns the address of the memory location where the value of a and b are stored to the pointers.
Then we print the value of a and b through p and q by using the dereference operator. The meaning of writing asterisk sign with pointer name is value at that address.

  1. p=&b;
  2. q=&a;
  4. printf("\nswapped values:");
  5. printf("\na=%d\nb=%d",*p,*q);
  8. getch();

This is the part where we swap the value of a and b. We did this by assigning the pointers p and q opposite addresses. Means that first we assigned pointer p the address of ‘a’ and q the address of ‘b’. Now we assigned p the address of ‘b’ and q the address of ‘a’. Now our pointer p is pointing at the value that was stored in ‘b’ and q is pointing to a value that was stored in ‘a’
At last when we printed the value using dereference operator we got the swapped values of ‘a’ and ‘b’.
Execute > compile
then Execute > run

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th>
  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.