here i m showing ma code....its not the whole code but the structure the calling function and the called push function....plz help me tell how to extract data for the void pointer which is pointing to some value...... -
typedef struct stStack
-
{
-
int ntop;
-
void* pvitem;
-
}Stack;
-
pop(m_stack,nty);
-
-
-
void pop(Stack *m_sstack,ettype nty)
-
{
-
-
if(m_sstack->ntop==-1)
-
{
-
printf("\nstack underflow");
-
}
-
else if(nty==1)
-
{
-
int nnum;
-
nnum=*((int*)(m_sstack->pvitem[m_sstack->ntop]));
-
printf("\npoped no. is %d",nnum);
-
}
-
else if(nty==2)
-
{
-
char nnum;
-
nnum=*((char*)(m_sstack->pvitem[m_sstack->ntop]));
-
printf("\npoped no. is %c",nnum);
-
-
}
-
else if(nty==3)
-
{
-
float nnum;
-
nnum=*((float*)(m_sstack->pvitem[m_sstack->ntop]));
-
printf("\npoped no. is %f",nnum);
-
}
-
else
-
{
-
double nnum;
-
nnum=((double*)(m_sstack->pvitem[m_sstack->ntop]));
-
printf("\npoped no. is %f",nnum);
-
}
-
-
}
-
here nty is the type of the stack which user provide...
-
I find it hard to believe you get no compiler errors since you can not use a void pointer as an array or in any situation requiring arithmetic because the void type has no size.
However in your first code listing you attempt this at every line line this
m_sstack->pvitem[m_sstack->ntop]
If fact your first code listing gives me 6 compiler errors so I would be interested to know how you manage to compile without errors?
5 2195 Banfa 9,065
Expert Mod 8TB
Are you getting compiler errors with what you already have? If so post them.
not getting any compilation error but it is not taking the value char float and double.....
i m sending ma whole code just tell me wats the probs....i passing commandline argument <filename>type size -
/******************Creation of Stack and its operation*******************/
-
/*******By: Chirayu Joshi*******/
-
/*******26/08/2010*******/
-
-
-
#include<stdio.h>
-
#include<string.h>
-
#include<stdlib.h>
-
-
//void Operation_On_Stack(Stack *,ettype,int);
-
-
typedef enum ettype
-
{
-
in=1,chr,flot,doble
-
}ettype;
-
-
typedef struct stStack
-
{
-
int ntop;
-
void* pvitem;
-
}Stack;
-
-
-
/**********peek operation*****************/
-
-
void peek(Stack *m_sstack,ettype nty)
-
{
-
if(m_sstack->ntop==-1)
-
{
-
printf("\nstack underflow");
-
}
-
else if(nty==1)
-
{
-
printf("\n top value is %d",*((int*)(m_sstack->pvitem) + m_sstack->ntop));
-
}
-
else if(nty==2)
-
{
-
printf("\n top value is %c",*((char*)(m_sstack->pvitem) + m_sstack->ntop));
-
-
}
-
else if(nty==3)
-
{
-
printf("\n top value is %f",*((float*)(m_sstack->pvitem) + m_sstack->ntop));
-
}
-
else
-
{
-
printf("\n top value is %f",*((float*)(m_sstack->pvitem) + m_sstack->ntop));
-
}
-
-
}
-
-
-
/**********pop operation on stack**********/
-
-
void pop(Stack *m_sstack,ettype nty)
-
{
-
-
if(m_sstack->ntop==-1)
-
{
-
printf("\nstack underflow");
-
}
-
else if(nty==1)
-
{
-
int nnum;
-
nnum=*((int*)(m_sstack->pvitem) + m_sstack->ntop);
-
printf("\npoped no. is %d",nnum);
-
m_sstack->ntop--;
-
}
-
else if(nty==2)
-
{
-
char cnum;
-
cnum=*((int*)(m_sstack->pvitem) + m_sstack->ntop);
-
printf("\npoped no. is %c",cnum);
-
m_sstack->ntop--;
-
-
}
-
else if(nty==3)
-
{
-
float fnum;
-
fnum=*((int*)(m_sstack->pvitem) + m_sstack->ntop);
-
printf("\npoped no. is %f",fnum);
-
m_sstack->ntop--;
-
}
-
else
-
{
-
double dnum;
-
dnum=*((int*)(m_sstack->pvitem) + m_sstack->ntop);
-
printf("\npoped no. is %f",dnum);
-
m_sstack->ntop--;
-
}
-
-
}
-
-
-
-
/**********push operation on stack**********/
-
-
void push(Stack *m_sstack,void* pvelement,int nsize,ettype nty)
-
{
-
if(m_sstack->ntop==nsize)
-
{
-
printf("\n Stack Overflow");
-
exit(1);
-
}
-
else
-
{
-
m_sstack->ntop++;
-
if(nty==1)
-
{
-
*((int*)(m_sstack->pvitem) + m_sstack->ntop) = *(int*)pvelement;
-
printf("\nvalue entered in the stack :%d",*((int*)(m_sstack->pvitem) + m_sstack->ntop));
-
}
-
else if(nty==2)
-
{
-
*((char*)(m_sstack->pvitem) + m_sstack->ntop) = *(char*)pvelement;
-
printf("\nvalue entered in the stack :%c",*((char*)(m_sstack->pvitem) + m_sstack->ntop));
-
}
-
else if(nty==3)
-
{
-
*((float*)(m_sstack->pvitem) + m_sstack->ntop) = *(float*)pvelement;
-
printf("\nvalue entered in the stack :%d",*((float*)(m_sstack->pvitem) + m_sstack->ntop));
-
}
-
else
-
{
-
*((float*)(m_sstack->pvitem) + m_sstack->ntop) = *(float*)pvelement;
-
printf("\nvalue entered in the stack :%d",*((float*)(m_sstack->pvitem) + m_sstack->ntop));
-
}
-
}
-
}
-
-
-
-
/****************Operation on stack***************/
-
/**Providing the menu for the operation to be performed**/
-
/****no arguments to be given****/
-
/***this function returns void***/
-
-
void Operation_On_Stack(Stack *m_stack,ettype type,int nsize)
-
{
-
-
int nwill=1;
-
int nty=type;
-
while(nwill==1)
-
{
-
printf(" \nMAIN MENU: \n1.Pushing element to Stack\n2.Poping element from the Stack\n3.Peeking element from the Stack");
-
scanf("%d",&nwill);
-
switch(nwill)
-
{
-
case 1:
-
if(nty==1)
-
{
-
int nnum;
-
printf("\nEnter the data... ");
-
scanf("%d",&nnum);
-
printf("\ndata entered");
-
push(m_stack,(void*)&nnum,nsize,nty);
-
break;
-
}
-
else if(nty==2)
-
{
-
char cnum;
-
printf("\nEnter the data...");
-
getchar();
-
scanf("%c",&cnum);
-
push(m_stack,(void*)&cnum,nsize,nty);
-
break;
-
}
-
else if(nty==3)
-
{
-
float fnum;
-
printf("\nEnter the data... ");
-
scanf("%f",&fnum);
-
push(m_stack,(void*)&fnum,nsize,nty);
-
break;
-
}
-
else if(nty==4)
-
{
-
double dnum;
-
printf("\nEnter the data... ");
-
scanf("%f",&dnum);
-
push(m_stack,(void*)&dnum,nsize,nty);
-
break;
-
}
-
else
-
{
-
printf("\nenter the correct type");
-
break;
-
}
-
case 2:
-
pop(m_stack,nty);
-
break;
-
case 3:
-
peek(m_stack,nty);
-
break;
-
default: printf("\nInvalid Choice . ");
-
}
-
printf(" \nDo you want to do more operations on Stack ( 1 for yes, any other key to exit) ");
-
scanf("%d" , &nwill);
-
} //end of outer while
-
}
-
-
-
/*********main function*********/
-
-
int main(int argc,char*argv[])
-
{
-
int *pnInteger;
-
char *pcChar;
-
float *pfFloat;
-
double *pdDouble;
-
int nsize;
-
ettype type;
-
Stack m_stack;
-
nsize=atoi(argv[2]);
-
if((strcmp(argv[1],"int")==0)||(strcmp(argv[1],"INT")==0))
-
{
-
type=in;
-
}
-
else if((strcmp(argv[1],"char")==0)||(strcmp(argv[1],"CHAR")==0))
-
{
-
type=chr;
-
}
-
else if((strcmp(argv[1],"float")==0)||(strcmp(argv[1],"FLOAT")==0))
-
{
-
type=flot;
-
}
-
else if((strcmp(argv[1],"double")==0)||(strcmp(argv[1],"DOUBLE")==0))
-
{
-
type=doble;
-
}
-
else
-
{
-
printf("Enter the correct type\n");
-
exit(0);
-
}
-
-
switch(type)
-
{
-
case 1:
-
pnInteger=malloc(nsize*sizeof(int));
-
if(pnInteger)
-
{
-
m_stack.pvitem=pnInteger;
-
m_stack.ntop=-1;
-
printf("\nint type stack created\n");
-
printf("\n");
-
Operation_On_Stack(&m_stack,type,nsize);
-
-
}
-
else
-
{
-
printf("\nThere is no sufficient memory on the heap\n");
-
}
-
break;
-
-
case 2:
-
-
pcChar=malloc(nsize*sizeof(char));
-
if(pcChar)
-
{
-
m_stack.pvitem=pcChar;
-
m_stack.ntop=-1;
-
printf("char type stack created\n");
-
printf("\n");
-
Operation_On_Stack(&m_stack,type,nsize);
-
}
-
else
-
{
-
printf("\nThere is no sufficient memory on the heap\n");
-
}
-
break;
-
-
case 3:
-
-
pfFloat=malloc(nsize*sizeof(float));
-
if(pfFloat)
-
{
-
m_stack.pvitem=pfFloat;
-
m_stack.ntop=-1;
-
printf("float type stack created\n");
-
printf("\n");
-
Operation_On_Stack(&m_stack,type,nsize);
-
}
-
else
-
{
-
printf("\nThere is no sufficient memory on the heap\n");
-
}
-
break;
-
-
case 4:
-
-
pdDouble=malloc(nsize*sizeof(double));
-
if(pdDouble)
-
{
-
m_stack.pvitem=pdDouble;
-
m_stack.ntop=-1;
-
printf("double type stack created\n");
-
printf("\n");
-
Operation_On_Stack(&m_stack,type,nsize);
-
}
-
else
-
{
-
printf("\nThere is no sufficient memory on the heap\n");
-
}
-
break;
-
default:
-
printf("\nType not defined");
-
}
-
-
return 0;
-
}
-
Please summarize in words what you wish to accomplish.
Please explain what part of that is not working for you.
Banfa 9,065
Expert Mod 8TB
I find it hard to believe you get no compiler errors since you can not use a void pointer as an array or in any situation requiring arithmetic because the void type has no size.
However in your first code listing you attempt this at every line line this
m_sstack->pvitem[m_sstack->ntop]
If fact your first code listing gives me 6 compiler errors so I would be interested to know how you manage to compile without errors?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Larry Lindsey |
last post by:
I have a linked list class that is giving me problems when I try to return
the nth node. Here is what it looks like:
class LinkedList{
public:
ListNode getNode(int which);
int getValue();...
|
by: Peter L. |
last post by:
Hi!
I would like to know if it's allowed to dereference a pointer
to a class like im doing it. I'm building a tree with nodes.
class node
{
private:
int num;
|
by: Richard Cavell |
last post by:
Hi,
I want to create an arbitrary number of objects, whose size will vary.
I want to keep track of them. It seems best to create an array of
pointers to these objects. Since the array itself...
|
by: Mr A |
last post by:
Hi!
I've been thinking about passing parameteras using references instead
of pointers in order to emphasize that the parameter must be an
object.
Exemple:
void func(Objec& object); //object...
|
by: Pushkar Pradhan |
last post by:
I have some functions which take as i/p a buffer (it can be float, char,
or 16 bit, int etc.). The result is another o/p buffer, its type is also
flexible (it could be a float, char etc.).
I try...
|
by: vishal |
last post by:
Hi,
I have a large memory allocated in my program and was trying to print
the values read (byte by byte) since the pointer in turboc is 2 bytes,
I used a char far pointer to the calculted address...
|
by: Alexei A. Frounze |
last post by:
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2 = {0,1,2,4,9,16};
int aInt3 =...
|
by: aegis |
last post by:
The following was mentioned by Eric Sosman
from
http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source
"The alignment requirement for any type T must be a
divisor of...
|
by: rasmidas |
last post by:
Hi,
I am having the following block of code. Should I use a NULL checking for tokens, where ever there is *tokens. Please let me know soon.
while( !infile.eof() )
{
...
|
by: viza |
last post by:
Hi
I have program1.c:
typedef int (*fn_t)(int);
int fn( int f ){
return f;
}
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |