Help with Struct Code: Error C2440 & C2679

  • Thread starter Thread starter Rick21383
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around issues encountered while compiling C code in a C++ environment, specifically focusing on error messages related to type conversion and memory allocation in structures. Participants explore the differences between C and C++ regarding memory management and typecasting.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a code snippet that generates errors related to type conversion from 'void*' to 'struct node*', indicating a need for an explicit cast.
  • Another participant suggests a corrected version of the code, using a pointer to 'struct node' and applying a cast to the result of 'malloc'.
  • A participant explains that the original code allocates space for a 'struct node' but does not assign it correctly because the variable is not a pointer.
  • One participant notes that the code works in C but not in C++, highlighting the differences in how the two languages handle typecasting and memory allocation.
  • Another participant humorously acknowledges a mistake regarding the use of 'typedef' and the correct typecasting syntax in C.
  • There is a mention that the original code was opened as a .C file and worked fine, but when copied to a .cpp file, it generated errors, raising questions about the compatibility of C code in C++.

Areas of Agreement / Disagreement

Participants generally agree on the issues related to typecasting and memory allocation in C versus C++. However, there are differing opinions on the implications of these differences and how to resolve the compilation errors.

Contextual Notes

The discussion highlights limitations in understanding the nuances between C and C++ regarding typecasting and memory management, as well as the potential for confusion when transitioning code from one language to the other.

Rick21383
Messages
31
Reaction score
34
Could someone tell me what it wrong with this code? It seems to be identical to the code in a book I'm using but I get the error message with the line I've colored red:

error C2440: '=' : cannot convert from 'void *' to 'struct main::node *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast

int main()
{

struct node{
int data;
struct node *nextPtr;
};

struct node newPtr;
newPtr = malloc( sizeof( struct node ) );

return 0;
}

edit:After including <stdlib.h> which I realized I need for the memory allocation, I get a new error message:

error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'void *' (or there is no acceptable conversion)

I'm using Visual C++ 6.0 if that makes any difference
 
Last edited:
Computer science news on Phys.org
int main()
{
struct node{
int data;
struct node *nextPtr;
};

struct node *newPtr;
newPtr = (node*)malloc( sizeof( struct node ) );

return 0;
}

-- AI
 
struct node newPtr;

This statement already allocates space for a "struct node".

malloc( sizeof( struct node ) );

This statement allocates memory, and returns a pointer to the allocated memory memory. (a void pointer, specifically)

newPtr

This variable is not a pointer, thus you cannot assign to it a memory address address.

(node*)malloc( sizeof( struct node ) ;

In (ANSI) C, node isn't a type, so this should fail. You meant "(struct node*)", but I know you knew that. :smile:
 
Ok, apparently the code I'm trying to compile does not work in C++. I found the CD for the book I'm using, and opened up the source code. It opened up as a .C file and worked fine. However, when I created a new source file (in Visual C++) it created a .cpp file. I copied over the code and I got the error I originally posted. I don't understand why this won't compile as a .cpp file. I thought C was essentially a subclass of C++. Here's the code in it's entirety:

/* Fig. 12.3: fig12_03.c
Operating and maintaining a list */
#include <stdio.h>
#include <stdlib.h>

struct listNode { /* self-referential structure */
char data;
struct listNode *nextPtr;
};

typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;

void insert( ListNodePtr *, char );
char delete( ListNodePtr *, char );
int isEmpty( ListNodePtr );
void printList( ListNodePtr );
void instructions( void );

int main()
{
ListNodePtr startPtr = NULL;
int choice;
char item;

instructions(); /* display the menu */
printf( "? " );
scanf( "%d", &choice );

while ( choice != 3 ) {

switch ( choice ) {
case 1:
printf( "Enter a character: " );
scanf( "\n%c", &item );
insert( &startPtr, item );
printList( startPtr );
break;
case 2:
if ( !isEmpty( startPtr ) ) {
printf( "Enter character to be deleted: " );
scanf( "\n%c", &item );

if ( delete( &startPtr, item ) ) {
printf( "%c deleted.\n", item );
printList( startPtr );
}
else
printf( "%c not found.\n\n", item );
}
else
printf( "List is empty.\n\n" );

break;
default:
printf( "Invalid choice.\n\n" );
instructions();
break;
}

printf( "? " );
scanf( "%d", &choice );
}

printf( "End of run.\n" );
return 0;
}

/* Print the instructions */
void instructions( void )
{
printf( "Enter your choice:\n"
" 1 to insert an element into the list.\n"
" 2 to delete an element from the list.\n"
" 3 to end.\n" );
}

/* Insert a new value into the list in sorted order */
void insert( ListNodePtr *sPtr, char value )
{
ListNodePtr newPtr, previousPtr, currentPtr;

newPtr = malloc( sizeof( ListNode ) );

if ( newPtr != NULL ) { /* is space available */
newPtr->data = value;
newPtr->nextPtr = NULL;

previousPtr = NULL;
currentPtr = *sPtr;

while ( currentPtr != NULL && value > currentPtr->data ) {
previousPtr = currentPtr; /* walk to ... */
currentPtr = currentPtr->nextPtr; /* ... next node */
}

if ( previousPtr == NULL ) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else {
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
}
}
else
printf( "%c not inserted. No memory available.\n", value );
}

/* Delete a list element */
char delete( ListNodePtr *sPtr, char value )
{
ListNodePtr previousPtr, currentPtr, tempPtr;

if ( value == ( *sPtr )->data ) {
tempPtr = *sPtr;
*sPtr = ( *sPtr )->nextPtr; /* de-thread the node */
free( tempPtr ); /* free the de-threaded node */
return value;
}
else {
previousPtr = *sPtr;
currentPtr = ( *sPtr )->nextPtr;

while ( currentPtr != NULL && currentPtr->data != value ) {
previousPtr = currentPtr; /* walk to ... */
currentPtr = currentPtr->nextPtr; /* ... next node */
}

if ( currentPtr != NULL ) {
tempPtr = currentPtr;
previousPtr->nextPtr = currentPtr->nextPtr;
free( tempPtr );
return value;
}
}

return '\0';
}

/* Return 1 if the list is empty, 0 otherwise */
int isEmpty( ListNodePtr sPtr )
{
return sPtr == NULL;
}

/* Print the list */
void printList( ListNodePtr currentPtr )
{
if ( currentPtr == NULL )
printf( "List is empty.\n\n" );
else {
printf( "The list is:\n" );

while ( currentPtr != NULL ) {
printf( "%c --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
}

printf( "NULL\n\n" );
}
}

Can anyone explain why this won't compile in C++?
 
Last edited:
Hurkyl said:
In (ANSI) C, node isn't a type, so this should fail. You meant "(struct node*)", but I know you knew that. :smile:
Oops! I am used to much of 'typedef'ing that it just came out of habit. :biggrin:

Rick21383 said:
/* Fig. 12.3: fig12_03.c
Operating and maintaining a list */
#include <stdio.h>
#include <stdlib.h>

<snip><snip>
newPtr = malloc( sizeof( ListNode ) );
<snip><snip>

Can anyone explain why this won't compile in C++?
Single word "typecast".

-- AI
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
11K
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
2K
  • · Replies 13 ·
Replies
13
Views
7K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K