User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,584 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,585 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 3955 | Replies: 7
Reply
Join Date: Jul 2007
Posts: 14
Reputation: #include_rose is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
#include_rose #include_rose is offline Offline
Newbie Poster

warning:Warning: assignment from incompatible pointer type

  #1  
Oct 31st, 2007
I keep getting this warning every time I compile the following code.
Can anyone please tell me what I am doing wrong.
Also, the printf(head->data) keeps saying that printf's argument is invalid. How do I print head's data?
Any help is appreciated.
Thanks,


#include<stdio.h>
#include<malloc.h>

typedef struct
{
  int data;
  struct element *next;
}node;

 main()
{
  node *head;
  insert(&head);
  printf(head->data);
}


 insert(node **head)
{
  node *newElem;
  newElem= (node *)malloc (sizeof(node));
  if(!newElem)
  {
    return 0;

  }
 newElem->next=head;
 head=newElem;
 head->data=45;
  return 1;
}


AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: warning:Warning: assignment from incompatible pointer type

  #2  
Oct 31st, 2007
>printf(head->data);
printf takes a string as the first argument, not an integer. You probably just forgot to add the format string, which would make the line look like this:
  1. printf("%d\n", head->data);

If you don't mind, I'll make a few unrelated comments:

>#include<malloc.h>
malloc.h isn't a standard header, and it's certainly not the one you should be including for malloc. Include stdlib.h instead.

>main()
The best definition of main is:
  1. int main ( void )
  2. {
  3. /* Your code here */
  4.  
  5. return 0;
  6. }
This guarantees that you'll be able to compile now and in the forseeable future. Also, when you omit the return type, it's int. Just because you didn't say int explicitly doesn't mean you can omit the actual return value too.

>insert(node **head)
Once again, be sure to be explicit about your return types.

>newElem= (node *)malloc (sizeof(node));
This isn't built for maintenance. If you decide to change the name of the structure ("node" is surprisingly popular, and C doesn't handle naming conflicts very well), you have to change each and every occurrence of it in your code. You can get rid of the specific type entirely by doing this:
  1. newElem = malloc ( sizeof *newElem );
The cast isn't required in C, and because sizeof doesn't evaluate the operand, you can dereference the pointer all you want and nothing bad will happen. The plus side is that this trick is an easy way of getting the size of the thing a pointer points to without knowing ahead of time.
Last edited by Ptolemy : Oct 31st, 2007 at 5:13 pm.
Reply With Quote  
Join Date: Jul 2007
Posts: 14
Reputation: #include_rose is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
#include_rose #include_rose is offline Offline
Newbie Poster

Re: warning:Warning: assignment from incompatible pointer type

  #3  
Oct 31st, 2007
Thank u so much, I feel like a dumbass for writing such bad code
Reply With Quote  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: warning:Warning: assignment from incompatible pointer type

  #4  
Oct 31st, 2007
>I feel like a dumbass for writing such bad code
It's not that bad...
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: warning:Warning: assignment from incompatible pointer type

  #5  
Nov 1st, 2007
I hope that your struct was just missing the element part:
typedef struct element
{
  int data;
  struct element *next;
} node;

You aren't likely to compile properly if you use struct element without first tagging it...

Hope this helps.
Reply With Quote  
Join Date: Jul 2007
Posts: 14
Reputation: #include_rose is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
#include_rose #include_rose is offline Offline
Newbie Poster

Re: warning:Warning: assignment from incompatible pointer type

  #6  
Nov 1st, 2007
Originally Posted by Duoas View Post
I hope that your struct was just missing the element part:
typedef struct element
{
  int data;
  struct element *next;
} node;

You aren't likely to compile properly if you use struct element without first tagging it...

Hope this helps.

That's weird because my program is working even without doing it.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: warning:Warning: assignment from incompatible pointer type

  #7  
Nov 1st, 2007
> That's weird because my program is working even without doing it
Seems to me that you're compiling your C code with a C++ compiler then.
Try renaming your prog.cpp file to be prog.c then see what happens.
Reply With Quote  
Join Date: Jul 2007
Posts: 14
Reputation: #include_rose is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
#include_rose #include_rose is offline Offline
Newbie Poster

Re: warning:Warning: assignment from incompatible pointer type

  #8  
Nov 1st, 2007
Originally Posted by Salem View Post
> That's weird because my program is working even without doing it
Seems to me that you're compiling your C code with a C++ compiler then.
Try renaming your prog.cpp file to be prog.c then see what happens.

My bad, I guess when I copy pasted the code it somehow got lost.

I have it in my program. Thanks guys!!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 6:30 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC