Basics of Pointers In C Programming Language


In today’s video tutorial lets learn basics of pointers in C programming language. Think of it as base of a building. If you can hold these basic concepts strong your building will be safer.

Topics Covered

1. What are pointers?
2. How to declare pointers.
3. How to access the value present at the address stored in pointer variable.
4. Using Address of operator.
5. Using Indirection operator.

What are pointers?

A pointer is a variable which refers to or points to an address in your computers memory.

Definition: A pointer variable is a variable which holds the address of another variable.

And since pointer variable is also a variable, it also has a unique address associated with it.

Operators

& is called “address of” operator.
* is called Indirection or “value at address” operator.

Video Tutorial: Basics of Pointers In C Programming Language


[youtube https://www.youtube.com/watch?v=1R6dBm3LN68]

YouTube Link: https://www.youtube.com/watch?v=1R6dBm3LN68 [Watch the Video In Full Screen.]


Source Code: Basics of Pointers In C Programming Language

#include<stdio.h>

int main()
{
    int num = 5;
    int *ptr; // pointer variable

    ptr = &num; // both hold address of num

    printf("Value of num = %d\n", num);
    printf("Address of num = %d\n", &num);
    printf("Address of ptr = %d\n", &ptr);
    printf("Value of ptr = %d\n", ptr);
    printf("Access value of num from ptr = %d\n", *ptr);
    printf("Access value of num from num = %d\n", *(&num));

    return 0;
}

Output:
Value of num = 5
Address of num = 6356732
Address of ptr = 6356728
Value of ptr = 6356732
Access value of num from ptr = 5
Access value of num from num = 5

Note: Whenever you declare and initialize a variable, for example, int num = 5; Your program does the following tasks:
1. Reserves space in your computers memory depending upon the data type.
2. Associates the name num with this memory location.
3. Stores the number 5 in this memory location.

Declaration of pointer variable

General Syntax
data_type* pointer_variable_name;

OR

data_type *pointer_variable_name;

Example:

int *ptr1;

int* ptr2;

Both these declaration are valid.

Rules for constructing pointer variable are same as that of regular variables: Rules for Constructing Variable Names: C

Assigning value to pointer variable

General Syntax
pointer_variable_name = &another_variable;

Example:

ptr = &num;

This stores the address of variable num as value of pointer variable ptr.

Declaring and Assigning value to pointer variable

Example:

int num = 5;
int *ptr;  // Declaration of pointer variable

ptr = &num;  // Assigning value to pointer variable

Note: While assigning address to pointer variable do not include * in front of pointer variable.

Values and addresses

num will have 5.
&num will have address of num.
&ptr will have address of ptr.
ptr will have address of num.
*ptr will have value of num.

Call by Value and Call by reference

Call by Value Example: C Program To Swap Two Numbers using Function
Call by Reference Example: C Program To Swap Two Numbers using Pointers

Note: Address can’t be negative values. So we can use %u as format specifier, which means unsigned integer.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Leave a Reply

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