I am using the code below to take in user input and then compare it with the string exit
so that users can exit out of a game by typing that into the command line. However it doesn't work unless I use *buffer
in the comparison with exit
. It does that because it points to the first letter in exit
. The problem is that means any word starting with e
will cause my while loop to exit.
#include<stdio.h>
#include<stdlib.h>
int input( char *s, int length);
int main(){
char *buffer;
size_t bufsize = 32;
size_t characters;
do{
buffer = (char *)malloc(bufsize * sizeof(char));
if(buffer == NULL){
perror("Unable to allocate buffer");
exit(1);
}
printf("Enter a command: ");
characters = getline(&buffer, &bufsize, stdin);
printf("%zu characters were read.\n", characters);
printf("You typed: '%s' \n",buffer);
}while(buffer != "exit");
}