Computer Science & C Programming


C Programming

A main characteristic of the C is the use of "memory address" called "pointers" illustrated below:

#include <stdlib.h>

char* create() {
  char* cells = malloc(DIM*DIM); // Allocate/reserve memory
  for (int p=0; p<DIM*DIM; p++)
    *(cells+p) = ' ';            // Access location
  return cells;                  // Result
}

void play(char* cells, char c, int p) {
  *(cells+p) = c; // OR: cells[p] = c;
}

void play2(char* cells, char c, int line, int col) {
  play(cells,c,DIM*line+col);
}

void print(char* cells) {
  for (int p=0; p<DIM*DIM; p++) {
    printf("%c",cells[p]);
    if (((p+1)%DIM)==0) printf("\n");
  }
}

void main() {
  char* cells = create();
  play2(cells,'X',0,1);
  print(cells);
}

NB. It can be interesting "type synonyms", eg.:

typedef char* CELLS;

CELLS create() { }
void play(CELLS cells, char c, int p) {}