C Programming - Const - Discussion

Discussion Forum : Const - Point Out Errors (Q.No. 4)
4.
Point out the error in the program.
#include<stdio.h>
const char *fun();

int main()
{
    char *ptr = fun();
    return 0;
}
const char *fun()
{
    return "Hello";
}
Error: Lvalue required
Error: cannot convert 'const char *' to 'char *'.
No error and No output
None of above
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
21 comments Page 2 of 3.

Amit Duhan said:   1 decade ago
6 21 [Error] invalid conversion from 'const char*' to 'char*' [-f permissive].

Compiler DevC++.

Rahul said:   1 decade ago
@Gunjan: You are right. I also get the same error in dev.

But it will run on code::blocks.

Arj said:   1 decade ago
Pointer to a const char can be stored in a regular pointer to a char?

Please explain.

Sanjee said:   1 decade ago
Inthe above program answer is b.

Because we are trying to assign const char* to char.

Gunjan said:   1 decade ago
Implementing the orignal given program in turbo C it is give error shown in option B.

Kiran said:   1 decade ago
Here we declare the function only, no calling thats why sub function doesn't execute.

Gitanshu said:   1 decade ago
I ran the program in Dev C++ and it point outs the error given in B option.

Kavya said:   7 years ago
char *ptr is not a pointer to the function, then how can it point to fun()?

Indi said:   1 decade ago
In gcc if you print the pointer there is no error with that code.

Abhishekppardeshi said:   1 decade ago
I think the fun() never gets called.


Post your comments here:

Your comments will be displayed after verification.