473,721 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stack

Hello all.I am trying to implement a stack which can store either
integer or float values.

The code is given below:

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

#define STACKSIZE 100
#define INT 1
#define FLOAT 2
#define STRING 3
struct stackelement{
int etype; /*etype equals INT,FLOAT,STRIN G*/
/*depending on the type of the */
/*corresponding element */
union {
int iVal;
float fVal;
/* char *pVal; */
}element;
};

struct stack{
int top;
struct stackelement item[STACKSIZE];
};
void push(struct stack *p,struct stackelement *se,int n)
{
if(p->top == STACKSIZE-1 )
printf("overflo w\n");

else
{
if(se->etype == INT)
p->item[++p->top].element.iVal=n ;
else if(se->etype == FLOAT)
p->item[++p->top].element.fVal=( float)n;
/*
else if(se->etype == STRING)
p->item[++p->top].element.pVal=" T";
*/
}
return;
}

void pop(struct stack *p,struct stackelement *se)
{
if(p->top==-1)
printf("underfl ow\n");
else
{
if(se->etype == INT)
printf("deleted number is\ %d\n",p->item[p->top].element.iVal);
else if(se->etype == FLOAT)
printf("deleted number is\ %d\n",p->item[p->top].element.fVal);

--p->top;
}
return;
}
void display(struct stack *p,struct stackelement *se)
{
int i;
if(p->top == -1)
printf("stack is empty\n\n");
else
{
if(se->etype == INT)
{
for(i=p->top; i >= 0; i--)
printf("%d\n", p->item[i].element.iVal\ );
}

else
{
for(i = p->top; i >= 0; i--)
printf("%d\n", p->item[i].element.fVal);
}
}
return;
}

void menu(void)
{
/*Sub Menu
to accept the options of push /pop etc*/
}

int main(void)
{
int choice_mm ;
struct stackelement se;
struct stack s;
s.top = -1;

/*Main Menu
to accept the choices for int/float type*/

return 0;
}

I am getting the warning:
warning C4133: 'function' : incompatible types - from 'stack *' to
'stackelement *'
whenever i try to call the push /pop/display functions

push(&s,&s_elem ,num);
pop(&s,&s_elem) ;
display(&s,&s_e lem);
in the main function
Can anyone please help me out?Thanks

Jan 4 '08 #1
9 2513
In <fl**********@a ioe.orgTarique <pe*****@yahoo. comwrites:
struct stackelement se;
struct stack s;
warning C4133: 'function' : incompatible types - from 'stack *' to
'stackelement *'
whenever i try to call the push /pop/display functions
push(&s,&s_elem ,num);
pop(&s,&s_elem) ;
display(&s,&s_e lem);
in the main function
Your sample main() function contains no declarations of a variable
called "s_elem". We can't help you until we know this information.

--
John Gordon A is for Amy, who fell down the stairs
go****@panix.co m B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Jan 4 '08 #2
John Gordon wrote:
In <fl**********@a ioe.orgTarique <pe*****@yahoo. comwrites:
> struct stackelement se;
struct stack s;
> warning C4133: 'function' : incompatible types - from 'stack *' to
'stackelemen t *'
whenever i try to call the push /pop/display functions
>push(&s,&s_ele m,num);
pop(&s,&s_elem );
display(&s,&s_ elem);
in the main function

Your sample main() function contains no declarations of a variable
called "s_elem". We can't help you until we know this information.
Oops! I missed that.s_elem is defined in the "menu" function
void menu(void)
{
int choice;
int flag=1;
int num=0;
struct stack s;
struct stack s_elem;

while(flag)
{
...Menu snipped...
...case statements...
}
return ;
}
Jan 4 '08 #3
Tarique wrote:
The code is given below:
...snip...

