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.
Muhammad Najmudheen said:
9 years ago
/*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.*/
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.*/
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..
Lakshmi said:
1 decade ago
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";
}
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";
}
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();
}
Ritesh_iiita said:
1 decade ago
@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]
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]
Pradeep said:
9 years ago
@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";
}
#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";
}
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.
Akash said:
8 years ago
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.
Because pointer contains constant string only.
So, the fun() returns a constant string so to store constant string pointer must be required.
Thank you.
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.
Srujan said:
1 decade ago
Here the string "hello" is local to that function and when it returns it goes out of scope and returns nothing.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers