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
--
"Share what you know. Learn what you don't."
> 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.
--
Ben.
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.
--
Eric Sosman
eso...@ieee-dot-org.invalid