0

sorry if I ask a question that has been done many times before, but i have found no solution.

I have this typedef struct

typedef struct                          
    {
    int matrix[row][col];
    }Position;

And i create a variable

Position park[weeks];

then i call it in a subroutine

void foo(struct Position *park[weeks]...)

and then

park[s].matrix[i][j]=car[k].id;

and i have the "request for etc etc" error, in this line above. What am I doing wrong? Sorry for my english.

2 Answers2

4

Change

park[s].matrix[i][j]=car[k].id;

to

park[s]->matrix[i][j]=car[k].id;

You've declared parks as an array of pointers to Position, so you have to use the operator -> to access the matrix member.

Alternately, you could write

(*park[s]).matrix[i][j]=car[k].id;

but the -> operator is a little cleaner.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

just delete the * in you foo. Then everything will be ok

void foo( Position *park[weeks]...)

==>

void foo(Position park[weeks]...)

Or

void foo(Position *park,...)

Like this:

typedef struct                          
{
    int matrix[2][2];
}Position;


void foo(Position *park)
{
    park[1].matrix[1][1]=5;
}

int main()
{
    Position park[2];
    foo (park);
    return 0;
}

I don't understand why typedef struct should put in main, however , if you have to , maybe you can put the function in the main too :

int main()
{
    typedef struct                          
    {
    int matrix[2][2];
    }Position;

    void foo(Position *park)
    {
    Position *park1 = (Position *)park;
    park1[1].matrix[1][1]=5;
    };
    Position park[2];
    foo (park);
    printf("%d\n",park[1].matrix[1][1]);
    return 0;
}
Lidong Guo
  • 2,817
  • 2
  • 19
  • 31
  • Already try this, gave me the error "dereferencing pointer to incomplete type" – FabriNeral Aug 20 '13 at 11:19
  • @user2699575 I think you need delete the `*` in you `foo` , If you want use variable `park` – Lidong Guo Aug 20 '13 at 11:25
  • @user2699575: `struct Position` and `Position` are two different types in C, the former of which doesn't exist in your code. – Medinoc Aug 20 '13 at 12:06
  • Hi @LidongGuo. If i remove struct i get the error "unknown type name", i think because the declaration of the type is in the main and is not globally visible. The problem is that in the declaration of the type i have to put a couple variables that are calcolated on run time, and for this i can't declare the new type struct out of the main. – FabriNeral Aug 20 '13 at 15:55
  • @FabriNeral you can try put the foo in the main too. – Lidong Guo Aug 20 '13 at 22:42