What is the role of Declaring a Pointer in C programming

Declaring a Pointer 

In this article, we are trying to understand the role of Declaring a Pointer in C, with program.
 
Declaring a Pointer in C programming
Declaring A Pointer
  •  Pointing variables, must be declared before they may be used in a C program.
  • When a pointer variable is declared, the variable name must be preceded by asterisk(*).
  • The data type that appears in the declaration refers to the data item that is stored in the address represented by the pointer.
  • Pointer variables can point to numeric or character variable, arrays, functions, other pointer variables, or null (zero) value.

 

Usage:

 data-type *pointer_variable;

  • data-type refers to the data type of the pointer's object.
  • *pointer_variable is the name of the pointer variable.
  • Within a variable declaration, a pointer variable can be initialized by assigning it the address of another variable.

 

Program-1 

Difference between ordinary arithmetic expression & Declaring a Pointer

#include <stdio.h> 

/*Difference between ordinary arithmetic expression & Declaring a Pointer. */

void main()

{        int num1, num2;

          int value=3;

          int *value_pointer;                /*value_pointer points to value */

          num1 =2 * (value+5);                /* arithmetic expression */

          value_pointer =&value;

          num2 = 2 * (*value_pointer+5);

         printf("\nNum1 = %d  Num2 = %d", num1, num2);

         }


Run

Num 1 = 16  Num2 = 16

Explanation: 

There are 2 integer expressions in the above program. The first, 2 * (value + 5), is an ordinary arithmetic expression, while the second, 2 * (*value_pointer + 5), involves the use of pointers. Still, the  expressions are equal to each other because value and *value_pointer each represent the same integer value.


 Program-2

Assigning a value to a variable

#include <stdio.h> 

/*Assigning a value to a variable */

void main()

{        int value=3;

          int *value_pointer;               

          value_pointer =&value;

         printf("\n *Value_pointer = %d  Value = %d", *value_pointer, value);

         *value_pointer = 7;

         printf("\n *Value_pointer = %d  Value = %d", *value_pointer, value);

         }


Run

*Value_pointer = 3  Value  = 3

*Value_pointer = 3  Value  = 3

 

Explanation:

  • Assign an initial value of 3 to the integer variable value
  • Assign the address of value to the pointer variable value_pointer 
                  So, Value_pointer becomes a pointer to value
                 The expression *value_pointer represents the value 3
  • The first printf statement displays the current values of *value_pointer and value
  •  The value of *value_pointer is reset to 7

                 So, value will be reassigned the value 7

  • The second printf statement displays the new values of *value_pointer and value.

     

Exercise regularly. Eat a nutritious diet. Don't smoke - WHO (Covid-19).

See also...

Let's see also...

 

Thanks a lot for reading.
Neel Kamal
What is the role of Declaring a Pointer in C programming What is the role of Declaring a Pointer in C programming Reviewed by Neel Kamal on November 29, 2021 Rating: 5

No comments:

For More Details Subscribe & Comment..!

Powered by Blogger.