This question was asked in my Tally interview question please help me and tell me whats the error in the C code given below. I'd be grateful.
int main()
{
char *p="Tally";
strcpy(p,"piyush");
printf("%s",p);
}
This question was asked in my Tally interview question please help me and tell me whats the error in the C code given below. I'd be grateful.
int main()
{
char *p="Tally";
strcpy(p,"piyush");
printf("%s",p);
}
There are several "errors" in this code (whatever was supposed to be meant by "error").
The primary issue is that the code calls undeclared functions strcpy
and printf
. Formally, this is a compile error in modern C. And in case of printf
this is undefined behavior in pre-C99 versions of C.
And if we fix that issue, then the strcpy
call will make an attempt to modify a string literal. String literals are not modifiable. Such modification attempt will cause undefined behavior.
Because of the above UB, it is impossible to say what is passed to printf
, but it looks like printf
call was meant to perform output to a text stream without finishing the last line with newline character. It is implementation-defined whether such newline character is required.
Finally, although it is not an "error", one can argue that const char *
pointers should normally be used for pointing at string literals.
p is a pointer to the string literal "Tally"
. You cannot overwrite a literal.
In line 3 you are storing a pointer to a string literal, then you try to overwrite its content with that strcpy
; problem is, string literals are read-only (it's UB if you try to write on them, and on modern platforms it typically results in a crash).
If you want a writable string, you have to allocate a local buffer (wide enough for any data you want to store in it).
Still, these are the basis of string handling in C, I strongly suggest you to revise these arguments on your C book before going further.
p
is a pointer to a five character string literal. piyush
is six characters long. This will overflow the space allocated for *p
, and even if it didn't, *p
can not be modified due to the way it was declared.
"Tally" is in the read only section of the executable. You are trying to change it, It balks and therefore fails. What is the problem
this line is wrong
char *p="Tally";
the pointer has no adres where it has to write to you can do for example this
char t;
char *p;
p = &t;
*p='a';
o and in a char you cant put a string
edit a nice link to look at http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/#pointers
Answer to your interview question - Here are the errors with this code :
1. No libraries included, hence printf and strcpy are undeclared functions
2. int main() has no return in the code. return 0; is required // gcc will throw a warning when -Wall flag is set
3. p is a string literal. Which can't be overwritten.
EDIT:
For those of you who say return is not required: Here is the result when compiling with gcc with -Wall flag set:
Notra:Desktop Sukhvir$ gcc -Werror -Wall -g -o try try.c
cc1: warnings being treated as errors
try.c: In function ‘main’:
try.c:19: warning: control reaches end of non-void function
Granted compiler will overlook this if -Wall isn't set, but it is still good practice to include it. @AndreT the very fact that gcc throws up a warning is the hint that it is meant to be there. Sure code can still compile without it .. but that doesn't make it good practice
Edit 2:
when gcc is forced to use C99 standards, it compiles without warnings. So please disregard point 2 of my post.