Help with Struct Code: Error C2440 & C2679

  • Thread starter Rick21383
  • Start date
In summary, the conversation is about a code that is not working in C++ as it does in C. The problem seems to be related to memory allocation and pointers. The code involves creating a struct node, allocating memory for it, and assigning values to its data members. However, the code does not compile in C++ and results in error messages related to type conversions and undefined operators. The conversation also discusses the use of typedef and how it affects the code's compilation.
  • #1
Rick21383
31
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
  • #2
int main()
{
struct node{
int data;
struct node *nextPtr;
};

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

return 0;
}

-- AI
 
  • #3
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:
 
  • #4
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:
  • #5
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
 

Q1: What do error codes C2440 and C2679 mean in Struct Code?

These error codes refer to specific issues with assigning values to a struct variable. C2440 indicates a type mismatch, while C2679 indicates an operator cannot be applied to the given types.

Q2: How can I fix error code C2440 in Struct Code?

To fix C2440, make sure the type of the value being assigned matches the type of the struct variable. If necessary, use type casting to convert the value to the correct type.

Q3: What causes error code C2679 in Struct Code?

C2679 is typically caused by attempting to use an operator on two values that are not compatible. For example, using the "+" operator on a string and an integer can trigger this error.

Q4: How do I resolve error code C2679 in Struct Code?

To resolve C2679, make sure the two values being used with the operator are compatible. If necessary, use type casting to convert one of the values to a compatible type.

Q5: Are there any common mistakes that can lead to error codes C2440 and C2679 in Struct Code?

Yes, some common mistakes include forgetting to include necessary headers, using incorrect variable names, and using incompatible data types. It is important to carefully check for these types of errors when encountering these error codes.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
6
Views
920
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Computing and Technology
Replies
13
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top