943,634 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 20559
  • C RSS
Oct 31st, 2007
0

warning:Warning: assignment from incompatible pointer type

Expand Post »
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,


  1. #include<stdio.h>
  2. #include<malloc.h>
  3.  
  4. typedef struct
  5. {
  6. int data;
  7. struct element *next;
  8. }node;
  9.  
  10. main()
  11. {
  12. node *head;
  13. insert(&head);
  14. printf(head->data);
  15. }
  16.  
  17.  
  18. insert(node **head)
  19. {
  20. node *newElem;
  21. newElem= (node *)malloc (sizeof(node));
  22. if(!newElem)
  23. {
  24. return 0;
  25.  
  26. }
  27. newElem->next=head;
  28. head=newElem;
  29. head->data=45;
  30. return 1;
  31. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
#include_rose is offline Offline
14 posts
since Jul 2007
Oct 31st, 2007
0

Re: warning:Warning: assignment from incompatible pointer type

>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.
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Ptolemy is offline Offline
62 posts
since Oct 2007
Oct 31st, 2007
0

Re: warning:Warning: assignment from incompatible pointer type

Thank u so much, I feel like a dumbass for writing such bad code
Reputation Points: 10
Solved Threads: 0
Newbie Poster
#include_rose is offline Offline
14 posts
since Jul 2007
Oct 31st, 2007
0

Re: warning:Warning: assignment from incompatible pointer type

>I feel like a dumbass for writing such bad code
It's not that bad...
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Ptolemy is offline Offline
62 posts
since Oct 2007
Nov 1st, 2007
0

Re: warning:Warning: assignment from incompatible pointer type

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 1st, 2007
0

Re: warning:Warning: assignment from incompatible pointer type

Click to Expand / Collapse  Quote originally posted by Duoas ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
#include_rose is offline Offline
14 posts
since Jul 2007
Nov 1st, 2007
0

Re: warning:Warning: assignment from incompatible pointer type

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 1st, 2007
0

Re: warning:Warning: assignment from incompatible pointer type

Click to Expand / Collapse  Quote originally posted by Salem ...
> 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!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
#include_rose is offline Offline
14 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Matrix sorting and factorial
Next Thread in C Forum Timeline: calculating fibonacci(1000)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC