Compiling C as C++

From: Craig Bumpstead (cbumpste_at_yahoo.com.au)
Date: 07/31/04


Date: 30 Jul 2004 21:17:13 -0700

Hi,

I am dissecting a piece of code from Deital & Deital on using a stack.

When I changed the source extension from C to CPP and compiled, it
then threw the following error in visual C and a similar one with GCC:

c:\program files\microsoft visual
studio6\myprojects\stack\stack.cpp(68) : error C2440: '=' : cannot
convert from 'void *' to 'struct stackNode *'
Conversion from 'void*' to pointer to non-'void' requires an explicit
cast

Could someone please help me out?
Or point me to some reference material.

Thanks,
Craig

#include <stdio.h>
#include <stdlib.h>

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

typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;

void push ( StackNodePtr *, int );

int main()
{

StackNodePtr stackPtr = NULL;

int choice = 0;
int value = 0;

while ( choice != -1 )
{
        printf("1) Push a number on the stack.\n");
        printf("-1 to Exit.\n");

        scanf( "%d", &choice );

        switch ( choice )
        {
        case 1:// push
                printf("Enter a number to push to the stack.\n");
                scanf( "%d", &value );
                push ( &stackPtr, value );
                value = 0;
                choice = 0;
                break;

        case 2://pop
                break;

        case 3: //print
                break;

        default:
                break;
        }
}

return 0;
}

void push ( StackNodePtr *topPtr, int info)
{
        StackNodePtr newPtr;

        newPtr = malloc( sizeof( StackNodePtr ) );
        /* The above line is the issue. */

        if ( newPtr != NULL )
        {
                newPtr->data = info;
                newPtr->nextPtr = *topPtr;
                *topPtr = newPtr;
        }
        else
                printf("No memory available\n");
}