Solved

warning: assignment from incompatible pointer type

Posted on 2006-11-20
9
1,104 Views
Last Modified: 2010-04-15
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
Comment
Question by:rrahulgupta
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17983111
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17983158
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
 
LVL 22

Expert Comment

by:grg99
ID: 17986522
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:rrahulgupta
ID: 17988883
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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17989420
Could you repost the code and tell us which is line 22 please.

Paul
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 17989468
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
 

Author Comment

by:rrahulgupta
ID: 17989597
Thanks to you all.
I got the point.
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17991928
>>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
 
LVL 53

Expert Comment

by:Infinity08
ID: 17993635
>> 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Ho to creat split-view application in iPad that display PDF file?? 3 155
C hashtable library 3 93
C language IDE – Compilers installation 14 71
outlook office 365 8 122
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

823 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question