differentiate between
const char *a;
char *const a; and
char const *a;
Answer Posted / shaista naaz
char * a = "Hello world";
1)
we cannot make a[i] = 'Some character'. This is not allowed.
but we can make
a = "Hi world";
that is char * a is basically a is a pointer which can point
to different address like when we say
a = "Hello world" and then we say a = "Hi world";
Here a is pointing to different address bit as I said before
If we try a[i] = 'some char' will give compiler error as
because here the string is constant not pointer so this is
const char * a = a is a pointer which is pointing to a
constant string. This is a default behaviour.
2)
char * const a = "I am good" ;
Now here you cannot do any thing no modification allowed.
Try doing a[0] = 'Y';
it fails.
Try doing a = "You are good";
It fails too
Error is You cannot assign to a variable which is a constant.
So a is a variable which is a pointer to character and is
constant.
or a is a constant pointer to character.
3)
char const * a = const char *a
As in both the case a is a pointer to character which is
constant and a can point to different string but this string
itself cannot be modified. This is the default behavior of
char * a.
| Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
Can a variable be both const and volatile?
can any one tel me wt is the question pattern for NIC exam
What does it mean when the linker says that _end is undefined?
What is a program flowchart and explain how does it help in writing a program?
we called a function and passed something do it we have always passed the "values" of variables to the called function. such functions calles are called a) calls by reference b) calls by value c) calls by zero d) none of the above
Explain what is the most efficient way to store flag values?
How would you obtain the current time and difference between two times?
Why is #define used?
List the difference between a 'copy constructor' and a 'assignment operator' in C?
In c language can we compile a program without main() function?
What is the difference between volatile and const volatile?
What are the types of type qualifiers in c?
How is a structure member accessed?
Add Two Numbers Without Using the Addition Operator
What are the scope of static variables?