How to access or modify the const variable in c ?
Answers were Sorted based on User's Feedback
Answer / skp
@Answer #7 - Answer of #6 is correct.
In Dev C++ the output is as answer #6.
| Is This Answer Correct ? | 0 Yes | 2 No |
Answer / ramya
const int x = 10;
int *ptr = &x;
*ptr = 20;
printf ("Value of x is %d\n", x);
Even though the variable x is const the value gets changed..
And its one of the drawback in c..
| Is This Answer Correct ? | 28 Yes | 31 No |
Answer / raj
@Ans No #9
Actually we are here discuss about how to modify value
store in the constant variable.
I don't more ,but i think in the last we have value of 'z'
same as before corrupted.
please reply me if any one have the answer of this question.
Is it possible or not??
Thanks
| Is This Answer Correct ? | 2 Yes | 8 No |
Answer / santhi
we can access the constant variable in c through the
functions which are declared as constant.
| Is This Answer Correct ? | 13 Yes | 22 No |
Answer / samrat
You can modify the const variable in C by using pointers.
#include <stdio.h>
int main()
{
const int val = 20;
printf("Value is: %d\n", val);
int *ptr =(int*)&val;
*ptr = 2000;
printf("Value is: %d\n", val);
return 0;
}
Output
Value is: 20
Value is: 2000
| Is This Answer Correct ? | 13 Yes | 28 No |
Answer / vignesh
@Ramya
const value cannot be changed...you declared it wrong,
int *ptr = &x;
is not correct.It should be,
int ptr = &x;
also,according to your program only the *ptr value is 20 and
not the value of x
| Is This Answer Correct ? | 5 Yes | 51 No |
Why does notstrcat(string, "!");Work?
What is false about the following A compound statement is a.A set of simple statments b.Demarcated on either side by curly brackets c.Can be used in place of simple statement d.A C function is not a compound statement.
Explain what is the concatenation operator?
You are to write your own versions of strcpy() and strlen (). Call them mystrcpy() and mystrlen(). Write them first as code within main(), not as functions, then, convert them to functions. You will pass two arrays to the function in the case of mystrcpy(), the source and target array.
How to calculate sum
write function to reverse char array ... without using second array
the maximum length of a character constant can be a) 1 character b) 8 characters c) 256 chaacters d) 125 characters
write a c program to add two integer numbers without using arithmetic operator +
What are macros in C?
What is new line escape sequence?
write a fuction for accepting and replacing lowercase letter to'Z' with out using inline function.
A program to allow an input operand and operator from the operator and read on the display and output operand.