For working professionals
For fresh graduates
More
Explore C Tutorials: From Begi…
1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
Now Reading
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
The constant pointers in the C programming language are the pointers that hold the address of any variable, and the value of these constant pointers can not be changed once assigned. In the more specialized word, if any pointer is pointing to the located memory address of a variable and it will not allow the user to change the pointer memory allocation to vary other memory locations, these kinds of procedures we used in case we want the address of any particular type variable to be fixed, or we do not want to allocate the memory location to a pointer, we can set the data type of constant pointer they can be an integer, float, etc. according to the requirement or data type of variable which the cons pointer is pointing. In this article, we are going to learn about Const Pointer in C.
As described above, a constant pointer in C is one whose value can not be changed in the program. It is relatively similar to a constant variable in C. The only difference here is that, by description, pointers store memory addresses. A constant pointer will continue to point to the same location in the memory that it was initially assigned.
Syntax:
<data type> * const <pointer name> = <memory address>;
Examples:
Let's discuss the constant pointer example:
#include
int main() {
const int num = 5; // Declare a constant type integer variable
const int* ptr; // Declare a constant type pointer to an integer value
ptr = # // Assign this address of num to the pointer
// Try to change the values pointed to by ptr
// This will result in a compilation error since the value is constant
*ptr == 10;
// Try to reassign the pointer values to point to a different constant value
// This is allowed because the pointer itself is not constant
const int new_num = 8;
ptr = &new_num;
// Print the values of the num and the new_num
printf("num = %d\n", num);
printf("new_num = %d\n", new_num);
// Print the values that are pointed to by the pointer
printf("*ptr = %d\n", *ptr);
return 0;
}
Output:
num = 5
new_num = 8
*ptr = 8
In this illustration, we first declare a constant integer variable num with a value of 5. After that, we declare a constant pointer in C to an integer value ptr using the const int * syntax.
We give the pointer ptr the address of num. Any effort to change the value that ptr points to will cause a compiler error since it is a constant value.
We next define a second integer constant variable new_num, with the value of 8. The pointer ptr is then reassigned to refer to the address of new_num. This is OK because the pointer itself is not a constant.
Eventually, we print the values of num and new_num and the value pointed to by the pointer ptr. Notably, num and new_num still have their original values, but the value that the pointer ptr was pointing at now refers to new_num.
In the pointers to constant, the data which is pointed by the pointer is constant and can not be changed. However, the pointer itself is variable and can shift and point in a different direction.
Let's examine why it is not desirable to save the address of a const variable using a regular pointer. Consider the following instance:
#include <stdio.h>
int main()
{
const int x = 10;
int *ptr = &x;
*ptr = 50;
printf("Value of 'x' is %d", x);
return 0;
}
Output:
main.c:6:20: warning: The initialization discards the ‘const’ qualifier from the pointer target type [-Wdiscarded-qualifiers]
6 | int *ptr = &a;
| ^
The value of 'a' is 50
As we can see from the result above, even though an is defined as a const variable, its value has changed from 10 to 50 despite the compiler issuing a warning. This occurs because anytime a regular pointer, like ptr, attempts to change the value of a const variable, the C compiler for Mac momentarily discards the constant attribute that was assigned to the variable. This is not advised since it might result in security holes and undermine the goal of making the variable constant.
As a result, in order to point to constant variables in C, we must use a distinct pointer-type syntax.
Let's discuss the constant pointer in c with the example:
Use the const keyword before the data type of the pointer to define a pointer to a const value:
int main()
{
const int x{ 5 };
const int* ptr { &x }; // Allowed: ptr is pointing directly to a "const int"
*ptr = 6; // not allowed: we can not change a const value
return 0;
}
In the above constant pointer in the c example, ptr refers to a const int. The value which is pointed to cannot be modified since the data type being referenced is const.
However, because a pointer to const is not itself const (it only pointers to a const value), we may alter what the pointer is pointing to by giving it a new address:
int main()
{
const int x{ 9 };
const int* ptr { &x }; // ptr directly points to const int x
const int y{ 6 };
ptr = &y; // Allowed: ptr now points at const int y
return 0;
}
A pointer to const is a point to non-const variables, just like a reference to const does. Whether or not the object at that location was
initially specified as const, a reference to const regards the value being referred to as constant:
int main()
{
int x{ 7 }; // non-constant
const int* ptr { &x }; // ptr points to a "const int"
*ptr = 6; // not allowed: Since ptr points to a "const int," we are unable to alter the value by using the ptr.
x = 6; // allowed: This value is still non-constant when accessed through the non-const identifier x
return 0;
}
The data pointed to by the pointer in constant pointers to constants is constant and cannot be modified. Since the pointer is fixed, it cannot be moved to another location.
Syntax:
const <type of pointer>* const <name of the pointer>;
Examples:
#include
int main()
{
int a=10, b=20;
const int* const ptr=&a;
*ptr=12; //wrong
ptr=&b; //wrong
printf("Value of ptr is :%d",*ptr);
return 0;
}
In this article, we discussed the pointers in C and its examples. When turning the C source code into assembly-level instructions, the compiler can perform various optimizations when a constant pointer is defined. Declaring a pointer variable as constant acts as a safeguard against unintentional changes if it is not intended for a pointer variable to have its value changed when it is supplied as a parameter to a function.
1. What do you mean by a Const pointer?
A constant pointer is one that is incapable of altering the address of the variable it points to.
Const pointers may only point to one variable at a time; they cannot be changed to point to another variable. This is referred to as a const pointer.
2. How can I tell if 'const' applies to directed data or pointers?
The const keyword belongs to pointed data if it occurs in the left half of the statement (like in const int * foo), and the pointer is mentioned if it appears in the right portion (like in int * const bar). Split the statement at the asterisk sign.
3. What is a Pointer to a Constant?
This is a pointer that can be altered to point to a different address in memory, but it cannot be altered to change the location it points to. The const keyword should come before the data type being referenced in C to indicate this.
4. What are constant pointers to constant?
A constant pointer to a constant is a pointer that is a composite of the two points. It cannot alter the value assigned to the variable it is referring to or the address to which it is pointing.
5. What is Const char * P?
Const char *p represents a constant character. As a result, the value that is saved in the pointer cannot be altered. The character data may be accessed using the pointer, but it cannot be changed. Strings of characters that won't be modified can be stored using this kind of pointer.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.