Home > Back-end >  How to check for an empty character pointer in C
How to check for an empty character pointer in C

Time:01-06

I'm recently working on simple chess game on the terminal using C, but I've ran into a small issue in regard to taking user input.

The following function takes in a 3-Dimensional array containing current chess piece placements, a pointer to a buffer that'll contain the user input, and the size of this buffer which I set to 7.

void next_move(char board[8][8][4], char* move, size_t buff_size){
 
         char * move_1 = (char )malloc(sizeof(char)3);
         char * move_2 = (char )malloc(sizeof(char)3);
         char delim[]=" ";
 
         getline(&move,&buff_size,stdin);
 
         move_1=strtok(move,delim);
         move_2=strtok(NULL,delim);
 
         if(*move_1 == '\0'){
                 printf("invalid move !");
                 return ;
         }
 
         printf("%s%s\n",move_1,move_2);
          if(!check_move(board, move_1, move_2)){
                 printf("valid move !\n");
 
         }
}

The function check_move takes in both moves inputted by the user and verifies if they're valid chess moves (E.g "Nb1 Nc3").

My issue lies in the fact that when the user inputs no character or a string of characters not containing a space (the delimeter defined in strtok) it results in a segmentation fault when I try to do the check:

if(*move_1 == '\0')

Which is used mainly to handle the exception in the case that the move_1 and move_2 pointers are null.

I have two questions:

  1. How can I check if a char pointer is null ? (I have already tried using move_1 == NULL)
  2. Why the does the code continue execution and returns if I set the conditional statement to if(*move_1 != 0). Although this causes all input (even if it's the correct format) to not be valid.

CodePudding user response:

Question 1

It sounds like maybe move is NULL, but your first call to strtok is supposed to have a non-NULL pointer. Therefore, right after you call getline, I think you should check to make sure move is not NULL. It would also be a good idea to check that move_1 and move_2 are not NULL. You can do these checks with code like this:

getline(&move,&buff_size,stdin);
if (move == NULL) {
  // handle this case, probably with an early return
}

Note that the code above only shows a check for move, but I think you should probably do the same thing for move_1 and move_2, right after you assign to them, and before you use them or derefence them. (Some of these checks might turn out to be unnecessary upon further investigation, but for now I think the priority is to just fix all the segmenation faults so your program can run without crashing.)

Question 2

It sounds like you changed your code to the following and it started declaring all your input as invalid:

if(*move_1 != '\0'){
  printf("invalid move !");
  return;
}

Well, the explanation for that is simple. You are telling your compiler that if the first character of the move_1 string is not a null-termination character, that it should print "invalid move !" and return. Therefore, whenever move_1 is non-empty, it will do that. Maybe you meant to use == (equal) instead of != (not equals).

CodePudding user response:

If you mean by entering no characters that you are pressing enter that means your condition should be like: if(*move_1 == '\n'), since the Enter in a new line feed to the command line which is '\n' escape character.
I wish I can see your full project and work together to complete it...

  •  Tags:  
  • Related