0

I have a custom structure called pair, which has two arguments: int winner, and int loser

typedef struct
{
    int winner;
    int loser;
}
pair;

I also have an array of pairs called pairs, but when I attempt to add a pair to the array, it says,

tideman.c:157:37: error: expected expression
                pairs[pair_count] = {i, j};

Is there a problem with using curly bracket notation for adding structures to arrays in C? If I make a variable for adding pairs, will it change all the pairs in the array every time I add a new pair (due to mutability problems)?

    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; i++)
        {
            if (preferences[i][j] > preferences[j][i])
            {
                pairs[pair_count] = {i, j};
            }
        }
    }
0

1 Answer 1

3

{i, j} would only work in an initialization (pair x = {i, j};). pairs[pair_count] = {i, j}; would be an assignment, and there you need a variable of type pair as the right-hand side. The most straightforward way to obtain one would be to use a compound literal pairs[pair_count] = (pair){i, j};. Alternatively, you can assign to each member separately (pairs[pair_count].winner=i; pair[pair_count].loser=j;).

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.