• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1139
  • Last Modified:

warning: assignment from incompatible pointer type

Hi Experts,
I have written the following code.
 
// ======== struct =================
typedef struct {
   unsigned int ui_domain_id;
   unsigned int ui_proxy_id;  
   char*        pch_domain_name;
   struct       tm *ptm_last_modified;    
   void*        (* function) (void *);    
   struct       st_sip_proxy_domain *p_next;     /*List */
} st_sip_proxy_domain;

// ===== callee fuction create list=======================

void create_list(st_sip_proxy_domain **pst_sip_proxy_domain, MYSQL_RES *res) {
    MYSQL_ROW row;
         if((row = mysql_fetch_row(res)) != NULL){
      if(*pst_sip_proxy_domain!=NULL){
            while (*pst_sip_proxy_domain != NULL) {
             pst_sip_proxy_domain = &(*pst_sip_proxy_domain)->p_next;       // WARNING
           }
      }
      st_sip_proxy_domain *head;
      head = malloc(sizeof(st_sip_proxy_domain));
      head->p_next = *pst_sip_proxy_domain;                                                     // WARNING
      *pst_sip_proxy_domain = head;
      head->ui_domain_id = atoi(row[0]);
        head->ui_proxy_id = atoi(row[1]);
            head -> pch_domain_name = (char *)malloc(strlen(row[2]));
        strcpy(head -> pch_domain_name,row[2]);
            create_sip_proxy_domain_list(&head, res);
       }
}

// ======= caller fuction
st_sip_proxy_domain *return_all_sip_proxy_domain(st_sip_proxy_domain *p_sip_proxy_domain)
 {
   MYSQL_RES *res;
   st_sip_proxy_domain *head;
   // ...
   head = NULL;
   create_sip_proxy_domain_list(&head, res);    // Called

   // ...
   return head;
}

There is warning coming on the given lines as "warning: assignment from incompatible pointer type".
I tried all the ways what I can do but couldn't able to remove that warning.
Can anyone suggest, what is the problem with the code.
0
rrahulgupta
Asked:
rrahulgupta
  • 4
  • 2
  • 2
  • +1
1 Solution
 
PaulCaswellCommented:
Hi rrahulgupta,

I cant compile it here but this can often make things easier:

typedef struct st_sip_proxy_domain{ // << Added struct name here.
   unsigned int ui_domain_id;
   unsigned int ui_proxy_id;  
   char*        pch_domain_name;
   struct       tm *ptm_last_modified;    
   void*        (* function) (void *);    
   struct       st_sip_proxy_domain *p_next;     /*List */
} st_sip_proxy_domain;

If that doesnt help, post back and we'll see what else is wrong.

Paul
0
 
PaulCaswellCommented:
Hi rrahulgupta,

>>           pst_sip_proxy_domain = &(*pst_sip_proxy_domain)->p_next;      // WARNING
Try:
           pst_sip_proxy_domain = &((*pst_sip_proxy_domain)->p_next);

Perhaps also:

>>   struct       st_sip_proxy_domain *p_next;     /*List */
   st_sip_proxy_domain *p_next;     /*List */

Paul
0
 
grg99Commented:
Whew, it's alaways tricky when you have **'s.  What I'd do is have a temporary variable in the function that holds the dereferenced pointer, so you can unwrap one level of indirection.  If done right, you shouldnt have to use the ampersand or casts.  Since the next pointer is correctly declared, you shouldnt need any funny business with ampersands.

Also it's always an error to do a malloc( strlen()) as you're not allocating space for the ending \0.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rrahulguptaAuthor Commented:
Hi PaulCaswell,