The rest of the code (menu)is given below.
I have changed the definition of the function.The earlier one was
causing the code to fail.
void menu(struct stack *s,struct stack *s_elem)
{
int num=0;
int choice;
int flag=1;

while(flag)
{
printf("Menu");
printf("\n1.Pus h");
printf("\n2.Pop ");
printf("\n3.Dis play");
printf("\n4.Exi t");
printf("\n5.Cle ar screen\n");
printf("Enter your choice :");
scanfs("%d",&ch oice);

switch(choice)
{
case 1:
printf("Enter the number to push");
scanfs("%d",&nu m);
fflush(stdin);
push(s,s_elem,n um);/*warning*/
break;
case 2:
pop(s,s_elem); /*warning*/
break;
case 3:
display(s,s_ele m);/*warning*/
break;
case 4:
flag=0;
break;
case 5:
system("cls");
break;
default:
printf("wrong choice\n");
}
}
}

int main(void)
{
int choice_mm ;
struct stackelement se;
struct stack s;
s.top = -1;

for(;;)
{
printf("Choose the type of stack:\n");
printf("1.Integ er\n");
printf("2.Float \n");
printf("3.Strin g\n");
printf("4.Exit\ n");
scanfs("%i", &choice_mm);

switch(choice_m m)
{
case 1:
se.etype=INT;
system("cls");
menu(&s,&se);/*warning*/
system("cls");
break;
case 2:
se.etype=FLOAT;
menu(&s,&se);/*warning*/
break;
case 3:
se.etype=STRING ;
menu(&s,&se);/*warning*/
break;
case 4:
exit(0);
break;
default:
printf("wrong input");
}
}
return 0;
}
I am compiling the code with Visual c++ 2008 (express edition) (/as a c
code/)
I am really sorry for the long post but i am still getting the warnings:

warning C4133: 'function' : incompatible types - from 'stack *' to
'stackelement *'

warning C4133: 'function' : incompatible types - from 'stackelement *'
to 'stack *'

