Error Code

Will this code cause an error during runtime, error during compilation, or no error?

1.) char str[5];
strcpy (str, "hello");


2.) char *str;
strcpy (str, "hello");

Questions by Mike67

Showing Answers 1 - 33 of 33 Answers

The first code will produce error during compiling. The reason for that being is word hello is assingned as "Hello". This is the syntax of string type, however the array is of char type. So a comiler error will be produced.

The second code will also produce a error during compiling. This is because the code has pointer as the data, but the value given is like to be assingned to a normal variable.  So it would produce a compile time error.

Thank you.

First code will give error
Because while using string functions like strcpy,one extra space is required but we have space of 5 characters and "hello" is also 5 character long,so for storing '' means null character... there is no space so it produces error and the second code is *char means we can store any no. of characters so it will not give any error.

cyriak

  • Mar 15th, 2010
 

Both will give segmentation fault.
For first str[6] is required and for second we have to do malloc for pointer

  Was this answer useful?  Yes

krisj15

  • Apr 19th, 2010
 

I had tested this piece of code in Visual studio 2003...

It doesn't give any compilation error for both the cases.
At runtime , you'll get the error for both the case's.

For the first case you get a Stack corruption error while in the second case, since the pointer is uninitialized, so is as good as a bad pointer and thus access voilation c00005 is thrown.


  Was this answer useful?  Yes

anchal.vab

  • May 23rd, 2010
 

I have tried the given code of TC compiler it is not giving any error in both the codes, however I am not getting correct output for the second code snippet

  Was this answer useful?  Yes

kbjarnason

  • Jul 1st, 2010
 

The implementation is not required to produce an error in either case, at either compile time or runtime.  Whether it actually does depends on many factors - quality of implementation (does it detect the overuse of available space), whether the runtime OS uses memory protection (or the implementation offers a bounds-checking mechanism), what the sensitivity of the protection is (eg it may only fault if passing a 1K boundary) and so forth.


  Was this answer useful?  Yes

dogra

  • Jul 4th, 2010
 

I have run both the code in Developer C. While the first one gave output hello, the second one just gave runtime error!


  Was this answer useful?  Yes

HariGM

  • Aug 3rd, 2010
 

The first code snippet works fine as the assigned space is enough for the string to stored.

The second code snippet will not work since we havent intialized to any of the local variables.
it will work if we assign the base address of the local char array to str
char * str;
char str1[5];
str=str1;

strcpy (str,"Hello");

  Was this answer useful?  Yes

Both snippets result in undefined behavior. 

The first snippet invokes undefined behavior because we are attempting to write one past then end of the array object (7.21.1/1).  This may result in a run-time error (segfault) depending on whether anything "important" is stored in the memory immediately following str.  It may also manifest as a different problem if you overwrite the contents of another variable.

The second snippet invokes undefined behavior because the pointer value is indeterminate (6.7.8/10, 7.1.4/1).  This will most likely result in a run-time error (segfault).

I don't know of any compiler that would catch either problem at translation time. 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions