0

Hey guys I need some help. I'm trying to store a SCar struct into an array of Scar inside the SOwner structure, for each different SOwner, though I'm getting this error:

Incompatible types when assigning to type 'Scar' from type 'struct SCar *'

Here's some code :

typedef struct {
char name[40];
SCar cars [100];
} SOwner;

typedef struct {
char color[40];
char brand[12];
} SCar;

SOwner *ownerPTR;
SCar *carPtr

void function(){
for(i=0; i<10 ; i++){
    (ownerPtr)->cars[i] = (carPtr+i);   // Problem here <<<--
}

Is there any simple way to make this work out? Thanks

2
  • Just ownerPtr->cars[i] = carPtr[i]; Commented Nov 10, 2014 at 15:37
  • In general, if code isn't working, pare it down to the simplest example that doesn't work, which in this case would be something like int n; int *p; n=p;. Commented Nov 10, 2014 at 15:40

2 Answers 2

3

You must dereference the pointer on the right hand side to generate a value of type SCar.

Like so:

ownerPtr->cars[i] = carPtr[i];

or

ownerPtr->cars[i] = *(carPtr + i);

But the latter is just a more complicated way to write the former, so just use indexing.

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

Comments

0

you have to define an array of pointers

typedef struct {
    char name[40];
    SCar * cars [100];
} SOwner;

1 Comment

It's not necessary to touch the struct definition unless OP wants to avoid the implicit copying of SCars.

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.