kindly Help!
Jan 4 '08 #4
In article <fl**********@a ioe.org>, Tarique <pe*****@yahoo. comwrote:
>Tarique wrote:
>void menu(struct stack *s,struct stack *s_elem)
[...]
>int main(void)
{
struct stackelement se;
struct stack s;
> menu(&s,&se);/*warning*/
The second parameter of your call is &se, which is the address
of a struct stackelement. However, your menu() routine expects
the second parameter to be struct stack *s_elem, which is the
address of a struct stack, not of a struct stackelement.
--
So you found your solution
What will be your last contribution?
-- Supertramp (Fool's Overture)
Jan 4 '08 #5
On Jan 4, 9:07*am, Tarique <peo_...@yahoo. comwrote:
Hello all.I am trying to implement a stack which can store either
integer or float values.

The code is given below:

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

#define STACKSIZE * * * 100
#define INT * * * * * * * * * * 1
#define FLOAT * * * * * 2
#define STRING * * * * *3

struct stackelement{
* * * * int etype; * * */*etype equals INT,FLOAT,STRIN G*/
* * * * * * * * * * * * * * * * /*depending on the type of the */
* * * * * * * * * * * * * * * * /*corresponding element */
* * * * union {
* * * * * * * * int * * iVal;
* * * * * * * * float * fVal;
* * * * /* * * *char * **pVal; **/
* * * * }element;

};

struct stack{
* * * * int top;
* * * * struct stackelement item[STACKSIZE];

};

void push(struct stack *p,struct stackelement *se,int n)
{
* * * * if(p->top == STACKSIZE-1 )
* * * * *printf("overfl ow\n");

* * * * else
* * * * {
* * * * * * * * if(se->etype == INT)
* * * * * * * * * * * * p->item[++p->top].element.iVal=n ;
* * * * * * * * else if(se->etype == FLOAT)
* * * * * * * * * * * * p->item[++p->top].element.fVal=( float)n;
* * * * * * * * /*
* * * * * * * * else if(se->etype == STRING)
* * * * * * * * * * * * p->item[++p->top].element.pVal=" T";
* * * * * * * * */
* * * * }
* * * * return;

}

void pop(struct stack *p,struct stackelement *se)
{
* * *if(p->top==-1)
* * * * *printf("underf low\n");
* * *else
* * *{
* * * * * * * * if(se->etype == INT)
* * * * * * * * * * * * printf("deleted number is\%d\n",p->item[p->top].element.iVal);
* * * * * * * * else if(se->etype == FLOAT)
* * * * * * * * * * * * printf("deleted number is\%d\n",p->item[p->top].element.fVal);

* * * * * * * * --p->top;
* * *}
* * *return;

}

void display(struct stack *p,struct stackelement *se)
{
* * *int i;
* * *if(p->top == -1)
* * * * *printf("stack is empty\n\n");
* * *else
* * * * {
* * * * * * * * if(se->etype == INT)
* * * * * * * * {
* * * * * * * * * * * * for(i=p->top; i >= 0; i--)
* * * * * * * * * * * * * * * * printf("%d\n", p->item[i].element.iVal\ );
* * * * * * * * }

* * * * * * * * else
* * * * * * * * {
* * * * * * * * * * * * for(i = p->top; i >= 0; i--)
* * * * * * * * * * * * * * * * printf("%d\n", p->item[i].element.fVal);
* * * * * * * * }
* * * * }
* * * * return;

}

void menu(void)
{
/*Sub Menu
to accept the options of push /pop etc*/

}

int main(void)
{
* * * * int * * choice_mm ;
* * * * struct *stackelement se;
* * * * struct *stack s;
* * * * s.top = -1;

* * * * /*Main Menu
* * * * to accept the choices for int/float type*/ * * *

* * * * return 0;

}

I am getting the warning:
* warning C4133: 'function' : incompatible types - from 'stack *' to
'stackelement *'
whenever i try to call the push /pop/display functions

push(&s,&s_elem ,num);
pop(&s,&s_elem) ;
display(&s,&s_e lem);
in the main function
Can anyone please help me out?Thanks
Your display function does not look right to me either.
Jan 4 '08 #6
Tarique wrote:
Hello all.I am trying to implement a stack which can store either
integer or float values.

The code is given below:
....snip...

Here is the final code:
Works fine in case of Integer n floats.However i lose track of the
character pointer once i exit the push function (in case of strings).
So in case of *Strings* i cannot display the popped string or the
members of the stack at any given time.
Using an extra global variable to keep track also does not help!
It's confusing!!!

PS:ive not really bothered about scanf..Will change that :)

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

#define STACKSIZE 100
#define INT 1
#define FLOAT 2
#define STRING 3

struct stackelement{
int etype; /*etype equals INT,FLOAT,STRIN G*/
/*depending on the type of the */
/*corresponding element */
union {
int iVal;
float fVal;
char *pVal;
}element;
};

struct stack{
int top;
struct stackelement item[STACKSIZE];
};

void push(struct stack *p,struct stackelement *se)
{
int test;
int iValue = 0;
float fValue = 0.0;
char cValue[10];

printf("Enter the number/character to push");
if(se->etype == INT)
scanf("%d",&iVa lue);
else if(se->etype == FLOAT)
scanf("%f",&fVa lue);
else if(se->etype == STRING)
{
while((test=get char())!='\n')
;
fgets(cValue,10 ,stdin);
}
if(p->top == STACKSIZE-1 )
printf("overflo w\n");

else
{
if(se->etype == INT)
p->item[++p->top].element.iVal=i Value;
else if(se->etype == FLOAT)
p->item[++p->top].element.fVal=f Value;
else if(se->etype == STRING)
p->item[++p->top].element.pVal=c Value;
}
return;
}

void pop(struct stack *p,struct stackelement *se)
{
if(p->top==-1)
printf("underfl ow\n");
else
{
if(se->etype == INT)
printf("Deleted Integer is\ %d\n",p->item[p->top].element.iVal);
else if(se->etype == FLOAT)
printf("Deleted Float is\ %f\n",p->item[p->top].element.fVal);
else if(se->etype == STRING)
printf("Deleted String is\ %s\n",p->item[p->top].element.pVal);
--p->top;
}
return;
}
void display(struct stack *p,struct stackelement *se)
{
int i;
if(p->top == -1)
printf("Stack is Empty\n\n");
else
{
if(se->etype == INT)
{
for(i=p->top; i >= 0; i--)
printf("%d\n", p->item[i].element.iVal );
}

else if(se->etype == FLOAT)
{
for(i = p->top; i >= 0; i--)
printf("%f\n", p->item[i].element.fVal);
}

else if(se->etype == STRING)
{
for(i = p->top; i >= 0; i--)
printf("%s\n",p->item[p->top].element.pVal);
}
/*((((*(p)).item )[0]).element).pVal */
}
return;
}

void menu(struct stack *s,struct stackelement *s_elem)
{
int num=0;
int choice;
int flag=1;

while(flag)
{
printf("Menu");
printf("\n1.Pus h");
printf("\n2.Pop ");
printf("\n3.Dis play");
printf("\n4.Exi t");
printf("\n5.Cle ar screen\n");
printf("Enter your choice :");
scanfs("%d",&ch oice);

switch(choice)
{
case 1:
push(s,s_elem);
system("cls");
break;
case 2:
pop(s,s_elem);
break;
case 3:
display(s,s_ele m);
break;
case 4:
flag=0;
break;
case 5:
system("cls");
break;
default:
printf("wrong choice\n");
}
}
}

int main(void)
{
int choice_mm ;
struct stackelement se;
struct stack s;
s.top = -1;

for(;;)
{
printf("Choose the type of stack:\n");
printf("1.Integ er\n");
printf("2.Float \n");
printf("3.Strin g\n");
printf("4.Exit\ n");
scanf_s("%i", &choice_mm);

switch(choice_m m)
{
case 1:
se.etype=INT;
system("cls");
menu(&s,&se);
system("cls");
break;
case 2:
se.etype=FLOAT;
menu(&s,&se);
break;
case 3:
se.etype=STRING ;
menu(&s,&se);
break;
case 4:
exit(0);
break;
default:
printf("wrong input");
}
}
return 0;
}
Jan 4 '08 #7
On Jan 4, 12:22*pm, Tarique <peo_...@yahoo. comwrote:
Tarique wrote:
Hello all.I am trying to implement a stack which can store either
integer or float values.
The code is given below:

...snip...

Here is the final code:
Works fine in case of Integer n floats.However i lose track of the
character pointer once i exit the push function (in case of strings).
So in case of *Strings* i cannot display the popped string or the
members of the stack at any given time.
Using an extra global variable to keep track also does not help!
It's confusing!!!

PS:ive not really bothered about scanf..Will change that :)

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