when i tried first and third option suggested by you, it's trowing following errors:
___________________________________________________________
src/sip_proxy_domain.h:22: parse error before "st_sip_proxy_domain"
src/sip_proxy_domain.h:22: warning: no semicolon at end of struct or union
src/sip_proxy_domain.h:23: warning: data definition has no type or storage class
src/sip_proxy_domain.h:33: parse error before '*' token
src/sip_proxy_domain.h:33: parse error before '*' token
src/sip_proxy_domain.h:33: warning: data definition has no type or storage class
src/sip_proxy_domain.h:40: parse error before '*' token
src/sip_proxy_domain.h:40: parse error before '*' token
src/sip_proxy_domain.h:40: warning: data definition has no type or storage class
src/sip_proxy_domain.h:47: parse error before '*' token
src/sip_proxy_domain.h:54: parse error before '*' token
src/sip_proxy_domain.h:61: parse error before '*' token
src/sip_proxy_domain.c:56: parse error before '*' token
src/sip_proxy_domain.c: In function `create_sip_proxy_domain_list':
src/sip_proxy_domain.c:58: `res' undeclared (first use in this function)
src/sip_proxy_domain.c:58: (Each undeclared identifier is reported only once
src/sip_proxy_domain.c:58: for each function it appears in.)
src/sip_proxy_domain.c:59: `pst_sip_proxy_domain' undeclared (first use in this function)
src/sip_proxy_domain.c:65: `head' undeclared (first use in this function)
src/sip_proxy_domain.c: At top level:
src/sip_proxy_domain.c:82: parse error before '*' token
src/sip_proxy_domain.c:82: parse error before '*' token
src/sip_proxy_domain.c: In function `return_all_sip_proxy_domain':
src/sip_proxy_domain.c:87: `head' undeclared (first use in this function)
src/sip_proxy_domain.c: At top level:
src/sip_proxy_domain.c:110: parse error before '*' token
src/sip_proxy_domain.c:110: parse error before '*' token
src/sip_proxy_domain.c: In function `return_sip_proxy_domain':
src/sip_proxy_domain.c:115: `head' undeclared (first use in this function)
src/sip_proxy_domain.c:121: `p_sip_proxy_domain' undeclared (first use in this function)
src/sip_proxy_domain.c: At top level:
src/sip_proxy_domain.c:144: parse error before '*' token
src/sip_proxy_domain.c:256: parse error before '*' token
src/sip_proxy_domain.c:405: parse error before '*' token

make.exe: *** [objs/sip_proxy_domain.o] Error 1
Execution terminated
________________________________________________________

And with second option, same warning in coming.

Hi grg99,
I didn't really get your point about warning.
Can you explain it? please...
0
 
PaulCaswellCommented:
Could you repost the code and tell us which is line 22 please.

Paul
0
 
Infinity08Commented:
See the lines modified below ... explanation follows !

// ======== struct =================
typedef struct st_sip_proxy_domain {                                                         // <--- MODIFIED
   unsigned int ui_domain_id;
   unsigned int ui_proxy_id;  
   char*        pch_domain_name;
   struct       tm *ptm_last_modified;    
   void*        (* function) (void *);    
   struct       st_sip_proxy_domain *p_next;     /*List */
} st_sip_proxy_domain;

// ===== callee fuction create list=======================

void create_list(st_sip_proxy_domain **pst_sip_proxy_domain, MYSQL_RES *res) {
    MYSQL_ROW row;
         if((row = mysql_fetch_row(res)) != NULL){
     if(*pst_sip_proxy_domain!=NULL){
           while (*pst_sip_proxy_domain != NULL) {
           pst_sip_proxy_domain = (st_sip_proxy_domain**) &((*pst_sip_proxy_domain)->p_next);       // <--- MODIFIED
          }
     }
     st_sip_proxy_domain *head;
     head = (st_sip_proxy_domain*) malloc(sizeof(st_sip_proxy_domain));                                          // <--- MODIFIED
     head->p_next = (struct st_sip_proxy_domain*) *pst_sip_proxy_domain;                                       // <--- MODIFIED
     *pst_sip_proxy_domain = head;
     head->ui_domain_id = atoi(row[0]);
        head->ui_proxy_id = atoi(row[1]);
          head -> pch_domain_name = (char *)malloc(strlen(row[2]));
        strcpy(head -> pch_domain_name,row[2]);
          create_sip_proxy_domain_list(&head, res);
      }
}

// ======= caller fuction
st_sip_proxy_domain *return_all_sip_proxy_domain(st_sip_proxy_domain *p_sip_proxy_domain)
 {
   MYSQL_RES *res;
   st_sip_proxy_domain *head;
   // ...
   head = NULL;
   create_sip_proxy_domain_list(&head, res);    // Called

   // ...
   return head;
}



Explanation :

>> typedef struct st_sip_proxy_domain {                                                         // <--- MODIFIED
This is a better declaration of a typedef'd struct

>>            pst_sip_proxy_domain = (st_sip_proxy_domain**) &((*pst_sip_proxy_domain)->p_next);       // <--- MODIFIED
>>      head = (st_sip_proxy_domain*) malloc(sizeof(st_sip_proxy_domain));                                          // <--- MODIFIED
>>      head->p_next = (struct st_sip_proxy_domain*) *pst_sip_proxy_domain;                                       // <--- MODIFIED
casting ensures that the "warning: assignment from incompatible pointer type" warnings are gone
0
 
rrahulguptaAuthor Commented:
Thanks to you all.
I got the point.
0
 
PaulCaswellCommented:
>>casting ensures that the "warning: assignment from incompatible pointer type" warnings are gone

Remember that it can also hide mistakes so use them sparingly.

Which is line 22 and what compiler are you using?

Sorry I got in late this evening. Gotta sleep. Back tomorrow.

Paul
0
 
Infinity08Commented:
>> Remember that it can also hide mistakes so use them sparingly.
Sure, but in this case it's correct, and even necessary - I take a habit of double-checking that.
It's an awkward part of linked lists in C.

General example without all the extra code that clutters up the view :

    typedef struct st_sip_proxy_domain {
       struct st_sip_proxy_domain *p_next;
    } st_sip_proxy_domain;

This is the "easiest" way of making a linked list, but it does force you to cast from time to time (on certain compilers).
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now