Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Change magic number to macro

...

Code Block
bgColor#FFCCCC
#include <stdio.h>

#define INV_SIZE 20

typedef struct {
  int item[20INV_SIZE];
  int length;
} Inventory;

int *getStock(Inventory iv);

int main(void) {
  Inventory iv;
  size_t i;
  int *item;

  iv.length = 0;
  item = getStock(iv);

  for (i = 0; i < 20INV_SIZE; i++) {
    if (item[i] == 1) {
      printf("Almost out of stock of item: %d", i);
    }
  }
  return 0;
}

int *getStock(Inventory iv) {
  if (iv.length == 0) {
    return NULL;
  }
  else {
    return iv.item;
  }
}

...

Code Block
bgColor#ccccff
#include <stdio.h>

#define INV_SIZE 20

typedef struct {
  int item[20INV_SIZE];
  int length;
} Inventory;

int *getStock(Inventory iv);

int main(void) {
  Inventory iv;
  size_t i;
  int *item;

  iv.length = 0;
  item = getStock(iv);

  for (i = 0; i < 20INV_SIZE; i++) {
    if (item[i] == 1) {
      printf("Almost out of stock of item: %d", i);
    }
  }
  return 0;
}

int *getStock(Inventory iv) {
  return iv.item;
}

...