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

 

 

Similar post

Pointer in  C++ language

Single dimension Array in C++ language

Two dimension Array in C++ language

Three dimension Array in C++ language

Single dim- Array in Java language

Two dim- Array in Java language

Three dim- Array in Java language

Single dim- Array in C language

Two dim- Array in C language

Three dim- Array in C language

 

Suggested for you

Nested for loop in C++ language

Nested while loop in C++ language

The new keyword in Java

for loop in C++ language

While loop in C++ language

 

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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago