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";
}
Discussion:
21 comments Page 1 of 3.
Arj said:
1 decade ago
Pointer to a const char can be stored in a regular pointer to a char?
Please explain.
Please explain.
Abhishekppardeshi said:
1 decade ago
I think the fun() never gets called.
Kiran said:
1 decade ago
Here we declare the function only, no calling thats why sub function doesn't execute.
Kiran said:
1 decade ago
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();
}
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:
1 decade ago
Inthe above program answer is b.
Because we are trying to assign const char* to char.
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.
Rahul said:
1 decade ago
@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..
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:
1 decade ago
@Gunjan: You are right. I also get the same error in dev.
But it will run on code::blocks.
But it will run on code::blocks.
Diana said:
1 decade ago
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:
1 decade ago
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers