-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuse_of_const_keyword.c
More file actions
34 lines (34 loc) · 963 Bytes
/
use_of_const_keyword.c
File metadata and controls
34 lines (34 loc) · 963 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*PROGRAM TO UNDERSTAND THE USE OF 'CONST' AT DIFFERENT PLACES*/
#include<stdio.h>
void case_1();
void case_2();
void case_3();
void main(){
case_1();
case_2();
case_3();
}
void case_1(){ //Note: Using 'const' before declaration of 'x' variable restricts it as 'read only' variable
const int x=5,y=10;
int *ptr=&x;
// x=11; //i.e. gives error cuz of using 'const'
*ptr=80;
ptr=&y;
printf("\n%d, %d, %d",x,y,*ptr);
}
void case_2(){ //Note: Using 'const' before declaration of '*ptr' pointer restricts it as 'read only' pointer
int x=5,y=10;
const int *ptr=&x;
x=11;
// *ptr=80; //i.e. gives error cuz of using 'const'
ptr=&y;
printf("\n%d, %d, %d",x,y,*ptr);
}
void case_3(){ //Note: Using 'const' between the '*' & 'pointer name' restricts the pointer to assign new address
int x=5,y=10;
int *const ptr=&x;
x=11;
*ptr=80;
// ptr=&y; //i.e. gives error cuz of using 'const'
printf("\n%d, %d, %d",x,y,*ptr);
}