Mohammad Kholghi
Mohammad Kholghi

Reputation: 773

Get Char Array of n elements in c

Is there a way to get an array of exactly n elements as a function input? (I know I can use an if statement to check the input, but I need a "live warning" or a "compile error".)

Something like:

void getInt(int number /*This number must be exactly four digits, to be printed on four 7-segments*/){

}

void getCh(char *names /*exactly 4 chars*/){
    
}

int main(){
    char boy[5] = "David";
    char girl[4] = "Jane";
    getCh(boy); //I need a compile error here (Or any other run-time warnings)
    getCh(girl); //"Jane" is 4 characters. OK.
    int num = 40, num2 = 12000;
    getInt(num);
    getInt(num2); //I need a warning/compile error here.
    return 0;
}

Is there any feature? Like macros, pointers, etc.

Upvotes: 0

Views: 342

Answers (3)

4386427
4386427

Reputation: 44274

Disclamer: For getCh this answer expect the input to be a valid C-style string. I have asked OP to clarify whether this is a valid assumption. I'll update the answer when/if OP clarifies this.

There is no way to get a compiler warning/error for this. All you can do is to implement your own run-time check.

To generate a run-time warning do:

void getInt(int number)
{
    // Check that input is a 4 digit int
    if (number < 1000 || number > 9999)
    {
        puts("Warning: Illegal call");
        return;
    }

    ....
}

void getCh(char *names)
{
    // Check that input length is 4
    if (strlen(names) != 4)
    {
        puts("Warning: Illegal call");
        return;
    }

    ....    
}

To generate a run-time error (program halt) do:

void getInt(int number)
{
    assert(number >= 1000 && number <= 9999);

    ....
}

void getCh(char *names)
{
    assert(strlen(names) == 4);

    ....    
}

BTW:

Your arrays are to short for the initializers:

char boy[5] = "David"; --> char boy[6] = "David";
char girl[4] = "Jane"; --> char girl[5] = "Jane";

or better - leave the size-stuff to the compiler (then you wont get it wrong)

char boy[] = "David";
char girl[] = "Jane";

EDIT

If you don't like using strlen, you can check the first 5 chars one-by-one. For instance:

assert(names[0] != '\0' &&
       names[1] != '\0' &&
       names[2] != '\0' &&
       names[3] != '\0' &&
       names[4] == '\0');

Upvotes: 4

M.M
M.M

Reputation: 141554

You can get a compile error for other sizes by having:

#define getCh(x) (_Static_assert(sizeof(x) == 4, "Size is not 4"), getCh(x))

Note that this only works when you supply an array as the argument, it doesn't work for pointers.

For added safety it is possible to have the macro reject pointers at compile time, see Array-size macro that rejects pointers .

Upvotes: 1

user15773347
user15773347

Reputation:

Both boy and girl are wrong defined, it should be:

char boy[6] = "David";
char girl[5] = "Jane";

I need a warning/compile error here.

You could have a look at signals to build your own error logic. https://man7.org/linux/man-pages/man0/signal.h.0p.html

Upvotes: 1

Related Questions