Firstly, malloc() and realloc() manage memory, not arrays. There is a difference. However, if you treat a pointer by returned by malloc() as if it was an array, then the compiler treats it that way as well. Array indices, like Java start at zero. So
Code:
char *array = malloc(2); /* sizeof(char) is defined by the C standard to be 1 */
array[0] = 'A';
array[1] = 'B';
array[2] = 'C'; /* invalid, but will not usually yield a compilation error. Result is undefined behaviour, which often manifests as a runtime error */
If you want to compare strings, you do not use the comparison operators directly. You either compare one character at a time (in a loop) or use functions like strcmp() (declared in standard header <string.h>). This is because, by convention in C, a string is represented as an array of char, with a terminating char of value zero. So the string "end" is represented as four characters with values 'e', 'n', 'd', and '\0'. So, if array is an array of 4 char, and we want to stop if it contains the string "end". One way is
Code:
while (!(array[0] == 'e' && array[1] == 'n' && array[2] == 'd' && array[3] == '\0')) ....
This is functionally equivalent to
Code:
while (strcmp(array, "end") != 0) ....
strcmp() is shorthand for "string comparison". It effectively implements a loop, comparing one character at a time.
Also, when you malloc() or realloc(), YOU have to clean up the memory allocated. There is no garbage collection in C.
Another thing is that getchar() returns an int, and EOF is a value of type int that cannot be represented in a char. That means the type of ch needs to be int, not char .... otherwise a loop of the form while (ch != EOF) will never end.
At this stage, any basic textbook on C will get you started. However, I recommend VERY strongly that you try to put Java out of your mind while learning C. C does things very differently than Java, even if sometimes the code looks similar. You will waste a lot of time and effort, unnecessarily, if you keep thinking in terms of how you do things in Java first, and then map to C.