#define STACKSIZE * * * 100
#define INT * * * * * * 1
#define FLOAT * * * * * 2
#define STRING * * * * *3

struct stackelement{
int etype; * * */*etype equals INT,FLOAT,STRIN G*/
* * * * * * * * * * * * /*depending on the type ofthe */
* * * * * * * * * * * * /*corresponding element */
union {
* * * * int * * iVal;
* * * * float * fVal;
* * * * char * **pVal;

}element;
};

struct stack{
int top;
struct stackelement item[STACKSIZE];

};

void push(struct stack *p,struct stackelement *se)
{
int * * test;
int * * iValue *= 0;
float * fValue *= 0.0;
char * *cValue[10];

printf("Enter the number/character to push");
if(se->etype == INT)
* * * * scanf("%d",&iVa lue);
else if(se->etype == FLOAT)
* * * * scanf("%f",&fVa lue);
else if(se->etype == STRING)
{
* * * * while((test=get char())!='\n')
* * * * * * * * ;
* * * * fgets(cValue,10 ,stdin);

}

if(p->top == STACKSIZE-1 )
* * *printf("overfl ow\n");

else
{
* * * * if(se->etype == INT)
* * * * * * * * p->item[++p->top].element.iVal=i Value;
* * * * else if(se->etype == FLOAT)
* * * * * * * * p->item[++p->top].element.fVal=f Value;
* * * * else if(se->etype == STRING)
* * * * * * * * p->item[++p->top].element.pVal=c Value;

}
return;
}

