Вы находитесь на странице: 1из 6

*********************************************************************** ************* THE CONST PHENOMENON ******************

*********************************************************************** 1] Point out the error in the following program. main() { const int x; x = 128; printf("%d",x); } 2] What would be the output of the following program? main() { int y = 128; const int x = y; printf("%d",x); } A] B] C] D] 3] 128 Garbage value Error 0 What would be the output of the following program? main() { const int x = get(); pjrintf("%d",x); } get() { return(20); } A] B] C] D] 4] 20 Garbage value Error 0 Point out the error, if any, in the following program : #define MAX = 128 main() { const int mac = 128; char array[max]; char string[MAX]; array[0] = string[0] = 'A';

printf("%c %c",array[0],string[0]); } 5] Point out the error in the following program : main() { char mybuf[] = "Zanzibar"; charyourbuf[] = "Zienzkewiz"; cajr *const ptr = mybuf; *ptr = ''; ptr = yourbuf; } 6] Point out the error in the following program : main() { char mybuf[] = "Zanzibar"; char yourbuf[] = "Zienckewiz"; const char *ptr = mybuf; *ptr = 'a'; ptr = yourbuf; } 7] Point out the error in the following program : main() { char mybuf[] = "Zanzibar"; const char *const ptr = "Hello"; ptr = mybuf; *ptr = 'M'; } 8] What does the following prototype indicate? strcpy(cahr *target, const char *source); 9] What does the following prototype indicate? const char *change(char *,int) 10] Point out the error in the following program: main() { const char *fun(); char *ptr = fun(); } const cahr *fun() { return"Hello";

} 11] Is there any error in the following program? <Yes / No> main() { const cahar *fun(); char *ptr = fun(); } const char *fun() { return"Hello"; } 12] Point out the error in the following program : main() { const char *fun(); *fun() = 'A'; } const char *fun() { return"Hello"; } 13] 14] What do you mean by const correctness? What would be the output of the following program? main() { const int x = 5; int *ptrx; ptrx = &x; *ptrx = 10; printf("%d",x); } A] B] C] D 15] 5 10 Error Garbage value What would be the output of the following program? main() { const int x = 5; const int *ptrx; ptrx = &x; *ptrx = 10; printf("%d",x);

} A] B] C] D 16] 5 10 Error Garbage value Point out the error in thee following program: main() { const int k = 7; int *const q = &k; printf("%d",*q); } 17] What is the difference int the following declarations? const char *s; char const *s; 18] What is the difference int the following declarations? const char *const s; char const *const s; 19] Is the following a properly written function? If not, why not? int fun(const int n) { int a; a = n * n; return a; }

*********************************************************************** ****************** ANSWERS **********************

*********************************************************************** 1] Nowhere other than through initialization can a program assign a value

to a const identifier. 'x' should have been initialised ared. 2] 3] A a

where it is decl

4] The dimension of an array must always be positive non-zero which max is not. 5] 'ptr' pointer is constant. In ptr = yourbuf the program is o modify it, hence an error. 6] 'ptr' can be modified but not the object that it is pointing to. Hence '*ptr = 'A'' gives an error. 7] 'ptr' is a constant pointer to a constant object. We can modify 'ptr' nor the object it is pointing to. 8] We can modify the pointers source as well as target. However, ct to which source is pointing cannot be nodified. 9] The function change() receives a char pointer and an int and a pointer to a constant char. 10] 12] . No. fun() retruns a pointer to a const character which cannot be

integer

trying t

neither

the obje

returns

modified

13] A program is 'const correct' if it nevr changes (a more common term is mutates) a constant object. 14] 15] B C only by

16] Warning: Suspicious pointer conversion. 'k' can be pointed to a pointer to a const; q is a const pointer. 17] 18] There is no difference. There is no difference.

19] Since fun() would be called by value there is no need to declare n as a const since passing by value already prevents fun() from modifying n.

Вам также может понравиться