Basic pointer casting question 
Author Message
 Basic pointer casting question

Hi,

I was wondering - is is necessary to cast pointers in a situation
such as the following?

LINK_TYPE *Get_Link_From_Links_Given_Ap(OPS_TYPE *Ap, PLAN_TYPE *plan)
{

        LINK_TYPE *link=NULL;
        LIST *links=plan->links;

        while(links) {
                link = links->link;
                if(Ap==link->Ap) {
                        puts("Found Ap in links!");
                        return(link);
                }
                links = links->next;
        }

        puts("Oh dear... I appear to have mis-calculated....");
        return(NULL);

Quote:
}

I get *loads* of
"warning: assignment from incompatible pointer type"
when I compile with gcc and I'm not sure if it's because of
pointers not being cast, or a lack of prototypes....

Thanks for any help,
Iain



Sat, 10 Jan 1998 03:00:00 GMT  
 Basic pointer casting question

Quote:
>Hi,

>I was wondering - is is necessary to cast pointers in a situation
>such as the following?

Can't tell since you didn't say what's inside of the data structures!

/Mark



Sat, 10 Jan 1998 03:00:00 GMT  
 Basic pointer casting question

Quote:
>Hi,

>I was wondering - is is necessary to cast pointers in a situation
>such as the following?

>LINK_TYPE *Get_Link_From_Links_Given_Ap(OPS_TYPE *Ap, PLAN_TYPE *plan)
>{

>        LINK_TYPE *link=NULL;
>        LIST *links=plan->links;

>        while(links) {
>                link = links->link;
>                if(Ap==link->Ap) {
>                        puts("Found Ap in links!");
>                        return(link);
>                }
>                links = links->next;
>        }

>        puts("Oh dear... I appear to have mis-calculated....");
>        return(NULL);
>}

>I get *loads* of
>"warning: assignment from incompatible pointer type"
>when I compile with gcc and I'm not sure if it's because of
>pointers not being cast, or a lack of prototypes....

It's impossible to say without knowing the definitions of LINK_TYPE, LIST,
OPS_TYPE and PLAN_TYPE.

Since your code doesn't call any functions other than puts I doubt whether
warnings from it have anything to do with prototypes.

'Incompatible pointer type' warnings nearly always indicate a problem
with your code which should be corrected, *not* glossed over with
indiscriminate casts. Casts are very rarely necessary with ANSI C.

I also suggest you avoid very similar variable names such as link and links
in your code. link = links->link is a maintenance nightmare.

--
-----------------------------------------


-----------------------------------------



Sat, 10 Jan 1998 03:00:00 GMT  
 Basic pointer casting question

Quote:
>Hi,

>I was wondering - is is necessary to cast pointers in a situation
>such as the following?

>LINK_TYPE *Get_Link_From_Links_Given_Ap(OPS_TYPE *Ap, PLAN_TYPE *plan)
>{

>    LINK_TYPE *link=NULL;
>    LIST *links=plan->links;

>    while(links) {
>            link = links->link;
>            if(Ap==link->Ap) {
>                    puts("Found Ap in links!");
>                    return(link);
>            }
>            links = links->next;
>    }

>    puts("Oh dear... I appear to have mis-calculated....");
>    return(NULL);
>}

>I get *loads* of
>"warning: assignment from incompatible pointer type"
>when I compile with gcc and I'm not sure if it's because of
>pointers not being cast, or a lack of prototypes....

>Thanks for any help,
>Iain

Just look at the first assignment.

Quote:
>    LIST *links=plan->links;

If the structure element named 'links' is other than a
void pointer or a LIST pointer, then this line will and
should cause a warning.

In general, any assignment made to a structure pointer X must
(in order avoid warnings) be either a void pointer or the address
of a structure of the same type as that pointed to by X.

Otherwise you must cast the pointer and the usual cautions about
prudent casting apply.

Hope this helps.

George Varndell



Sat, 10 Jan 1998 03:00:00 GMT  
 Basic pointer casting question

: It's impossible to say without knowing the definitions of LINK_TYPE, LIST,
: OPS_TYPE and PLAN_TYPE.

: Since your code doesn't call any functions other than puts I doubt whether
: warnings from it have anything to do with prototypes.

: 'Incompatible pointer type' warnings nearly always indicate a problem
: with your code which should be corrected, *not* glossed over with
: indiscriminate casts. Casts are very rarely necessary with ANSI C.