void pop(struct stack *p,struct stackelement *se)
{
if(p->top==-1)
* * *printf("underf low\n");
else
{
* * * * if(se->etype == INT)
* * * * * * * * printf("Deleted Integer is\ %d\n",p->item[p->top].element.iVal);
* * * * else if(se->etype == FLOAT)
* * * * * * * * printf("Deleted Float is\ %f\n",p->item[p->top].element.fVal);
* * * * else if(se->etype == STRING)
* * * * * * * * printf("Deleted String is\ %s\n",p->item[p->top].element.pVal);

* * * * --p->top;

}
return;
}

void display(struct stack *p,struct stackelement *se)
{
int i;
if(p->top == -1)
* * *printf("Stack is Empty\n\n");
else
{
* * * * if(se->etype == INT)
* * * * {
* * * * * * * * for(i=p->top; i >= 0; i--)
* * * * * * * * * * * * printf("%d\n", p->item[i].element.iVal );
* * * * }

* * * * else if(se->etype == FLOAT)
* * * * {
* * * * * * * * for(i = p->top; i >= 0; i--)
* * * * * * * * * * * * printf("%f\n", p->item[i].element.fVal);
* * * * }

* * * * else if(se->etype == STRING)
* * * * {
* * * * * * * * for(i = p->top; i >= 0; i--)
* * * * * * * * * * * * printf("%s\n",p->item[p->top].element.pVal);
* * * * }
* * * * /*((((*(p)).item )[0]).element).pVal */

}
return;
}

void menu(struct stack *s,struct stackelement *s_elem)
{
int num=0;
int * * * * * * choice;
int * * * * * * flag=1;

while(flag)
{
* * * * printf("Menu");
* * * * printf("\n1.Pus h");
* * * * printf("\n2.Pop ");
* * * * printf("\n3.Dis play");
* * * * printf("\n4.Exi t");
* * * * printf("\n5.Cle ar screen\n");
* * * * printf("Enter your choice :");
* * * * scanfs("%d",&ch oice);

* * * * switch(choice)
* * * * {
* * * * * * case 1:
* * * * * * * * push(s,s_elem);
* * * * * * * * system("cls");
* * * * * * * * break;
* * * * * * case 2:
* * * * * * * * pop(s,s_elem);
* * * * * * * * break;
* * * * * * case 3:
* * * * * * * * display(s,s_ele m);
* * * * * * * * break;
* * * * * * case 4:
* * * * * * * * flag=0;
* * * * * * * * break;
* * * * * * case 5:
* * * * * * * * system("cls");
* * * * * * * * break;
* * * * * * default:
* * * * * * * * printf("wrong choice\n");
* * *}

}
}

int main(void)
{
int * * choice_mm ;
struct *stackelement se;
struct *stack s;
s.top = -1;

for(;;)
{
* * * * printf("Choose the type of stack:\n");
* * * * printf("1.Integ er\n");
* * * * printf("2.Float \n");
* * * * printf("3.Strin g\n");
* * * * printf("4.Exit\ n");
* * * * scanf_s("%i", &choice_mm);

* * * * switch(choice_m m)
* * * * {
* * * * * * * * case 1:
* * * * * * * * * * * * se.etype=INT;
* * * * * * * * * * * * system("cls");
* * * * * * * * * * * * menu(&s,&se);
* * * * * * * * * * * * system("cls");
* * * * * * * * * * * * break;
* * * * * * * * case 2:
* * * * * * * * * * * * se.etype=FLOAT;
* * * * * * * * * * * * menu(&s,&se);
* * * * * * * * * * * * break;
* * * * * * * * case 3:
* * * * * * * * * * * * se.etype=STRING ;
* * * * * * * * * * * * menu(&s,&se);
* * * * * * * * * * * * break;
* * * * * * * * case 4:
* * * * * * * * * * * * exit(0);
* * * * * * * * * * * * break;
* * * * * * * * default:
* * * * * * * * * * * * printf("wrong input");
* * * * }

}
return 0;
}- Hide quoted text -

