slider

Pointers in C Programming || Basic Tuto

Hi Guys… Welcome you all to the Digital Chalkie Educational Blog. After a while, I came with an article to our ” C Programming Tutorial Series”(CLICK HERE).  If you haven’t liked our Facebook Page yet, please like it to support us. (FB Page: BMaxx Edu).  Today we are going to discuss Pointers in C Programming Language.

Pointers in C are easy and fun to learn. We can do some programming tasks easily with pointers. If you want to be a good programmer in C, you must learn about this.

What are the pointers in C?

Pointers/Pointer Variables are a kind of special variables that used to store memory addresses rather than values.

In other words, we can say that value of a pointer variable is the ‘ address of another variable’.  If you can’t understand these phrases clearly, just leave it. You will be understanding about these phrases when you are going through the article

Pointer Syntax

As other variables and constants, you must declare the pointers before using them. We use asterisk mark (*) with the variable name to declare a pointer variable

data_type  *VaribleName ;

int    *intPointer;     /* pointer to an integer */
double *doublePointer;  /* pointer to a double */
float  *floatPointer;   /* pointer to a float */
char   *charPOinter     /* pointer to a character */

Assign Address to Pointers

To assign an address to a pointer variable we use “&” mark with the name of the relevant variable name.   We can display it with the format below.

pointerName = &variableName;

int varA, *pointerA;
varA=10;
pointerA = &varA;

Here, 10 is assigned to the “varAvariable. And, the address of varA is assigned to the pointerA pointer

Use the values of Variables  pointed by Pointers

To get the value of the thing pointed by the pointers, we use the * operator. For example:

int varA, *pointerA; 
varA=10; 
pointerA = &varA;
printf("Value assigned by pointer= %d", *pointerA);
                  // OUTPUT = 10

Here the address of  ‘varA’  has assigned to the ‘pointerA’ as its value. By using ” *pointA ” we can get the value stored in that address. If you missed to use the “ * ” mark, you will get a garbage value. Practically it is the memory address of  ‘varA‘ variable.

For example :

  int varA, *pointA;
    varA=10;
    pointA=&varA;
    printf(" Output of pointA  = %d",pointA);
    printf("\n Output of *pointA = %d ",*pointA);

pointers
– Output of the above code segment –

Change the values of Variables  pointed by Pointers

To change or update the value of the variables pointed by the pointers, we use the * operator. For example:

int varA, *pointerA; 
varA=10; 
pointerA = &varA;
printf("Value = %d", *pointerA); //OUTPUT = 10
*pointerA = 23
printf("Value = %d", *pointerA); //OUTPUT = 23

NOTE: If you use as  “pointerA=23” it will give an error when you build the program. It happened because pointerA is dealing with memory addresses. When we work with values we have to use “*pointerA”

KEEP IN MIND

    • pointerA   – this is the name of pointer variable
    •  *pointer    – this is the pointer we use to work

Common Mistakes when using pointers

Following two errors are the very common mistakes programmers do while using pointers in C programming.

int varA, *pA;
pc = c; // Error
// pA is address but c is not

*pc = &c; // Error
// &c is address but *pc is not

Example to Work with Pointers

 

Sample Programme

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int *pointB, varB;   //(1)

   printf("Address of  varB as int: %d\n", &varB); //Address of varB
   printf("Address of *pointB as int: %d\n\n", &pointB); // 22

   varB = 20;   //(2)
   printf("Address of varB: %d\n", &varB);
   printf("Value of varB: %d\n\n", varB);  // 20

   printf("Value of  pointB: %d\n", pointB); //Address of varB
   /*printf("Value of *pointB: %d\n\n", *pointB);*/
   // You can't build program with this line. So we comment it

   pointB = &varB;   //(3)
   printf("Value of  pointB: %d\n", pointB); //Address of varB
   printf("Value of *pointB: %d\n\n", *pointB); // 22

   varB = 15;  //(4)
   printf("Value of  pointB: %d\n", pointB); //Address of varB
   printf("Value of *pointB: %d\n\n", *pointB); // 15

   *pointB = 10;  //(5)
   printf("Address of varB: %d\n", &varB);
   printf("Value of varB: %d\n\n",varB); // 2

   return 5;
}

// Use %x or %p to print the memory addresses as a good programing practise

Explanation Diagram

Explanation to Proagram

 

Guy its time to wind up. This is end of this article and i think you must get a clear idea about Pointers in C programming. Wish you all the best. PLease leave your opinion as a comment in  below. HAVE A GOOD DAY.. STAY SAFE…

Bonomi Tharinda
Bonomi Tharinda is the Developer of the BMaxx Blog. He is a skilled web developer and graphic designer. In addition, he is an undergraduate student of a representative University in Sri Lanka. He is working as a tutor of an institute and as a part-time blogger, he will help you all to make your problems solved.Here are his social accounts and you can follow him through them.