Thanks - sorry for not responding earlier - this news server was
out of action for a while....

Here are the data structures, followed by the code again with the
warning messages placed after the lines they complain about.

The data structures are included normally as a #include file. I'd
really appreciate any advice as I'm sure it must be something simple,
but I'm not experienced enough with C to know what....

Iain

typedef struct {
        struct OPS_TYPE *Ap;    /*Action Producer*/
        struct OPS_TYPE *Ac;    /*Action Consumer*/
        int true;
        char effect[MAX_LENGTH_STRING];

Quote:
} LINK_TYPE;

typedef struct  {

        char name[MAX_LENGTH_STRING];
        struct LINK_TYPE *link;
        struct OPS_TYPE *op;
        struct PLAN_TYPE *plan;

        struct LIST *next;
        struct LIST *prev;

Quote:
} LIST;

typedef struct {
        struct LIST *actions;
        struct LIST *order;
        struct LIST *links;
        struct LIST *bindings;

Quote:
} PLAN_TYPE;

LINK_TYPE *Get_Link_From_Links_Given_Ap(OPS_TYPE *Ap, PLAN_TYPE *plan)
{

        LINK_TYPE *link=NULL;
        LIST *links=plan->links;
warning: initialization from incompatible pointer type

        while(links) {
                link = links->link;
warning: assignment from incompatible pointer type

                if(Ap==link->Ap) {
warning: comparison of distinct pointer types lacks a cast

                /*      puts("Found Ap in links!");*/
                        return(link);
                }
                links = links->next;
warning: assignment from incompatible pointer type
        }

        puts("Oh dear... I appear to have mis-calculated....");
        return(NULL);

Quote:
}



Fri, 23 Jan 1998 03:00:00 GMT  
 Basic pointer casting question

[Snip]
:>
:> typedef struct  {
[Snip]
:>         struct LIST *next;
:>         struct LIST *prev;
:> } LIST;
:>
:>
:> typedef struct {
:>         struct LIST *actions;
:>         struct LIST *order;
:>         struct LIST *links;
:>         struct LIST *bindings;
:> } PLAN_TYPE;
:>
:>
:>
:> LINK_TYPE *Get_Link_From_Links_Given_Ap(OPS_TYPE *Ap, PLAN_TYPE *plan)
:> {
:>
:>   LINK_TYPE *link=NULL;
:>   LIST *links=plan->links;
:> warning: initialization from incompatible pointer type
[Snip]

"struct LIST * links" declares 'links' as a pointer to a struct with the
_tag_ "LIST". This is not the same as the struct declared in the typedef
which has the _name_ "LIST". These two identifiers are in different name
spaces.
  Declaring your structs like

  typedef struct LIST {        /* Tag */
    struct LIST *next, *prev;  /* Tag used for self-referential data struct */
  } LIST;                      /* Name */

  typedef struct {
    /* ... */
    struct LIST *links;        /* Refer to above struct by _tag_ */
  } PLAN_TYPE;

should solve your problem. PLAN_TYPE could also be written as

  typedef struct {
    /* ... */
    LIST *links;               /* Refer to above struct by _name_ */
  }

The tag and the name not necessarily have to be the same - they're unrelated
things. So you could also use

  typedef struct ListTag {
    struct ListTag *next, *prev;
  } LIST;

Now both "struct ListTag" and "LIST" refer to the same struct type.

Regards,

  Thomas

----------------------------------------------------------------------
Swiss Federal Institute of Technology | Thomas Wolf
Software Engineering Laboratory       | EPFL-DI-LGL
Thomas Wolf (TW)                      | CH-1015 Lausanne (Suisse)

----------------------------------------------------------------------



Fri, 23 Jan 1998 03:00:00 GMT  
 Basic pointer casting question


Mon, 26 Jan 1998 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. QUESTION: Casting a Pointer to 1 of Many Possible Pointers

2. Very basic casting question

3. casting of struct element void pointer casting

4. cast to pointer question

5. cast to pointer question(correction)

6. Pointer casting question

7. pointer cast question

8. question: cast to a function pointer

9. basic question about pointers and local vars

10. QueryInterface basic question-- why the double pointer?

11. QueryInterface basic question-- why the double pointer?

12. REALLY basic question : pointers

 

 
Powered by phpBB® Forum Software