0

In my code the following function exists:

int Count_border(int loc[], int search[], int search_c){
    int count = 0, i, j;


    for(j = -1; j < 2; j += 2){
        if(In_array(BOARD[loc[0] + j][loc[1]], search, search_c) == 1) count++;
    }
    for(j = -1; j < 2; j += 2){
        if(In_array(BOARD[loc[0]][loc[1] + j], search, search_c) == 1) count++;
    }


    return count;
}

In this function I am searching for values in the array search. How it is done doesn't matter for this question. My question is however, how can I input a "manual" array, like this: Count_border(con_input, {-1, 0, 1}, 3);

This syntaxis isn't allowed by the compiler. And I don't want to create an array before the function, I really want to hardcode it.

Thank you for your help!

EDIT:

Now I am getting this error:

In function 'main':
file.c:40:1: error: expected ';' before '}' token
 }
 ^
file.c:85:1: error: expected declaration or statement at end of input
 }

Where this is my whole code, PLEASE help me out.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void Put(int letter, int number);
void Set(int letter, int number, int who);
void Init_board();
int Count_border(int loc[], int search[], int search_c);
int In_array(int val, int arr[], int size);

int BOARD[9][9]; // 0 = empty, 1 = me, 2 = enemy
int STONES[2][81][9][9], STONE_COUNTER[2];

int main(void){
    char input[5];
    int con_input[2], t;

    Init_board();
    memset(STONES, 0, sizeof(STONES));
    memset(STONE_COUNTER, 0, sizeof(STONE_COUNTER));
    scanf("%s", input);
    if(strcmp(input,"Start") == 0){
        Put(4, 4);
    }

    scanf("%s", input); //get the first input after start

    do{
        con_input[0] = input[0]-'a';        /* Convert the input */
        con_input[1] = input[1];
        Set(con_input[0], con_input[1], 2);

        t = Count_border(con_input, (int[]){-1, 0, 1}, 3);
        printf("%i\n", t);

        scanf("%s", input);                 /* Get the next input */
    } while(strcmp(input, "Quit") != 0)


}

void Init_board(){
    int i,j;
    memset(BOARD, -1, sizeof(BOARD));
    for(i = 0; i < 9; i++){
        for(j = 0; j < 9; j++){
            BOARD[i][j] = 0;
        }
    }
}

void Put(int letter, int number){
    char t = letter + 'a';
    printf("%c%i\n", t, number);
    //fflush(stdout);
    Set(letter, number, 1);
}

void Set(int letter, int number, int who){
    BOARD[letter][number] = who;
}

int Count_border(int loc[], int search[], int search_c){
    int count = 0, i, j;


    for(j = -1; j < 2; j += 2){
        if(In_array(BOARD[loc[0] + j][loc[1]], search, search_c) == 1) count++;
    }
    for(j = -1; j < 2; j += 2){
        if(In_array(BOARD[loc[0]][loc[1] + j], search, search_c) == 1) count++;
    }


    return count;
}

int In_array(int val, int arr[], int size){
    int i;
    for (i=0; i < size; i++) {
        if (arr[i] == val)
            return 1;
    }
    return 0;
}



/* notes:
fflush(stdout);

*/

2 Answers 2

3

If you have a C99 (or newer) compiler just do

Count_border(con_input, (int[]){-1, 0, 1}, 3);

this (int[]){ something } is called a compound literal in C jargon and defines a temporary object (here an int array with 3 elements) that you can pass to your function.

Sign up to request clarification or add additional context in comments.

4 Comments

Because I am joining in a contest, and there's no C99 allowed
@GenieKort: It works in C99 and C11; it doesn't work in C89/C90, which is all that's available from a well-known compiler, o/s and office software vendor.
@GenieKort, and you are sure that by no C99 allowed they mean C11? Would be interesting to know who is so progressive.
Well, actually I don't know much about the C compiler versions ;). Where is a good explanation found?
1

Something like this?

#include <stdio.h>

void f(char arr[]);

int main(int argc, char *argv[])
{
        f((char [4]){'1', '2', '3', '5'});

        return 0;
} 

void f(char arr[4])
{
        int i;
        for (i = 0; i < sizeof(arr)/sizeof(*arr); i++)
                printf("%c ", arr[i]);
        putchar('\n');
}

5 Comments

You should explain that you are using a C99/C11 compound literal. It would also be worth thinking about what would happen with the code if the array size wasn't 4 -- or how you might work around that as a problem.
I thought that compound literals are used for so long that it is hard to find a place where they wouldn't work. But you are right. As of array size, I do not know if this is a good solution (see update). What do you think?
You haven't had much to do with Windows compilers from Microsoft, then...they still don't support C99 features such as compound literals (in fact, they don't properly support most of C99).
As to your rewrite, it will work by coincidence on 32-bit systems, but fails on 64-bit systems because sizeof(arr) is the same as sizeof(char *) which is 8 instead of 4. You probably need to pass the size as a parameter: void f(int n, char arr[n]) { for (size_t i = 0; i < n; i++) printf("%c ", arr[i]); putchar('\n'); }, and then use f(4, (char[]){'1', '2', '3', '5'});. Getting around the manual sizing is tricky. One option is ugly: #define ARG_ARRAY ((char []){'1', '2', '3', '5'}) plus f(sizeof(ARG_ARRAY)/sizeof(ARG_ARRAY[0]), ARG_ARRAY); but ugly is the operative term.
I was sure that specifying number of elements in function argument should work the same way as in declaration, because compiler have information about number of elements. Sprinkle ashes upon my head.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.