BM Balmiki Mandal 02 Sep 2022 5 views 0 comments C Programming

How to apply constant terms on the pointer in c

Applying Constant Terms on Pointers in C: A Comprehensive Guide

To apply constant terms on the pointer in C, we can use the const keyword. The const keyword tells the compiler that the value of the pointer cannot be changed.

There are two ways to apply constant terms on the pointer in C:

Constant pointer to a constant variable

 This is a pointer that cannot be changed to point to a different variable, and the variable it points to cannot have its value changed.

C programming
const int a = 10;
const int* ptr = &a;

ptr = &a; // This is allowed.
*ptr = 20; // This is not allowed.

In this example, the pointer ptr is a constant pointer to a constant variable. This means that ptr cannot be changed to point to a different variable, and the variable a that ptr points to cannot have its value changed.

Pointer to a constant variable

This is a pointer that can be changed to point to a different variable, but the variable it points to cannot have its value changed.

C Programming
int a = 10;
int* const ptr = &a;

ptr = &a; // This is allowed.
*ptr = 20; // This is not allowed.

In this example, the pointer ptr is a pointer to a constant variable. This means that ptr can be changed to point to a different variable, but the variable that ptr points to cannot have its value changed.

It is important to note that constant pointers are not the same as constant variables. A constant variable is a variable whose value cannot be changed. A constant pointer is a pointer whose value cannot be changed.

Constant pointers can be useful for preventing accidental changes to the value of a variable. For example, if we have a function that takes a constant pointer to a variable as a parameter, we can be sure that the function will not accidentally change the value of the variable.

Example of a function that takes a constant pointer to a variable as a parameter:

C programming
void print_variable(const int* ptr) {
  printf("%d\n", *ptr);
}

This function can be used to print the value of any variable, as long as the variable is passed to the function as a constant pointer.

 

Constant pointers are a powerful tool that can help us to write more reliable and secure code.

Top Resources

write a macro basic program in c

write a program to Multiplication of two number using macros in c

define a macros for finding the biggest of two numbers

write a c program for printing time date using Pre-define Macros

Further Reading:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

 

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

0 Comments

No comments yet, Be the first to comment.

Post a Comment

Please sign in to post a comment.

Related Posts

C Bitwise Operators Explained: 10 Practice Programs with Output

E.1: Bitwise vs. Logical Operators#include<stdio.h> int main(void){ int x=7,y=19; printf("%d %d ",x&y...

AG Anvesh G 29 Nov 2026
Bit wise operators Assignment

Bitwise Operators practical assignment: Take runtime input, bit-level manipulation, and different data units r...

AG Anvesh G 27 Sep 2026
Understanding Float and Double Binary Representation in C

Understanding Float and Double Binary Representation in CIntroduction:When we store decimal numbers such as 10...

AG Anvesh G 31 May 2026