Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

warning: assignment from incompatible pointer type

Posted on 2006-11-20
9
Medium Priority
?
1,121 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

722 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