- Show quoted text -
/*
This thing still has a long way to go, but it is a little closer to
what you want.
The string you were using was an automatic variable. When a function
exits, all of its automatic variables go away.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STACKSIZE 100
#define CHAR_ELEMENT_SI ZE 10
typedef enum element_type {
uninitialized = 0, integer, floating, string
} e_type;

typedef struct stackelement {
e_type etype;
union {
int iVal;
float fVal;
char pVal[CHAR_ELEMENT_SI ZE];
} element;
} ste;

typedef struct stack {
int top;
ste item[STACKSIZE];
} Stack;

void push(struct stack * p, struct stackelement * se)
{
int iValue = 0;
float fValue = 0.0;
char cValue[CHAR_ELEMENT_SI ZE]={0};
int converted;
char *collected;

printf("Enter the number/string to push: ");
fflush(stdout);
if (se->etype == integer)
converted = scanf("%d", &iValue); /* Check converted and take
appropiate action! */
else if (se->etype == floating)
converted = scanf("%f", &fValue); /* Check converted and take
appropiate action! */
else if (se->etype == string) {
collected = fgets(cValue, sizeof cValue, stdin); /* Check
collected and take appropiate action! */
}
if (p->top == STACKSIZE - 1)
printf("overflo w\n");
else {
if (se->etype == integer)
p->item[++p->top].element.iVal = iValue;
else if (se->etype == floating)
p->item[++p->top].element.fVal = fValue;
else if (se->etype == string)
strcpy(p->item[++p->top].element.pVal , cValue);
else
puts("Unexpecte d element type.");
}
return;
}

void pop(struct stack * p, struct stackelement * se)
{
if (p->top == -1)
printf("underfl ow\n");
else {
if (se->etype == integer)
printf("Deleted Integer is %d\n", p->item[p-
>top].element.iVal);
else if (se->etype == floating)
printf("Deleted Float is %f\n", p->item[p-
>top].element.fVal);
else if (se->etype == string)
printf("Deleted String is %s\n", p->item[p-
>top].element.pVal);
--p->top;
}
return;
}

void display(struct stack * p, struct stackelement * se)
{
int i;
if (p->top == -1)
printf("Stack is Empty\n\n");
else {
if (se->etype == integer) {
for (i = p->top; i >= 0; i--)
printf("%d\n", p->item[i].element.iVal);
} else if (se->etype == floating) {
for (i = p->top; i >= 0; i--)
printf("%f\n", p->item[i].element.fVal);
} else if (se->etype == string) {
for (i = p->top; i >= 0; i--)
printf("%s\n", p->item[p->top].element.pVal);
}
/* ((((*(p)).item)[0]).element).pVal */
}
return;
}

void menu(struct stack * s, struct stackelement * s_elem)
{
int choice;
int flag = 1;
char *collected;
char st[4];
while (flag) {
printf("Menu");
printf("\n1.Pus h");
printf("\n2.Pop ");
printf("\n3.Dis play");
printf("\n4.Exi t");
printf("\n5.Cle ar screen\n");
printf("Enter your choice :");
collected = fgets(st, sizeof st, stdin);
choice = atoi(st);
switch (choice) {
case 1:
push(s, s_elem);
#ifdef WIN32
system("cls");
#else
system("clear") ; /* Still, not really portable. */
#endif
break;
case 2:
pop(s, s_elem);
break;
case 3:
display(s, s_elem);
break;
case 4:
flag = 0;
break;
case 5:
system("cls");
break;
default:
printf("wrong choice\n");
}
}
}

int main(void)
{
e_type choice_mm;
struct stackelement se = {0};
struct stack s = {0};
int converted;

s.top = -1;
for (;;) {
printf("Choose the type of stack item:\n");
printf("1.Integ er\n");
printf("2.Float \n");
printf("3.Strin g\n");
printf("4.Exit\ n");
converted = scanf_s("%i", &choice_mm); /* Check converted and
take appropiate action! */
switch (choice_mm) {
case 1:
se.etype = integer;
system("cls");
menu(&s, &se);
system("cls");
break;
case 2:
se.etype = floating;
menu(&s, &se);
break;
case 3:
se.etype = string;
menu(&s, &se);
break;
case 4:
exit(0);
break;
default:
printf("wrong input");
}
}
return 0;
}
Jan 5 '08 #8
user923005 wrote:
....snip...

