| | |
warning:Warning: assignment from incompatible pointer type
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2007
Posts: 14
Reputation:
Solved Threads: 0
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,
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,
C Syntax (Toggle Plain Text)
#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; }
>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:
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:
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:
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.
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:
c Syntax (Toggle Plain Text)
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:
c Syntax (Toggle Plain Text)
int main ( void ) { /* Your code here */ return 0; }
>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:
c Syntax (Toggle Plain Text)
newElem = malloc ( sizeof *newElem );
Last edited by Ptolemy; Oct 31st, 2007 at 5:13 pm.
I hope that your struct was just missing the element part:
You aren't likely to compile properly if you use struct element without first tagging it...
Hope this helps.
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.
•
•
Join Date: Jul 2007
Posts: 14
Reputation:
Solved Threads: 0
•
•
•
•
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.
•
•
Join Date: Jul 2007
Posts: 14
Reputation:
Solved Threads: 0
•
•
•
•
> 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.
I have it in my program. Thanks guys!!
![]() |
Similar Threads
- warning: assignment makes pointer from integer without a cast (C)
- why incompatible warnings in this code? (C)
- error: too many arguments to function `mysql_query' (C)
- Having trouble displaying an Arrays. Please help. (C)
- Pointers to struct (C++)
- structures and functions woohoo! (C++)
Other Threads in the C Forum
- Previous Thread: Matrix sorting and factorial
- Next Thread: calculating fibonacci(1000)
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






