0
#include <stdio.h>
#include <conio.h>
int main()
{
  char b[] = "samuel ricky";
  char c[2];

  c[0] =  'd';
  c[1] =  'a';
  c[2] =  'd';

  printf("%s\n", b);

  getch();
  return 0;   
}

If I run this code, the output is:

damuel ricky

And if the code is deleted

c[2] =  'd';

the output is:

samuel ricky

whereas if the code is removed, there is no connection at all with the results output. How did it happen?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Samuel Ricky
  • 69
  • 1
  • 2
  • 10

2 Answers2

3

Please enable all your warnings, with the -Wall flag, and you will find the answer in your console:

C02QT2UBFVH6-lm:~ gsamaras$ gcc -Wall main.c 
main.c:10:5: warning: array index 2 is past the end of the array (which contains 2 elements) [-Warray-bounds]
    c[2] =  'd';
    ^ ~
main.c:6:5: note: array 'c' declared here
    char c[2];
    ^
1 warning generated.

As the warning says, you write to memory that you don't nescesairily own, since indexing in arrays start from 0, but you seem to already know that.

So char c[2]; has two cells, c[0] and c[1]. Writing to c[2] is invoking Undefined Behavior, which means that what you see printed in your machine now, may be different tomorrow, or at any time in any other machine.

In conclusion, what happens in your machine now is that you are writing 'd' to c[2], which is out of bounds, and it happens to be written in the memory cell of b[0]. That's why you see "damuel ricky" and I, for example, see "samuel ricky".


If I were you, I wouldn't use conio.h, you might end up like Socrates... ;)

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Thanks for reply, Now, how to activate the -Wall? I use windows, and I've tried to turn the opening dev c ++ - tools - compiler option - in "add of the following commands ..." I wrote "-Wall" and I check. But the warning still does not appear. – Samuel Ricky Sep 16 '16 at 01:10
  • @SamuelRicky ah dev-c++, this is a very old tool, and I don't know how to enable them there. If you are attending a class, ask your TA, he might know, if this is possible.. – gsamaras Sep 16 '16 at 01:12
  • So, what compiler that can use -wall ? – Samuel Ricky Sep 16 '16 at 01:32
  • I had used `Eclipse` or `Netbeans` on Windows. The both provide many warnings. `-Wall` is used in `gcc`. – gsamaras Sep 16 '16 at 01:34
2

The declaration char c[2] allocates space for an array with two elements with indices 0 and 1. Your writing to index 2 of that array invokes undefined behavior.

As for why you're seeing the result damuel ricky—many implementations allocate local variables in contiguous chunks on the stack (ignoring some necessary padding). Furthermore, many calling conventions (like the cdecl calling convention) allocate local variables from high to low memory, like so (recall that the stack grows toward low memory):

+------+------+------+------+   <-- low memory
| c[0] | c[1] | b[0] | b[1] |
+------+------+------+------+
| b[2] | ...                |
+------+------+------+------+  <-- high memory

So accessing index 2 of c is effectively the same as accessing index 0 of b. Still note that this is implementation dependent, and you may not exhibit the same behavior using other compilers or machines.

Purag
  • 16,941
  • 4
  • 54
  • 75