C Programming - Const - Discussion

Discussion :: 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";
}

[A]. Error: Lvalue required
[B]. Error: cannot convert 'const char *' to 'char *'.
[C]. No error and No output
[D]. None of above

Answer: Option C

Explanation:

No answer description available for this question.

Arj said: (Aug 20, 2011)  
Pointer to a const char can be stored in a regular pointer to a char?

Please explain.

Abhishekppardeshi said: (Sep 20, 2011)  
I think the fun() never gets called.

Kiran said: (Oct 3, 2011)  
Here we declare the function only, no calling thats why sub function doesn't execute.

Kiran said: (Oct 3, 2011)  
Here we have function and pointer declaration only. No calling.

Try this one, you guys may understand.

#include<stdio.h>
const char *fun()
{
return "Hello";
}
int main()
{
char *ptr=fun();
printf("%c",*ptr++);

printf("%c", *ptr++);
printf("%c", *ptr++);
printf("%c", *ptr++);
printf("%c", *ptr++);

getch();
}

Sanjee said: (Oct 14, 2011)  
Inthe above program answer is b.

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

Gunjan said: (Nov 6, 2011)  
Implementing the orignal given program in turbo C it is give error shown in option B.

Rahul said: (Jan 7, 2012)  
@Kiran :

Here we declare the function as well as we call also in the statement
char *ptr=fun();
you can justify it by following ex:

#include<stdio.h>
const char *fun();
int main()
{
char *ptr = fun();
return 0;
}
const char *fun()
{
printf("must be call");
return "Hello";
}

Some of the compilers give error: invalid conversion from 'const char*' to 'char*' but some give the output successfully..

Rahul said: (Jan 7, 2012)  
@Gunjan: You are right. I also get the same error in dev.

But it will run on code::blocks.

Diana said: (Aug 31, 2012)  
If the const weren't there then the ans will be C otherwise it gives error i.e ans will be B in case of Turbo C/C++ compiler.

Arun Prasad said: (Nov 20, 2012)  
The correct o/p is B in TC++ compiler.

Error: Cannot convert from 'const char *' to 'char *'. This occurs because fun() returns a pointer to a constant character which is being assigned to a pointer to a non-constant character.

Ritesh_Iiita said: (Jul 22, 2013)  
@Arun prasad:

O/P in gcc compiler is also b its is throwing error i.e invalid conversion from const char to char. here is the code and output:

#include<stdio.h>
const char *fun();

int main()
{
char *ptr =fun();
return 0;
}
const char *fun()
{
return "Hello";
}

o/p:7 INDIABIXTEST6_18.C [Error] invalid conversion from 'const char*' to 'char*' [-fpermissive]

Gitanshu said: (Sep 5, 2013)  
I ran the program in Dev C++ and it point outs the error given in B option.

Indi said: (Oct 4, 2013)  
In gcc if you print the pointer there is no error with that code.

Lakshmi said: (Apr 14, 2014)  
I hope there is no problem with assigning char* to const char*. So no error.

As there is printf statement in the program there is no output.
So option C is correct.

\\Check with the following program. It will help you friends.

#include<stdio.h>
const char *fun();
int main()
{
char *ptr = fun();
int i;
for(i=0;i<5;i++)
{
printf("%c",*(ptr+i));
}
return 0;
}
const char *fun()
{
return "Hello";
}

Srujan said: (Feb 15, 2015)  
Here the string "hello" is local to that function and when it returns it goes out of scope and returns nothing.

Amit Duhan said: (Apr 2, 2015)  
6 21 [Error] invalid conversion from 'const char*' to 'char*' [-f permissive].

Compiler DevC++.

Muhammad Najmudheen said: (Feb 25, 2016)  
/*Actually the code will work fine if we declare the pointer in main function as*/

const char *ptr=fun();

/*It is because here the function returns a constant character pointer value. so it should be received like that only*/

return "hello";

/*This statement will store the string in memory and will return the base address of the string. It will work fine since the function is declared as a function which will return a constant character pointer value and it is received by a constant character pointer in main function.*/

Pradeep said: (Jul 5, 2016)  
@All. Refer this code.

#include<stdio.h>
const char *fun();

int main()
{
char *ptr = (char*)fun();
//printf("%p %s\n",&ptr,ptr);
puts(ptr);
return 0;
}
const char *fun()
{
return "Hello";
}

Akash said: (Oct 10, 2017)  
Yes, you are right @Lakshmi.

Because pointer contains constant string only.

So, the fun() returns a constant string so to store constant string pointer must be required.

Thank you.

Kavya said: (Aug 3, 2018)  
char *ptr is not a pointer to the function, then how can it point to fun()?

Ankit said: (Jul 9, 2020)  
The answer must be option B.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.