-1

I'm trying to fill the fields of an array contained in a structure that is contained in another structure.

Here are the structures definitions:

struct Team
{
    std::string name;
    int position;
    int **matches;
};
struct Championship
{
    Team *teams;
    unsigned int size;
};

Then, I allocate the memory in a function.

Championship *createChampions(unsigned int n)
{
    int i;
    Championship *ptrcamp;
    ptrcamp = new Championship;
    ptrcamp -> size;
    ptrcamp -> teams = new Team [n];
    ptrcamp -> teams -> name;
    ptrcamp -> teams -> position;
    ptrcamp -> teams -> matches = new int *[2];
    for (i = 0; i < 2; i++)
    {
        ptrcamp -> equipos -> partidos[i] = new int [n - 1];
    }
    return ptrcamp;
}`

The problems occurs when I try to save the values for each team in the "matrix" created dynamically.

void fillcamp(Championship ptrcamp, int n)
{
    int i, j, k;
    string s;
    for (i = 0; i<n; i++)
    {
        cin >> ptrcamp.teams[i].name;
        cin >> ptrcamp.teams[i].position;
        cout << ptrcamp.teams[i].name;
        cout << ptrcamp.teams[i].position;
        for (j = 0; j < 2; j++) // With this I pretend to fill each column.
        {
            for (k = 0; k < n - 1; k++)// In this step I tried to fill the matrix
            {
                ptrcamp.teams[i].*(*(matches + k) + j) = -1;
            }
        }

    }
}

So the Compiler says:

> Campeonato.cpp(52): error : identifier "matches" is undefined
1>                  ptrcamp.teams[i].*(*(matches + k) + j) = -1;

I tried everything, the fact is that using the traditional *var[n] notation its not permited. Instead, I can use *(var+n).

Thanks for the help guys.

Bobok
  • 3
  • 3

1 Answers1

0

"matches" is a field, not a variable by itself. You need to prefix it with a dot or arrow notation. In your code it looks as though you think you are doing that but you aren't; I am as confused as the compiler looking at that line of code.

Do you perhaps mean:

       *(*( ptrcamp.teams[i].matches + k) + j) = -1;

If I were you, I'd simplify that line (and give the variables clear names) to make it clearer what you really are pointing to, before dereferencing. That alone might solve your problem.

Basya
  • 1,477
  • 1
  • 12
  • 22
  • So, as far I know, dot can not be applied to pointers. In my code, why this works? Should I use arrow operator, how? I'm confussed, it's a new concept to me. – Bobok Aug 12 '17 at 22:18
  • The dot here is not on a pointer. The member, "teams" is being accessed as an array. ptrcamp.teams[i] is the i'th element in that array, of type Team – Basya Aug 13 '17 at 20:11