-2

So I originally wrote my program using #define since I know it happens as a preprocessor, but my teacher commented that I should use const. I tried replacing #define in my code but it just broke it and I am not sure why. I understand that const is usable like a variable right so my code should just be calling it and would get the same result no? Here is a screenshot of what my code does working and not working Here is the working:

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
//Set constant.Preprocessor for speed repace LENGTH
//With what ever number of digits you would want.
#define LENGTH 4

//Converts the input number into an array of digits
int * digitArray(int number) {
    //Creating an array for all digits of the constant length
    static int digits[LENGTH];
    //Loop through the digits and use the mod operator to get each one of them
    for(int i = LENGTH-1; i >= 0; i--) {
        digits[i] = number % 10;
        number = number / 10;
    }
    //Finally, return the array pointer
    return digits;
}

//Compares the correct number array with the guess array of digits, checking for fermi/pica hints and returns
//the number of exact matches of both arrays
int checkGuess(int *num, int *guess) {
    //Create two bool arrays to flag the digits already used
    bool usedNum[LENGTH] = {false};
    bool usedGuess[LENGTH] = {false};
    //First loop to check for number of matches
    int matches = 0;
    int used = 0;
    for(int i = 0; i < LENGTH; i++) {
        //Check if the digit is the same for both arrays at i index
        if(num[i] == guess[i]) {
            //If so there is an exact match
            usedNum[i] = true;
            usedGuess[i] = true;
            matches++;
            used++;
        }
    }

And the not working:

const int LENGTH = 4;

//Converts the input number into an array of digits
int * digitArray(int number) {
    //Creating an array for all digits of the constant length
    static int digits[LENGTH];
    //Loop through the digits and use the mod operator to get each one of them
    for(int i = LENGTH-1; i >= 0; i--) {
        digits[i] = number % 10;
        number = number / 10;
    }
    //Finally, return the array pointer
    return digits;
}

//Compares the correct number array with the guess array of digits, checking for fermi/pica hints and returns
//the number of exact matches of both arrays
int checkGuess(int *num, int *guess) {
    //Create two bool arrays to flag the digits already used
    bool usedNum[LENGTH] = {false};
    bool usedGuess[LENGTH] = {false};
    //First loop to check for number of matches
    int matches = 0;
    int used = 0;
    for(int i = 0; i < LENGTH; i++) {
        //Check if the digit is the same for both arrays at i index
        if(num[i] == guess[i]) {
            //If so there is an exact match
            usedNum[i] = true;
            usedGuess[i] = true;
            matches++;
            used++;
        }
    }

I know this is a kind of a repeat of some other questions but I didn't see any answers that specifically address why it does not work in this instances and how to fix it so the code runs.

  • Does this answer your question? [In C, why can't a const variable be used as an array size initializer?](https://stackoverflow.com/questions/44267827/in-c-why-cant-a-const-variable-be-used-as-an-array-size-initializer) – DarthQuack Feb 20 '21 at 04:08
  • 1
    Yes @a.Li it kinda does but the teacher said we had to use a "class constant" so I am still confused because if I am understanding that link correctly this would never work. – BENJAMIN WEED Feb 20 '21 at 04:23
  • 1
    Your teacher doesn't know C (and likely wrongly expects it to be like C++) since `const` and `#define` are not at all interchangible in C. – R.. GitHub STOP HELPING ICE Feb 20 '21 at 04:31
  • Since it is used multiple array bounds, `#define LENGTH 4` is, I think, a pretty arguable definition. – Neil Feb 20 '21 at 06:07

1 Answers1

1

Your second snippet is not using a constant value known at compile time, note that const doesn't really mean constant but "read only" in C.

Switch from

const int LENGTH = 4;

to

enum { LENGTH = 4 };  // A real constant

and it should work.

Or you can leave it as is and compile with C99 or C11, then those arrays will be Variable-length-arrays (VLA's) (note that VLA's are optional in C11). But always prefer the first option when you know the number of elements before hand.

And here:

static int digits[LENGTH];

since the array is always filled in the loop, you don't need the static keyword.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • So in our requirements we are told to make the number a class constant. Is enum the same thing? We are not allowed to use global variables. but I don't think any of these are that right? – BENJAMIN WEED Feb 20 '21 at 04:15
  • a class constant? do you mean an object instead of class? yes, in `enum {LENGTH = 4 };`, `LENGTH` is constant (the compiler know his value at compile time and can not be modified) – David Ranieri Feb 20 '21 at 04:18
  • Yes the guidelines read "Your program should include a class constant representing the number of digits for the computer to include in the correct answer." and later "You are not allowed to use the break or continue statements or global variables." Which I interpreted as making it a const but if I am understanding these responses it will never work like that. – BENJAMIN WEED Feb 20 '21 at 04:21
  • I don't know what is a class constant, there is not a `class` keyword in C. Are you maybe talking about C++? – David Ranieri Feb 20 '21 at 04:22
  • This isn't helpful but I have no idea. This is first year C++ technically but we have been compiling and coding in only C. Like all the IDE we use are set to C not C++ – BENJAMIN WEED Feb 20 '21 at 04:26
  • "You are not allowed to use the break or continue statements or global variables." For what I see, you are not breaking any of these rules, but those arrays can not be `const` nor constants, otherwise you can not write on them, I don't get this requirement. – David Ranieri Feb 20 '21 at 04:30
  • Maybe it's a `C` course disguised as a `C++` course? `C` and `C++` are historically and syntactically related, but modern `C++` is very different. It would be a good thing to ask. – Neil Feb 20 '21 at 05:58