C Programming Pointer

C Programming Pointer

C Programming Pointer
In this chapter you will learn about C  programming pointers; What are pointers, how to use pointers and common mistakes to face while working with pointers.

Table of Contents

C programming pointers

The strong feature of C and C++ programming is Pointer through which it is more prominent than other popular  programming languages ​​like Python and Java.

Pointers are used in C programming to access memory and manipulate memory addresses.

C memory address

Before taking up the concept of pointers let’s get familiar with C programming address.

If your program has a count variable, &countit will give you the memory address. Here &k is called the reference operator.

You must see this symbol when you scanf()use the function. It is used to store the input value to the address of the variable by the user.

				
					/*This program is for better understanding of reference operator in C programming. */
#include <stdio.h>
int main(){
 int count=5;
 printf("Value: %d\n",count);
 printf("Address: %u",&count); // Notice that ampersand(&) is used before the count variable.
 return 0;
}
				
			

output

				
					Value: 5
Address: 2686778
				
			

Note: Using this same code you can get different values ​​of address.

In the above source code the value 5 is stored at location 2686778 in memory. count is only the name of that location.

C Pointer variable

In C programming, there is a type of special variable that stores the address of another variable. This is called a pointer variable or just a pointer.

Pointer declaration

				
					data_type* pointer_variable_name;
int * p;
				
			

In the above statement, p is intdeclared as a pointer variable of type .

C reference(&) and dereference(*) operators

As mentioned earlier, & is called the reference operator. Through this you will get the address of the variable.

Similarly, there is another operator with which you can get a value from an address. This is called the dereference(*) operator.

The following example clearly explains the use of pointers, reference operators and dereference operators.

Note:* Symbol used in pointer declaration is not dereference operator. It is just a symbol for pointer instruction.

Example: C program to learn pointer usage

				
					#include <stdio.h>
int main(){
   int* pc;
   int c;
   c=22;
   printf("Address of c:%u\n",&c);
   printf("Value of c:%d\n\n",c);
   pc=&c;
   printf("Address of pointer pc:%u\n",pc);
   printf("Content of pointer pc:%d\n\n",*pc);
   c=11;
   printf("Address of pointer pc:%u\n",pc);
   printf("Content of pointer pc:%d\n\n",*pc);
   *pc=2;
   printf("Address of c:%u\n",&c);
   printf("Value of c:%d\n\n",c);
   return 0;
}
				
			

output

				
					Address of c: 2686784
Value of c: 22

Address of pointer pc: 2686784
Content of pointer pc: 22

Address of pointer pc: 2686784
Content of pointer pc: 11

Address of c: 2686784
Value of c: 2
				
			

Explanation of above program and diagram

Common Mistakes When Using Pointers

Suppose you want pointer pc to point to the address of c. then

				
					int c, *pc;
pc = c; /* Error! where pc is the address but c is not the address. */
*pc=&c; /* Error! Where *pc is the value pointed to by address and &c is the address. */
pc=&c; /* Correct! Since both pc and &c are addresses. */
*pc=c; /* Correct! Since *pc is the value indicated by the address and c is the value. */
				
			

In both cases the pointer pc does not point to the address of c.

2 thoughts on “C Programming Pointer”

  1. Pingback: C Call by Reference: Using pointers -

  2. Pingback: C Dynamic Memory Allocation -

Leave a Comment

Your email address will not be published. Required fields are marked *