assignment from incompatible pointer type

B

Bart Vandewoestyne

Going through someone else's code, i came across the following:

typedef struct {
...
double *local_Cp;
...
double **localC;
...
} MediaInfo;

...

MediaInfo *media = <some-initialization>;

media->localC = media->local_Cp;

gcc gives me a warning:

warning: assignment from incompatible pointer type

Coming from the Fortran 95 world, I'm not too familiar with pointers yet...
but I think the original author of the code made a mistake here, and it
should probably be fixed as

media->localC = &(media->local_Cp);

Am I correct? Or could the way the original author has written the code be
really intentional?

Thanks,
Bart
 
B

Ben Bacarisse

Bart Vandewoestyne said:
Going through someone else's code, i came across the following:

typedef struct {
...
double *local_Cp;
...
double **localC;
...
} MediaInfo;

...

MediaInfo *media = <some-initialization>;

media->localC = media->local_Cp;

gcc gives me a warning:

warning: assignment from incompatible pointer type

This is a required diagnostic. gcc must say something about it and this
message is a reasonable shorthand.
Coming from the Fortran 95 world, I'm not too familiar with pointers yet...
but I think the original author of the code made a mistake here, and it
should probably be fixed as

media->localC = &(media->local_Cp);

Am I correct?

It's not possible to tell from the information presented. Your line is
correct as far as the types go but it does something entirely
different. If the /effect/ of the first form is the correct one, then
your type-correct alternative will break the code.
Or could the way the original author has written the code be
really intentional?

The best I can say is that it looks wrong. The effect of the code
(after forcing the compiler to complain) will almost certainly be as if
you had written:

media->localC = (void *)media->local_Cp;

which won't produce a warning but looks equally wrong. Only by
analysing what the two pointer are used for can you tell what should
have been written.
 
E

Eric Sosman

Going through someone else's code, i came across the following:

typedef struct {
...
double *local_Cp;
...
double **localC;
...
} MediaInfo;

...

MediaInfo *media =<some-initialization>;

media->localC = media->local_Cp;

gcc gives me a warning:

warning: assignment from incompatible pointer type

Right. Pointers in C have types; different types of pointers
aim at different types of targets. The local_Cp element is a
pointer to `double', and can legitimately aim at a `double' object
(or can be null). The localC element is a pointer to `double*',
that is, a pointer to another pointer that in turn can aim at a
`double':

local_Cp ----> [ 3.14159 ]

localC ----> [ double* ] ----> [ 2.71828 ]

The code tries to aim localC at a `double' (the same `double'
local_Cp aims at), but since localC can only target `double*'
objects, not `double' objects, the assignment is incorrect.
Coming from the Fortran 95 world, I'm not too familiar with pointers yet...
but I think the original author of the code made a mistake here, and it
should probably be fixed as

media->localC =&(media->local_Cp);

Am I correct?

You're correct in that the r.h.s. is now the proper type for
the l.h.s. to aim at. It's impossible to tell from the snippet
whether that's what it *should* aim at.
Or could the way the original author has written the code be
really intentional?

It was almost certainly intended, but the intent was almost
certainly mistaken. In exactly what way, I can't tell.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top