Q:

Grids and pointers in c

I made this program in C where an object R is placed on a grid and it's supposed to move taking inputs from they keyboard. For example, thi is what happens if you press N.
         0 1 2
       0 - - -                      R - -                  - - -                               
       1 R - -  PRESS N -> GO UP -> - - - PRESS N AGAIN -> - - -
       2 - - -                      - - -                  R - -

So R makes it go up. The object has to move around, so when it is at [A0][B0], for example, it needs to go all the way down [A2][B0]. See above.
It will move up, down, left and right.
Right now i'm creating the function to make it move up, but i'm having a lot of troubles: sometimes it randomly freezes to 2:0 and 0:0 without goind up, and when it's at A = 2, instead of going up of 1, it goes to 0, although i set it to do 2-1 (to go up it has to subtract 1).
I don't understand what's causing those troubles, any advice?
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define X 3
#define Y 3

struct coords{
    int a;
    int b;
};

typedef struct coords cord;

// Print the array
char printArray(char row[][Y], size_t one, size_t two, struct coords cord)
{  

   row[cord.a][cord.b] = 'X';

   // output column heads
   printf("%s", "       [0]  [1]  [2]");
   // output the row in tabular format
   for (size_t i = 0; i < one; ++i) {

      printf("\nrow[%lu] ", i);

      for (size_t j = 0; j < two; ++j) {
         printf("%-5c", row[i][j]);
      } 
   } 
} 

int moveUp(struct coords * cord);

int main(void)
{  
   struct coords cord;

   char row[X][Y] =  
      { { '-', '-', '-'},
        { '-', '-', '-'},
        { '-', '-', '-'} };

   srand(time(NULL));

   cord.a = (rand() % 3); 
   cord.b = (rand() % 3);
   printf("\nValori rand: A %d, B %d\n", cord.a, cord.b);

   // output the row

   //printf("\nrobot:%c\n", robot);
   puts("The array is:");
   printf("\n");

   printArray(row, X, Y, cord);
   row[cord.a][cord.b] = '-';

   //printArray(row, X, Y, &m, &n);
   char h;

   while(h != '3'){

    switch (h) {

      case 'N':

        moveUp(&cord);
        printArray(row, X, Y, cord);
        row[cord.a][cord.b] = '-';

        break;
    }
    scanf("%s", &h);

  }

  printf("\n");
}

int moveUp(struct coords * cord)
{

   cord->a - 1;

   if (cord->a == 2){
      cord->a - 1;
   } else if (cord->a == 1){
      cord->a - 1;
   } else if (cord->a == 0){
      cord->a + 2;
   }

   /*
   if (cord->a == 0) {
    cord-> a = 2;
   } else {
    cord->a - 1;
   }
   */

   printf("\n A = %d, B = %d\n", cord->a, cord->b);

}

A:

In the below code, you are checking the value of h before having read anything into it. If the uninitialized value of h happens to be 3, then execution will not enter the while loop.
   char h;
   while(h != '3')

So read in a value into h and then do the check in the while loop.
In the moveUp function, you can use the ternary conditional operator to assign the next position or the object R.
cord->a = (cord->a)? (cord->a - 1): 2;

