0

I am coming back from after reading this c-faq question I am totaly confused what happening here.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main  ()
 {
   char ar[3]="NIS", *c;
   printf ("%s\n", ar);
   strcpy (c, ar);
   printf ("%s\n", c);
   if (ar[4] == '\0')
{
   printf ("Null");
 }
else 
  {
  printf ("%c\n", ar[4]);
  }
}

Here I have assigned "NIS" Equal size of declare array.and when i try to access ar[3],ar[4] it giving null why ? it's ok in case of ar[3] but why in case of ar[4] Another thought: In c-faq it was mentioned that if you assign any string equal to the size of declared array, you can't use printf ("%s"), and strcpy() on that array as it is mentioned in c-faq. But in my above code i have used printf as well as strcpy here both working fine.It might be i have interpreted wrong please correct me. and another question is that When I try to compare ar[5] with null it is not printing anything that's ok but why it is printing Null for ar[4].My thought on this "NIS" String will store in memory like this..

Thanks in advance.

  --------------------------------------------------------
  |   N   |   I    |   S   |   /0   |  Garbage value here
  |_______|________|_______|________|_____________________
    ar[0]    ar[1]   ar[2]    ar[3]  

Well Here ar[3] is giving null when I compare it with '\0' that's ok but when I comapre it with ar[4] still it giving me null instead of some garbage value..

5
  • 2
    ar[3] is not a C-string. Declare it as ar[4] so it can hold the additional \0 byte. Commented May 17, 2012 at 7:05
  • @Daniel Kamil Kozar: Dear Please first read the entire question why are you in hurrry....I know whatever you pointed out. Commented May 17, 2012 at 7:06
  • 1
    The contents of the stack are undefined upon the entry to your function. In this case, you are apparently lucky enough that there's a \0 after your C-string. This, however, will not always be the case. Sorry for not reading the whole question earlier. Also, int main(void) instead of void main(). Commented May 17, 2012 at 7:07
  • @DanielKamilKozar: Is it necessary in my code mentioned main(void)....???I don't think so Commented May 17, 2012 at 7:19
  • 1
    Yes. In C, main() creates a function with an arbitrary number of parameters. Commented May 17, 2012 at 7:31

3 Answers 3

4

Your code exhibits undefined behaviour. It works for you by chance, but on another machine it could fail. As you understood from the FAQ, the code is not valid. But that does not mean it will always fail. That is simply the nature of undefined behaviour. Literally anything can happen.

Accessing ar[3] is illegal because that is beyond the end of the array. Valid indices for this array are 0, 1 and 2.

You did not allocate memory for c so any de-referencing of the pointer is undefined behaviour.

Your main declaration is wrong. You should write:

int main(void)
Sign up to request clarification or add additional context in comments.

5 Comments

I have checked on Codepad. Please check there it is working there.
Please read the answer again. The fact that code appears to work on one platform and compiler does not mean that code is legal. You just got lucky. Your luck will run out soon enough.
Intially I was correct but codepad make me so confused...I check there codepad just lost there mind...
Is it necessary to mention void in main function parameter and return type int.
Your main is non-standard. Surely it's easier to do it right?
2

Don't do this. The declaration char NIS[3]; gives you a three-character array with which you can use the indexes 0 through 2 inclusive.

Any use of other indexes (for dereferencing) is undefined behaviour and should not be done.

The reason why it may be working is because there's nothing stating that the "garbage" values have to be non-zero. That's what garbage means in this context, they could be anything.

Your strcpy is also undefined behaviour since your c pointer has not been initialised to anything useful.

Comments

0

ar[3] does not exist because ar is only 3 characters long.

That faq is saying that it is legal, but that it's not a C string.

If the array is too short, the null character will be cut off.

Basically, "abc" is silently 'a', 'b', 'c', 0. However, since ar is of length 3 and not four, the null byte gets truncated.

What the compiler chooses to do in this situation (and the OS) is not known. If it happens to work, that's just by luck.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.