Answer: O key! First, we try to understand what a regular pointer is, how is this declared, initialized and used in a C program? Consider a program, for example:
/* * diff_regptr_and_ptr2const1.c -- Program shows what happens when regular * pointer is assigned addresses of non-constant and constant data */ #include <stdio.h> int main(void) { float pass_per = 60.9; /* pass_per is non-constant float */ const float fare = 31.35; /* fare is constant float */ float *fp = &pass_per; /* fp, regular pointer to float */ printf("\nIn the exp. \"float *fp = &pass_per\":\n"); printf("fp is a regular pointer to float pass_per, value of " "pass_per is %f\n", *fp); printf("Now, we try modify pass_per using regular pointer fp...\n"); *fp = 70.9; printf("O key, now, in exp. \"*fp = 70.9\", pass_per is %f\n", *fp); printf("\nNow, let's see what happens when fp is assigned address of" " some \"constant data\"\n"); fp = &fare; /* assigned fp address of constant fare */ printf("\nIn the exp. \"fp = &fare\":\n"); printf("fp is a regular pointer to constant float \"fare\", value of" " fare is %f\n", *fp); printf("Now, we try to modify constant fare using fp...\n"); *fp = 62.70; printf("O key, now, in the exp. \"*fp = 62.70\", value of constant " "data fare is %f\n\n", *fp); return 0; }
We test the program for output below:
test1.c: In function ‘main’: test1.c:20:8: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default] [root@localhost ch06_Pointers]# ./a.out In the exp. "float *fp = &pass_per": fp is a regular pointer to float pass_per, value of pass_per is 60.900002 Now, we try modify pass_per using regular pointer fp... O key, now, in exp. "*fp = 70.9", pass_per is 70.900002 Now, let us see what happens when fp is assigned address of some "constant data" In the exp. "fp = &fare": fp is a regular pointer to constant float "fare", value of fare is 31.350000 Now, we try to modify constant fare using fp... O key, now, in the exp. "*fp = 62.70", value of constant data fare is 62.700001
So we observed behaviour of regular pointer is quite alarming when the same is assigned address of “constant data”. Program compiled well with a “Warning: assignment discards ‘const’ qualifier from the target type [enabled by default]”. Be careful! Never use regular pointer with constant data as this can modify even constant data.
Now, this time it’s turn to consider pointer to constant, again through some examples:
/* * diff_regptr_and_ptr2const2.c -- Program shows what happens when pointer * to const is assigned address of non-constant and constant data */ #include <stdio.h> int main(void) { int holidays = 69; /* non-constant holidays */ const int teachers = 98; /* teachers declared and initialized to be constant */ const int *cip = &holidays; /* cip is a pointer-to-constant-integer */ printf("\nFor Non-Constant Integer \"holidays\":\n"); printf("In exp. \"const int *cip = &holidays\", no of holidays in 2014" " %d\n", *cip); printf("\nO key, now we try to modify non- constant integer " "\"holidays\" \nusing pointer to constant integer \"cip\"\n"); *cip = 100; /* modifying holidays using cip */ printf("In exp. \"*cip = 100\", we tried to modify no of holidays in " "2014 from 69 to %d\n", *cip); cip = &teachers; /* assigning pointer-to-constant-int address of const data 'teachers' */ printf("\nFor Constant Integer \"teachers\":\n"); printf("In exp. \"cip = &teachers\", there are %d teachers in a school." "\n", *cip); printf("\nO key, now we try to modify constant integer \"teachers\"\n" "using pointer-to-constant-integer \"cip\"\n"); *cip = 100; /* modifying teachers using cip */ printf("In exp. \"*cip = 100\", we tried to modify no of teachers in a " "school from 98 to %d\n", *cip); return 0; }
Let’s analyse output of the program below:
[root@localhost ch06_Pointers]# gcc diff_regptr_and_ptr2const2.c diff_regptr_and_ptr2const2.c: In function ‘main’: diff_regptr_and_ptr2const2.c:14:2: error: assignment of read-only location ‘*cip’ diff_regptr_and_ptr2const2.c:24:2: error: assignment of read-only location ‘*cip’
Wow! Using pointer-to-constant is very-very safe! However, pointer-to-constant, in ex. prog. ‘cip’, was assigned addresses of non-constant and constant data at places without any problem but when we tried ‘cip’ to modify data whether constant or non-constant, pointed to by ‘cip’, it simply couldn’t. Instead, it caused errors at compile time with massage “error: assignment of read-only location ‘*cip’”.
A practical consequence of pointer-to-constant is to use pointer as a formal argument in a function which processes data and thus prevents data from being modified occidentally. For example:
/* adv_ptr2const.c -- Program displays advantages of pointer-to-constant */ #include <stdio.h> #define FAM_MEM 5 #define SUB 5 void modify_const_data(const float *, int); /* fun. prototype */ int main(void) { const float expenses[FAM_MEM] = {360.00, 437.12, 45.65, 338.34, 98.987}; /* array of constant floats */ float marks[SUB] = {34.03, 56.98, 66.78, 68.0, 80.0}; /*array of non-constant floats */ modify_const_data(marks, SUB); modify_const_data(expenses, FAM_MEM); return 0; } void modify_const_data(const float *base, int val) /* fun. def. */ { int i, FLAG = 1; const float *copy; for (i = 0; i < val; i++) { copy = base++; if (FLAG) { printf("Now, we try to modify elements of array...\n"); FLAG = 0; } *copy += 10.0; /* increment each array element by 10.0 */ } }
Observe the output as:
[root@localhost ch06_Pointers]# gcc adv_ptr2const.c adv_ptr2const.c: In function ‘modify_const_data’: adv_ptr2const.c:30:9: error: assignment of read-only location ‘*copy’
Here, in the function: modify_constant_data(), pointer to constant ‘*copy’ receives both non-constant and constant data but prevents original data from alterations.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Watch Advanced C Programming Videos
- Check C Books
- Apply for Computer Science Internship
- Practice BCA MCQs
- Apply for C Internship