A pointer is not an array.
So
static char *text;
defines a pointer which, since it is static
is a NULL pointer. The next action is attempting to treat that pointer as if it is an array,
while((*characters<999)&&(((text[*characters])=getchar())!=EOF))
which gives undefined behaviour. Practically, the result is usually overwriting some random area of memory. Operating systems - if they detect that - will terminate your program (which, in your case, means by triggering a segmentation violation).
Making text
static is irrelevant. Without the static
keyword, all that happens is that text
is created automatically when your program is run, and doesn't exist after it returns. In that case, it will be uninitiatialised (which means even accessing its value gives undefined behaviour).
What you need to do is make text
point at a valid location in memory. Since you are returning its value, that value has to be valid after the function returns. One way is
char text = malloc(999 + 1);
which will be fine, as long as the input does not exceed 999
characters. It will also be necessary for the caller to release that memory to avoid a memory leak.
Also, getchar()
returns an int
. An int
can represent the value of EOF
. There is no guarantee that a char
can. So assigning (text[*characters])=getchar()
means that value (since it has been converted to be a char
) may never compare equal with EOF
.