Thank you .That was quite helpful!
Jan 5 '08 #9
On Sat, 05 Jan 2008 01:52:33 +0530, Tarique <pe*****@yahoo. comwrote:
>Tarique wrote:
>Hello all.I am trying to implement a stack which can store either
integer or float values.

The code is given below:
...snip...

Here is the final code:
Works fine in case of Integer n floats.However i lose track of the
character pointer once i exit the push function (in case of strings).
So in case of *Strings* i cannot display the popped string or the
members of the stack at any given time.
Using an extra global variable to keep track also does not help!
It's confusing!!!
Actually it is a very common mistake. When you are attempting to
build a stack of strings, you store your data in various instances of
the member pVal of the union. pVal is a pointer. It holds an address
and not the contents of the string. The address that you store in
this pointer is the address of the array cValue in the function push.
This causes at least two problems:

The first is that cValue is an automatic object. It gets
created when push is entered AND destroyed when push returns. As soon
as push returns, the address you stored in pVal becomes indeterminate
by definition. Any future attempt you make to evaluate this address
causes undefined behavior.

The second is that even if you gave cValue a life span that
survived multiple calls to push, such as declaring it static, it would
only contain the value from the last call to push. This would
eliminate the undefined behavior. But every instance of pVal would be
pointing to the same address. When you go to print the stack
contents, you would be printing the exact same data for every stack
element.

There are methods for solving the problem:

You could change pVal so it was an array the same size as
cValue. Then you could use strcpy to copy the string itself, not its
address, into pVal.

Instead of using a defined array, you could use malloc to
dynamically allocate space for each new string and store the address
of this new memory in pVal.
>
PS:ive not really bothered about scanf..Will change that :)

#include<stdio .h>
#include<stdli b.h>
#include<strin g.h>

#define STACKSIZE 100
#define INT 1
#define FLOAT 2
#define STRING 3

struct stackelement{
int etype; /*etype equals INT,FLOAT,STRIN G*/
/*depending on the type of the */
/*corresponding element */
union {
int iVal;
float fVal;
char *pVal;
}element;
};

struct stack{
int top;
struct stackelement item[STACKSIZE];
};

void push(struct stack *p,struct stackelement *se)
{
int test;
int iValue = 0;
float fValue = 0.0;
char cValue[10];

printf("Ente r the number/character to push");
if(se->etype == INT)
scanf("%d",&iVa lue);
else if(se->etype == FLOAT)
scanf("%f",&fVa lue);
else if(se->etype == STRING)
{
while((test=get char())!='\n')
;
fgets(cValue,10 ,stdin);
}
if(p->top == STACKSIZE-1 )
You should make this test prior to asking the user for input.
printf("overflo w\n");

else
{
if(se->etype == INT)
p->item[++p->top].element.iVal=i Value;
else if(se->etype == FLOAT)
p->item[++p->top].element.fVal=f Value;
else if(se->etype == STRING)
p->item[++p->top].element.pVal=c Value;
}
return;
}

void pop(struct stack *p,struct stackelement *se)
{
if(p->top==-1)
printf("underfl ow\n");
else
{
if(se->etype == INT)
printf("Deleted Integer is\ %d\n",p->item[p->top].element.iVal);
else if(se->etype == FLOAT)
printf("Deleted Float is\ %f\n",p->item[p->top].element.fVal);
else if(se->etype == STRING)
printf("Deleted String is\ %s\n",p->item[p->top].element.pVal);
--p->top;
}
return;
}
void display(struct stack *p,struct stackelement *se)
{
int i;
if(p->top == -1)
printf("Stack is Empty\n\n");
else
{
if(se->etype == INT)
{
for(i=p->top; i >= 0; i--)
printf("%d\n", p->item[i].element.iVal );
}

else if(se->etype == FLOAT)
{
for(i = p->top; i >= 0; i--)
printf("%f\n", p->item[i].element.fVal);
}

else if(se->etype == STRING)
{
for(i = p->top; i >= 0; i--)
printf("%s\n",p->item[p->top].element.pVal);
}
/*((((*(p)).item )[0]).element).pVal */
}
return;
}

