Categories: pointer

Pointers in C programming Language

Pointers in C programming Language

We will discuss Pointers in C programming language

Pointer variable in C

  • The pointer variable is one of the features of C and C++ language
  • It is one of the most fundamental and important concepts; similar to an array in C and C++ language, but it is not an array.
  • Pointers are variable like other variables in C. They can store pieces of data.
  • Normal variables store values such as an int, a double . a char.
  • A pointer stores a memory address similar to an array. An array is static and the pointer is dynamic. Dynamically means we can increase or decrease memory allocation to your need.
  • So pointer is a special type of variable that contains the address of another variable.

What is a memory unit or address in C?

As we know, a memory unit is a collection of storage cells which can store any type of information.

Each memory cell has a unique value to identify each cell. It’s called address.

If a memory can store n- information, its address ranges from 0 to n-1. That is like an array index.

 

Find address of memory unit

In the program we will demonsrate how to find address   of memory unit in C language

Program

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

int main()
{
    int marks=67;//it is declare and initialized a variable as marks
    printf("Value of marks: %d\n",marks);
    printf("Address of marks: %u\n",&marks);
    getch();
    return 0;
}

Output

Value of marks: 67

Address of marks:2686748

In the above output, marks 67 is a value that is stored in memory location (address) 2686748.

 

declare and initialize pointers

All pointers variable must be declared before it is used in the program, like other variables in C.
The general form is,

var – it is a variable on your program in C language

&var – it is an address of your variable in the memory location

*var – this is a pointer of your memory location

 

Declaration of pointer

We can declare a pointer in three methods

Syntax

data_type* var;      // Declare a pointer variable called var as  pointer of type

data_type *var;

data_type * var;

 

– data types are meaning, such as int, float, double and more

* (sign) – to identify the variable as a pointer

*ptr – this is a pointer variable

var– this is a normal variable

Pointer variable in C

Here, ptr is a pointer which holds the address of variable var 

Example :

                  int *a;  // Declare a pointer variable called  as  pointing to an int

                                // it contains an address holds an int value

                                                 or


                 char* a;  // Declare a pointer variable called a as  pointing to a char

                                   // it contains an address holds a char value


                                                   or 


                   double * a;  // Declare a pointer variable called a as  pointer  a double

                                        // it contain an address holds a double value

Program 1

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

int main()
{
    int a=24;
    int b=56;
    float c=34.65;
    printf("a value is: %d\n",a);//display value of a
    printf("b value is: %d\n",b);//display value of b
    printf("c value is: %.2f\n",c);//display value of c
    printf(".....................\n");
    printf("address of value a is: %u\n",&a);
    //displays address in memory location of a value
    printf("address of value b is: %u\n",&b);
    //displays address in memory location of b value
    printf("address of value c is: %u\n",&c);
    //displays address in memory location of c value
    getch();
    return 0;
}

Output

a value is: 24
b value is: 56
c value is: 34.65
......................
address of value a is: 2686748
address of value b is: 2686744
address of value c is: 2686740

 

Program 2

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

int main()
{
    char a='A';
    int b=34;
    float c=43.23;
    printf("a value is: %c\n",a);//display value of a
    printf("b value is: %d\n",b);//display value of b
    printf("c value is: %.2f\n",c);//display value of c
    printf(".....................\n");
    printf("address of value a is: %u\n",&a);
    //displays address in memory location of a value
    printf("address of value b is: %u\n",&b);
    //displays address in memory location of b value
    printf("address of value c is: %u\n",&c);
    //displays address in memory location of c value
     printf(".....................\n");
     int *p1;//pointer declaration
     p1=&a;
     int *p2=&b;
     int *p3=&c;
     printf("\n");
     printf("address of a is: %u\n",p1);
     printf("address of b is: %u\n",p2);
     printf("address of c is: %u\n",p3);
printf(".....................\n");
     printf("a value is: %c\n",*p1);
     printf("b value is: %d\n",*p2);
     printf("c value is: %.2f\n",*p3);

    getch();
    return 0;
}

Output

a value is: A
b value is: 34
c value is: 43.23
............................
address of value a is : 2686739
address of value a is : 2686732
address of value a is : 2686728
............................
address of  a is : 2686739
address of  a is : 2686732
address of  a is : 2686728
............................
a value is: A
b value is: 34
c value is: 43.23

 

 

You may see also

pointer in C++

Factorial calculation of a number using the pointer in C
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago