![]() |
| | #1 |
| Registered User Join Date: Sep 2009
Posts: 3
| When I try to build this code, I come up with an error. Code: #include <stdio.h>
#include <stdlib.h>
struct node {
int x;
struct node *next;
};
int main()
{
/* This won't change, or we would lose the list in memory */
struct node *root;
/* This will point to each node as it traverses the list */
struct node *conductor;
root = malloc( sizeof(struct node) );
root->next = 0;
root->x = 12;
conductor = root;
if ( conductor != 0 ) {
while ( conductor->next != 0)
{
conductor = conductor->next;
}
}
/* Creates a node at the end of the list */
conductor->next = malloc( sizeof(struct node) );
conductor = conductor->next;
if ( conductor == 0 )
{
printf( "Out of memory" );
return 0;
}
/* initialize the new memory */
conductor->next = 0;
conductor->x = 42;
return 0;
}
invalid conversion from `void*' to `node*' I figured out that I could fix it by sticking (node*) in front of malloc. So my question is, should I have to do this? Is there something wrong with the tutorial or something wrong with the compiler? I'm using codeblocks 8.02 in windows xp with MinGW 5.1.4 (I'm using the latest stable versions of codeblocks and mingw) |
| Protolocke is offline | |
| | #2 |
| Registered User Join Date: Oct 2001
Posts: 2,110
| you're compiling in C++ mode. use .c, not .cpp or .c++, and use gcc, not g++. |
| robwhit is offline | |
| | #3 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > The error is in lines 16 and 27, whenever malloc is used: > invalid conversion from `void*' to `node*' It means you're using a C++ compiler to compile your C code. Name your source files so they end with .c, and make sure you're using gcc rather than g++
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #4 |
| Registered User Join Date: Sep 2009
Posts: 3
| My file does end in .c, and I'm using the GNU GCC compiler. I uninstalled MinGW and then reinstalled so that the g++ compiler option was unchecked. It turns out that codeblocks is still trying to compile using g++, because now it says "execution of 'ming32-g++.exe' has failed." I clicked on Settings->Compiler and debugger... then clicked on the "toolchain executables" tab. It does detect that there is a gcc compiler (ming32-gcc.exe) but I can't figure out how to get it to use that instead. I realise that this question should probably be posted on the Code Blocks forum, but if anyone here knows the solution that would be great. |
| Protolocke is offline | |
| | #5 | |
| Registered User Join Date: Jun 2009 Location: US of A
Posts: 300
| Quote:
root = malloc( sizeof(struct node) ); you should be doing this root = (struct node *) malloc( sizeof(struct node) ); Now you are telling it to typecast it to the desired data type. And that should solve the problem. | |
| roaan is offline | |
| | #6 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > root = (struct node *) malloc( sizeof(struct node) ); > Now you are telling it to typecast it to the desired data type. And that should solve the problem. Read the FAQ. This is an unnecessary bodge covering up deeper problems in the build setup. @OP What about compiler settings, and project settings?
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #7 |
| Registered User Join Date: Jun 2009 Location: US of A
Posts: 300
| Oops.....my fault. Thanks Salem for pointing that out. But till now i had always been casting malloc thinking it was mandatory and now i know its not. |
| roaan is offline | |
| | #8 |
| Registered User Join Date: Sep 2009
Posts: 3
| There's nothing in the compiler settings that will help, and I can't find project settings. Thanks for the help though guys |
| Protolocke is offline | |
| | #9 |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| Before ANSI C introduced the void * generic pointer, these casts were required because older compilers used to return a char pointer. But under new standards these casts are no longer necessary as a void pointer can be assigned to any pointer. These casts are still required in C++ however.
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition |
| BEN10 is offline | |
![]() |
| Tags |
| casting, conversion, linked lists, malloc, tutorial |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| singly linked circular list | DarkDot | C++ Programming | 0 | 04-24-2007 08:55 PM |
| Linked Lists tutorial | BunkFace | C Programming | 2 | 01-04-2006 03:59 AM |
| Malloc and calloc problem!! | xxhimanshu | C Programming | 19 | 08-10-2005 05:37 AM |
| Problem with pointers and linked lists | bsimbeck | C Programming | 2 | 02-21-2003 11:05 AM |
| A problem with my linked lists test. | valar_king | C Programming | 3 | 10-10-2001 12:03 PM |