Abstract Data Types / void * implementation

Here is my code:

myheader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef MYHEADER_H
#define MYHEADER_H
#define EMPTY_TOS -1
#define MIN_STACK_SIZE 5
#define FALSE 0
#define TRUE 1

struct Node;
typedef struct Node *Stack;


void *PopStack(Stack);
void *TopOfStack(Stack);
void PushStack(void *val, Stack); 
int IsEmptyStack(Stack);
int IsFullStack(Stack);

#endif 






myheader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "myheader.h"

#define EMPTY_TOS -1 
#define MIN_STACK_SIZE 5
#define FALSE 0
#define TRUE 1

struct Node
{
	void *val; 
	struct Node *next;
};

void PushStack(void *x, Stack s)
{
	struct Node *insert;

	insert = (struct Node *) malloc(sizeof(struct Node));
	
	if (insert == NULL)
	       printf("Out of memory space!\n");
	else
	{
		   insert->val = x;
		   insert->next= s->next;
		   s->next = insert;
	}

}

void *PopStack(Stack s)
{
	struct Node *remove;
	void *val;
	
    if (IsEmptyStack(s))
           printf("The stack is empty!\n");
	else
	{
           remove = s->next;
           val=s->next->val;
		   s->next = s->next->next;

		   free(remove);
	}
    return val;
}

void *TopOfStack(Stack s)
{
	if (!IsEmptyStack(s))
		   return s->next->val;
    else
    {
           printf("The stack is empty\n");
           return 0;
    }

}

int IsEmptyStack(Stack s)
{
	return (s->next == NULL);
}


int IsFullStack(Stack s)
{
	return FALSE;
}


project.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <conio.h>
#include "myheader.h"                    of link list stack 
#define TRUE 1
#define FALSE 0
#define RESET 0



int main()
{
    Stack S = NULL; 
     
     int x0, x1, x2, x3, y0, y1, y2, y3;
     int startx, starty; 
     int polygonCounter;
     initwindow(800, 800);
     
     int m = 0;
     setcolor(4);
     
     while(m < 100)
     {
             while(!ismouseclick(WM_MBUTTONDOWN))
             {
                 if(ismouseclick(WM_LBUTTONDOWN))
                 {
                     getmouseclick(WM_LBUTTONDOWN, x0, y0);
                     PushStack(&x0, S);
                     PushStack(&y0, S);
                                                                                 
                         if(polygonCounter == 0)
                         {
                             startx = x0;
                             starty = y0;
                         }
                         polygonCounter++;
                                                                                 
                         while(!ismouseclick(WM_LBUTTONDOWN))
                         {
                             setcolor(4);
                             y0 = PopStack(S);
                             x0 = PopStack(S);                                                                                       
                             x1 = mousex();
                             y1 = mousey();                                                                                      
                             line(x0, y0, x1, y1);                                                                                                                   
                             PushStack(&x0, S);
                             PushStack(&y0, S);
                                                                                                                     
                             if(polygonCounter > 1)
                             {
                                 y3 = PopStack(S);
                                 x3 = PopStack(S);
                                                                                                                                
                                 y2 = PopStack(S);
                                 x2 = PopStack(S);
                                                                                                                                
                                 line(x2, y2, x3, y3);
                                                                                                                                
                                 PushStack(&x2, S);
                                 PushStack(&x2, S);
                                 PushStack(&x3, S);
                                 PushStack(&x3, S);
                             }
                                                                                                                     
                             delay(50);
                             setcolor(0);
                             line(x0, y0, x1, y1);
                                                                                                                     
                             if(ismouseclick(WM_RBUTTONDOWN))
                             {
                                 setcolor(0);
                                 y3 = PopStack(S);
                                 x3 = PopStack(S);
                                 y2 = PopStack(S);
                                 x2 = PopStack(S);                                                                                                                                                 
                                 line(x3, y3, x2, y2);                                                                                                                                                 
                                 PushStack(&x2, S);
                                 PushStack(&y2, S);                                                                                                                                                 
                                 polygonCounter--;                                                                                                                                                 
                                 clearmouseclick(WM_RBUTTONDOWN);
                             }
                             
                             if(ismouseclick(WM_MBUTTONDOWN))
                             {
                                 y0 = PopStack(S);
                                 x0 = PopStack(S);
                                 delay(50);
                                 setcolor(3);
                                 line(x0, y0, startx, starty);
                                 PushStack(&x0, S);
                                 PushStack(&y0, S);
                             }                                                              
                         }
                     }
                 }
                     
                     
                     
                     x0 = RESET;  
                     x1 = RESET;
                     x2 = RESET;
                     x3 = RESET;                                                                                                          
                     y0 = RESET;
                     y1 = RESET;
                     y2 = RESET;
                     y3 = RESET; 
                     startx = RESET;
                     starty = RESET;
                     
                     
                     setcolor(4);
                     
                     clearmouseclick(WM_LBUTTONDOWN);
                     clearmouseclick(WM_RBUTTONDOWN);
                     clearmouseclick(WM_MBUTTONDOWN);
                     m++;
    }
                     
                     system("PAUSE");
                     return 0;
}


The problem is that dev c++ saying that "invalid conversion from void* to int" for all variables which are defined as integers(Ex: x0, y3, startx...).

Could you please help me to solve my problem?

Last edited on
PopStack returns void*, yet you have a bunch of integers being assigned its return value.

BTW, format your code with [code] and [ /code] tags, not [output] tags please - it makes it much easier to read.
You could be able to do a int x = static_cast<int>(void_pointer_here); to force the conversion. It should be doable that way, since both int and void* have a size of 4.
@Kyon,

int x = static_cast<int>(void_pointer_here); did not work when i used it wtih this : PushStack(void *x, Stack s)
I can't see where S is initialised. The ADT functions all assume you can dereference S, but it's always zero.
Topic archived. No new replies allowed.