Help using a struct

  • Thread starter Rick21383
  • Start date
28
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:
644
1
int main()
{
struct node{
int data;
struct node *nextPtr;
};

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

return 0;
}

-- AI
 

Hurkyl

Staff Emeritus
Science Advisor
Gold Member
14,845
17
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)

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:
 
28
37
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:
644
1
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
 

Related Threads for: Help using a struct

  • Posted
Replies
2
Views
4K
Replies
14
Views
3K
Replies
1
Views
905
  • Posted
Replies
7
Views
2K
  • Posted
Replies
1
Views
7K
Replies
36
Views
32K
Replies
4
Views
2K
Replies
4
Views
1K

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top