C Pointers basics explained with examples

    1 Votes

One of the interesting features that most C programmers enjoy and hates is pointers. Some C programmers enjoy it because it gives immense control over operations in programming. Reason for hating it is due to the fact that it can cause massive errors in program. If you can learn to handle this wisely, you will be a really good C programmer.

Pointers in C

Pointers are often used to make C programming much easier and it helps programmers improve their program's efficiency. Not only that, by using the pointer's you can achieve unlimited amount of memory usage in your program, that’s cool right.

Definitions and Uses of Pointers

Pointers are the variables that hold an address of another variable of the same data type. The cool thing about the address of a variable is that, you'll then be able to go to that address and access the data stored in it. Pointers are basically used in the C in different ways. Some of them are:

  • To create dynamic and flexible data structures.
  • To pass and handle variable arguments passed to functions (Call by reference).
  • To retrieve information stored in list like arrays.

Pointer Variables

As I said earlier, pointers indicate the memory address of any kind of object i.e. It can be address of any primitive data types or structures or even functions. The variable which holds the address is called the pointer variable.

Typical Pointer Declarations

Let’s see how we can declare a pointer in C. Generally say integer variable “var” is declared like below.

int var;

In case of pointer for example, say integer type of pointer the declaration is like this:

int *var;

In general the declaration is like

<type> *<variable name>;

That’s why it is recommended that you should always initialize a pointer value with NULL like this.

int *var = NULL;

You have noticed that "*" operator is used at declaration to denote a variable to a pointer type. Another operator "&" is used to represent the address of a variable.

int i = 10;
int *p = &i;

When you declare a variable, a memory is allocated for that variable. For the above example, say the variable "i" has address 0x2000 and the address contains value 10, now when the "p" variable is declared, it also gets a memory address say 0x2100, because we have declared that it will contain address of I, so it will contain the value of 0x2000.

0x2000 ( i ) 0x2100 ( p )

Also Read - Top C Interview Questions and Answers for Freshers & Advanced C Interview Questions and Answers

Dereferencing Pointers

As you seen earlier, "*" operator is used to declare the pointer type of the variable. "*" is also used to dereference the pointer also.

int j = *p;

It sets the value of "j" variable to the value contained at the memory address, the pointer p points to i.e. 10. You can also set a value to the memory address that a pointer holds like below.

*p = 20;

By this, the memory address that "p" pointer holds i.e. 0x2000 contains value 20 which is same as the value of i and *j, interesting right! That’s the beauty of the pointer, as you change the value that contains an address of a variable; it also changes the variable itself.

Also Read - Top Embedded C Interview Questions and Answers & C++ Interview Questions and Answers

Void Pointers 

You must have noticed that a pointer always holds an address of the same data type, like an integer, pointer always holds the address of integer type of variable, float of a float, double of a double etc.
But there is a special type of pointer called void pointer also known as generic pointer. This type of pointer can hold the memory address of any kind of data type or any object even functions too! This is declared like below:

void *void_ptr = &i;

But the only thing you need to remember is that you can’t dereference a pointer directly. You have to typecast it before you use it. Like if you want to print the value of the above declared void pointer, the memory address it contains point to is:

printf(“%d”,*(int *)void_ptr);

This is very useful when you want a pointer to point to data of different data types at different times.

Pointer Arithmetic

You are well aware about arithmetic of other variable like int, float, double etc. and you must think about whether those can be applied to a pointer variable or not. No, you can’t apply arithmetic operation like addition, subtraction, multiplications etc. between two pointer variables. The operations given below give you a Compiler Error.

int i=10;
int j=20;
int *pi = &i, *pj = &j;
int *res = *pi + *pj /* Compilation Error */
res = *pi - *pj /* Compilation Error */
res = *pi * *pj /* Compilation Error */

Only thing you can do is to increment and decrement operation to a pointer variable. This is mostly used by C programmers when dealing with arrays or strings.

char s[] = "a4academics.com";
char* ptr = s;
printf(“%s\n”,ptr);
printf(“%c\n”,*ptr);
printf(“%c\n”,*(ptr + 3));
ptr+=5;
printf(“%c\n”,ptr);
ptr--;
printf(“%c\n”,ptr);
The above gives following output:
a4academics.com
a
c
d
a

Related Articles

C Overview and Basic Syntax
Data Types in C Language
C Qualifiers - Constant and Volatile type Qualifier
C Type Casting with examples - float to int / int to char / int to long
C Constants and Literals
C Operators - Arithmetic, Assignment, Relational and Logical
C Operators - Bitwise Operators and Conditional Operators
C Operator Precedence and Associativity Table with Examples
C Input and Output Statements
C Format Specifiers for printf and scanf
C Decision Making Control Structures - 'if', 'if - else', nested 'if', 'switch'
Loops in C - for, while and do...while Loops
C 'break', 'continue' and 'goto' Statements to Control Program Flow
C Preprocessor Directives and Macros
C Functions Tutorial with Examples
C Arrays - Dynamic, 2d and Multidimensional with Examples
C Strings - Internal Working and Functions with example
Structures in C Programming with Example
Unions in c programming with example
C Storage Class Specifiers - auto, register, static and extern

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know