473,386 Members | 1,842 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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,STRING*/
/*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("overflow\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("underflow\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_elem);
in the main function
Can anyone please help me out?Thanks

Jan 4 '08 #1
9 2477
In <fl**********@aioe.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_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.

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

Jan 4 '08 #2
John Gordon wrote:
In <fl**********@aioe.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_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.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n5.Clear screen\n");
printf("Enter your choice :");
scanfs("%d",&choice);

switch(choice)
{
case 1:
printf("Enter the number to push");
scanfs("%d",&num);
fflush(stdin);
push(s,s_elem,num);/*warning*/
break;
case 2:
pop(s,s_elem); /*warning*/
break;
case 3:
display(s,s_elem);/*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.Integer\n");
printf("2.Float\n");
printf("3.String\n");
printf("4.Exit\n");
scanfs("%i", &choice_mm);

switch(choice_mm)
{
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**********@aioe.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,STRING*/
* * * * * * * * * * * * * * * * /*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("overflow\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("underflow\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_elem);
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,STRING*/
/*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",&iValue);
else if(se->etype == FLOAT)
scanf("%f",&fValue);
else if(se->etype == STRING)
{
while((test=getchar())!='\n')
;
fgets(cValue,10,stdin);
}
if(p->top == STACKSIZE-1 )
printf("overflow\n");

else
{
if(se->etype == INT)
p->item[++p->top].element.iVal=iValue;
else if(se->etype == FLOAT)
p->item[++p->top].element.fVal=fValue;
else if(se->etype == STRING)
p->item[++p->top].element.pVal=cValue;
}
return;
}

void pop(struct stack *p,struct stackelement *se)
{
if(p->top==-1)
printf("underflow\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.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n5.Clear screen\n");
printf("Enter your choice :");
scanfs("%d",&choice);

switch(choice)
{
case 1:
push(s,s_elem);
system("cls");
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)
{
int choice_mm ;
struct stackelement se;
struct stack s;
s.top = -1;

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

switch(choice_mm)
{
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,STRING*/
* * * * * * * * * * * * /*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",&iValue);
else if(se->etype == FLOAT)
* * * * scanf("%f",&fValue);
else if(se->etype == STRING)
{
* * * * while((test=getchar())!='\n')
* * * * * * * * ;
* * * * fgets(cValue,10,stdin);

}

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

else
{
* * * * if(se->etype == INT)
* * * * * * * * p->item[++p->top].element.iVal=iValue;
* * * * else if(se->etype == FLOAT)
* * * * * * * * p->item[++p->top].element.fVal=fValue;
* * * * else if(se->etype == STRING)
* * * * * * * * p->item[++p->top].element.pVal=cValue;

}
return;
}

void pop(struct stack *p,struct stackelement *se)
{
if(p->top==-1)
* * *printf("underflow\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.Push");
* * * * printf("\n2.Pop");
* * * * printf("\n3.Display");
* * * * printf("\n4.Exit");
* * * * printf("\n5.Clear screen\n");
* * * * printf("Enter your choice :");
* * * * scanfs("%d",&choice);

* * * * switch(choice)
* * * * {
* * * * * * case 1:
* * * * * * * * push(s,s_elem);
* * * * * * * * system("cls");
* * * * * * * * 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)
{
int * * choice_mm ;
struct *stackelement se;
struct *stack s;
s.top = -1;

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

* * * * switch(choice_mm)
* * * * {
* * * * * * * * 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_SIZE 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_SIZE];
} 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_SIZE]={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("overflow\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("Unexpected element type.");
}
return;
}

void pop(struct stack * p, struct stackelement * se)
{
if (p->top == -1)
printf("underflow\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.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n5.Clear 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.Integer\n");
printf("2.Float\n");
printf("3.String\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<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,STRING*/
/*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",&iValue);
else if(se->etype == FLOAT)
scanf("%f",&fValue);
else if(se->etype == STRING)
{
while((test=getchar())!='\n')
;
fgets(cValue,10,stdin);
}
if(p->top == STACKSIZE-1 )
You should make this test prior to asking the user for input.
printf("overflow\n");

else
{
if(se->etype == INT)
p->item[++p->top].element.iVal=iValue;
else if(se->etype == FLOAT)
p->item[++p->top].element.fVal=fValue;
else if(se->etype == STRING)
p->item[++p->top].element.pVal=cValue;
}
return;
}

void pop(struct stack *p,struct stackelement *se)
{
if(p->top==-1)
printf("underflow\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.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n5.Clear screen\n");
printf("Enter your choice :");
scanfs("%d",&choice);

switch(choice)
{
case 1:
push(s,s_elem);
system("cls");
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)
{
int choice_mm ;
struct stackelement se;
struct stack s;
s.top = -1;

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

switch(choice_mm)
{
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
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...
14
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...
4
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...
4
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...
8
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...
4
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...
16
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...
24
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...
1
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...
11
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.