assignment from incompatible pointer type

Discussion in 'C Programming' started by Bart Vandewoestyne, Apr 27, 2010.

  1. 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

    --
    "Share what you know. Learn what you don't."
    Bart Vandewoestyne, Apr 27, 2010
    #1
    1. Advertising

  2. Bart Vandewoestyne <> writes:

    > 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.
    Ben Bacarisse, Apr 27, 2010
    #2
    1. Advertising

  3. Bart Vandewoestyne

    Eric Sosman Guest

    On 4/27/2010 9:14 AM, Bart Vandewoestyne wrote:
    > 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.

    --
    Eric Sosman
    lid
    Eric Sosman, Apr 27, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. zmbdcqnrdfetnws

    incompatible pointer assignment ?

    zmbdcqnrdfetnws, Dec 5, 2003, in forum: C Programming
    Replies:
    8
    Views:
    560
    Martin Ambuhl
    Dec 6, 2003
  2. Brian Stubblefield

    initialization from incompatible pointer type

    Brian Stubblefield, May 25, 2004, in forum: C Programming
    Replies:
    3
    Views:
    15,203
    Brian Stubblefield
    May 27, 2004
  3. gk245
    Replies:
    10
    Views:
    1,005
    Peter Shaggy Haywood
    Apr 22, 2006
  4. Olaf \El Blanco\

    assignment from incompatible pointer type

    Olaf \El Blanco\, Jun 10, 2006, in forum: C Programming
    Replies:
    1
    Views:
    509
    Olaf \El Blanco\
    Jun 10, 2006
  5. Prathamesh Kulkarni

    incompatible pointer assignment

    Prathamesh Kulkarni, Dec 10, 2012, in forum: C Programming
    Replies:
    7
    Views:
    314
    Keith Thompson
    Dec 10, 2012
Loading...

Share This Page