Help with Struct Code: Error C2440 & C2679

  • Thread starter Thread starter Rick21383
  • Start date Start date
AI Thread Summary
The discussion centers on resolving compilation errors in C++ related to struct and memory allocation. The initial error, C2440, arises from attempting to assign a void pointer from malloc to a non-pointer struct variable. After correcting the pointer declaration, a new error, C2679, indicates that the assignment operator is not defined for the types involved. The user discovers that the code works in C but not in C++, highlighting the need for explicit typecasting in C++. The conversation emphasizes the differences in handling pointers and memory allocation between C and C++.
Rick21383
Messages
31
Reaction score
37
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
 
This week, I saw a documentary done by the French called Les sacrifiés de l'IA, which was presented by a Canadian show Enquête. If you understand French I recommend it. Very eye-opening. I found a similar documentary in English called The Human Cost of AI: Data workers in the Global South. There is also an interview with Milagros Miceli (appearing in both documentaries) on Youtube: I also found a powerpoint presentation by the economist Uma Rani (appearing in the French documentary), AI...
Back
Top