Hi I got confused to get the explanation for this below, can anyone explain it to me? Thanks in advance.
#include<stdio.h>
int main(){
char *arr,c;
arr = &c;
arr++;
*arr = 'a';
arr++;
*arr = 'b';
arr++;
*arr = 'c';
arr--;
arr--;
printf("\narr 1 = %c",arr);
printf("\narr 2 = %c",arr[1]);
printf("\narr 3 = %c",arr[2]);
getch();
return 1;
}
Output is :
arr 1 = a
arr 2 = b
arr 3 = c
but if the change the line:
printf("\narr 1 = %c",arr);
with
printf("\narr 1 = %c",arr[0]);
Now Output is:
arr 1 =
arr 2 = b
arr 3 = c
why ‘a’ is not getting printed.?
*For all those who are questioning the program as bad coding.. I know its not a good coding practice to use pointer like this, But my question is why arr[0] is not printing anything where as arr[1] & arr[2] is printing what is assigned?
In the first version of the program, what likely happens is:
cat some address on the the stack, say 1003 (a single byte).arrat some address on the stack, say 1004-1007 (four bytes).arr = &c;puts the address ofc, 1003, intoarr.arr++;adds one toarr, so it is now 1004 instead of 1003.*arr = 'a';writes'a'to the place wherearrpoints. This is now behavior that is undefined by the C standard. Whenarrwas set to&c, there was only one byte in the object at that location, and the C standard does not guarantee what happens when you write to other locations after incrementing that address outside the object.arr, so you are writing'a'into one of the bytes ofarr. This horribly alters the address that was inarr.arr++;increments the address again, and*arr = 'b';writes'b'somewhere. We do not know where, becausearrhas a bad value in it.arr++;increments the address again, and*arr = 'c';writes'c'somewhere. We do not know where.arr--;andarr--;return the address to the value it had after'a'was written.printfcall passes the value inarrto be printed with a “%c” specifier. “%c” is for printing a character, but thearryou pass it is a pointer. However, you previously wrote'a'into the pointer, so the value of the pointer has'a'in one of its bytes.printfprints one of the bytes in the pointer value, in this case the same byte where you wrote'a'. So “a” is printed. You cannot rely on this happening in other C implementations. This happened in this case only because the compiler happened to placearrin memory just aftercin memory, so the statement*arr = 'a';wrote'a'intoarr. This will not happen in all C implementations. You got “lucky.”'a'intoarr,arris pointing somewhere, although we do not know where. Let’s call this locationx.arr++;and*arr = 'b';wrote'b'to the locationx+1.arr++;and*arr = 'c';wrote'c'to the locationx+2.arr--;statements returnedarrto point tox. Soarr[1]is the character atx+1, which is'b', andarr[2]is'c'. Passing these to printf to be printed with “%c” prints “b” and “c”.In the second version of the program, when
arr[0]is passed toprintf:arris the value that was formed by writing'a'into one of its bytes. This is some “random” address, pointing to no location we have controlled. Apparently there is a 0 character there, or some other non-printing character, so, whenarr[0]is passed toprintf, nothing is printed.arr[1]andarr[2]print “b” and “c” because the code wrote'b'and'c'to those locations.