void menu(struct stack *s,struct stackelement *s_elem)
{
int num=0;
int choice;
int flag=1;

while(flag)
{
printf("Menu");
printf("\n1.Pus h");
printf("\n2.Pop ");
printf("\n3.Dis play");
printf("\n4.Exi t");
printf("\n5.Cle ar screen\n");
printf("Enter your choice :");
scanfs("%d",&ch oice);

switch(choice)
{
case 1:
push(s,s_elem);
system("cls");
break;
case 2:
pop(s,s_elem);
break;
case 3:
display(s,s_ele m);
break;
case 4:
flag=0;
break;
case 5:
system("cls");
break;
default:
printf("wrong choice\n");
}
}
}

int main(void)
{
int choice_mm ;
struct stackelement se;
struct stack s;
s.top = -1;

for(;;)
{
printf("Choose the type of stack:\n");
printf("1.Integ er\n");
printf("2.Float \n");
printf("3.Strin g\n");
printf("4.Exit\ n");
scanf_s("%i", &choice_mm);

switch(choice_m m)
{
case 1:
se.etype=INT;
system("cls");
menu(&s,&se);
system("cls");
break;
case 2:
se.etype=FLOAT;
menu(&s,&se);
break;
case 3:
se.etype=STRING ;
menu(&s,&se);
break;
case 4:
exit(0);
break;
default:
printf("wrong input");
}
}
return 0;
Do you think your code can ever reach this statement?
>}

Remove del for email
Jan 8 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
7735
by: Andrew | last post by:
Last night I was reading about implementing my own stack. The example given pushes items on and off the stack at the start and end of each procedure (ie. in a std module). What's not so clear is how this would work with class objects. In this case do you have to push the object on the stack at the start of every public procedure etc. in the class and pop it off at the end? I can't see how else you can know which object is active - or...
14
30095
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not part of the standard, then just disregard this question. Here's some questions I'm confused about, and if you can add anything else, please do so! Is the stack limited for each program?
4
2619
by: Chris Mabee | last post by:
Hello all, and Merry Christmas, I'm having a problem understanding an example of an array based implementation of a stack in a textbook of mine. The code in question is written below. The syntax is directly as in the book, except for where I added the comments at the lines I wanted to refer to or to skip sections of code. template <class Element_Type> class Stack {
4
3622
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
8
2095
by: LedZep | last post by:
What up everyone, I have to write a program that uses a stack to determine whether a string is a palindrome (a string that is spelled identically backward and forward). The program has to ignore spaces, case sensitivity and punctuation. I need three text boxes. The first will be for input of the string. The second will display the string when the "check" button is clicked and the result of the analysis (string is a palindrome or is...
4
2146
by: alisaee | last post by:
plz check what i have made wrong what is requierd her is to creat class queue and class stack and run the push,pop operation . #include<iostream.h> #include<conio.h> #include<stdio.h> class stack { public:
16
4444
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If local variables that are primitives go in stack, it is OK. But what
24
6578
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means the stack pointer should go upwards when there are "push" operations, and stack pointer should go downards when there are "pop" operations?? If this is the case, the address should go upwards (increasing) or downards (decreasing) then? i.e....
1
4216
by: alfie27 | last post by:
I currently have a working program that is a stack that stores integers. Now i have to convert it to store strings instead of integers. I have been working on this for hours and just keep getting errors of all kinds. I have decided to start from scratch. Any suggestions someone can give me would be greatly appreciated!! Here is the current code: #include <iostream> using std::cout; using std::cin; #include <cstring> using std::strcpy;
11
1779
by: tom | last post by:
Hi! Im new to Python and doing exercise found from internet. It is supposed to evaluate expression given with postfix operator using Stack() class. class Stack: def __init__(self): self.items = def push(self, item): self.items.append(item)
0
8840
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8730
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9367
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9131
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3189
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.