By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

dereferencing ‚void *‚ pointer stack_implementation

P: 6
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......

Expand|Select|Wrap|Line Numbers
  1. typedef struct stStack
  2. {
  3.     int ntop;
  4.     void* pvitem;
  5. }Stack;
  6. pop(m_stack,nty);
  7.  
  8.  
  9. void pop(Stack *m_sstack,ettype nty)
  10. {
  11.  
  12.     if(m_sstack->ntop==-1)
  13.     {
  14.         printf("\nstack underflow");
  15.     }
  16.     else if(nty==1)
  17.     {
  18.         int nnum;
  19.         nnum=*((int*)(m_sstack->pvitem[m_sstack->ntop]));
  20.         printf("\npoped no. is %d",nnum);
  21.     }
  22.     else if(nty==2)
  23.     {
  24.         char nnum;
  25.         nnum=*((char*)(m_sstack->pvitem[m_sstack->ntop]));
  26.         printf("\npoped no. is %c",nnum);
  27.  
  28.     }
  29.     else if(nty==3)
  30.     {
  31.         float nnum;
  32.         nnum=*((float*)(m_sstack->pvitem[m_sstack->ntop]));
  33.         printf("\npoped no. is %f",nnum);
  34.     }
  35.     else
  36.     {
  37.         double nnum;
  38.         nnum=((double*)(m_sstack->pvitem[m_sstack->ntop]));
  39.         printf("\npoped no. is %f",nnum);
  40.     }
  41.  
  42. }
  43. here nty is the type of the stack which user provide...
  44.  
Aug 30 '10 #1

✓ answered by Banfa

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?

Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,916
Are you getting compiler errors with what you already have? If so post them.
Aug 30 '10 #2

P: 6
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
Expand|Select|Wrap|Line Numbers
  1. /******************Creation of Stack and its operation*******************/
  2. /*******By: Chirayu Joshi*******/
  3. /*******26/08/2010*******/
  4.  
  5.  
  6. #include<stdio.h>
  7. #include<string.h>
  8. #include<stdlib.h>
  9.  
  10. //void Operation_On_Stack(Stack *,ettype,int);
  11.  
  12. typedef enum ettype
  13. {
  14.     in=1,chr,flot,doble
  15. }ettype;
  16.  
  17. typedef struct stStack
  18. {
  19.     int ntop;
  20.     void* pvitem;
  21. }Stack;
  22.  
  23.  
  24. /**********peek operation*****************/
  25.  
  26. void peek(Stack *m_sstack,ettype nty)
  27. {
  28.     if(m_sstack->ntop==-1)
  29.     {
  30.         printf("\nstack underflow");
  31.     }
  32.     else if(nty==1)
  33.     {
  34.         printf("\n top value is %d",*((int*)(m_sstack->pvitem) + m_sstack->ntop));
  35.     }
  36.     else if(nty==2)
  37.     {
  38.         printf("\n top value is %c",*((char*)(m_sstack->pvitem) + m_sstack->ntop));
  39.  
  40.     }
  41.     else if(nty==3)
  42.     {
  43.         printf("\n top value is %f",*((float*)(m_sstack->pvitem) + m_sstack->ntop));
  44.     }
  45.     else
  46.     {
  47.         printf("\n top value is %f",*((float*)(m_sstack->pvitem) + m_sstack->ntop));
  48.     }
  49.  
  50. }
  51.  
  52.  
  53. /**********pop operation on stack**********/
  54.  
  55. void pop(Stack *m_sstack,ettype nty)
  56. {
  57.  
  58.     if(m_sstack->ntop==-1)
  59.     {
  60.         printf("\nstack underflow");
  61.     }
  62.     else if(nty==1)
  63.     {
  64.         int nnum;
  65.         nnum=*((int*)(m_sstack->pvitem) + m_sstack->ntop);
  66.         printf("\npoped no. is %d",nnum);
  67.         m_sstack->ntop--;
  68.     }
  69.     else if(nty==2)
  70.     {
  71.         char cnum;
  72.         cnum=*((int*)(m_sstack->pvitem) + m_sstack->ntop);
  73.         printf("\npoped no. is %c",cnum);
  74.         m_sstack->ntop--;
  75.  
  76.     }
  77.     else if(nty==3)
  78.     {
  79.         float fnum;
  80.         fnum=*((int*)(m_sstack->pvitem) + m_sstack->ntop);
  81.         printf("\npoped no. is %f",fnum);
  82.         m_sstack->ntop--;
  83.     }
  84.     else
  85.     {
  86.         double dnum;
  87.         dnum=*((int*)(m_sstack->pvitem) + m_sstack->ntop);
  88.         printf("\npoped no. is %f",dnum);
  89.         m_sstack->ntop--;
  90.     }
  91.  
  92. }
  93.  
  94.  
  95.  
  96. /**********push operation on stack**********/
  97.  
  98. void push(Stack *m_sstack,void* pvelement,int nsize,ettype nty)
  99. {
  100.     if(m_sstack->ntop==nsize)
  101.     {
  102.         printf("\n Stack Overflow");
  103.         exit(1);
  104.     }
  105.     else
  106.     {
  107.         m_sstack->ntop++;
  108.             if(nty==1)
  109.             {
  110.                 *((int*)(m_sstack->pvitem) + m_sstack->ntop) = *(int*)pvelement;
  111.                 printf("\nvalue entered in the stack :%d",*((int*)(m_sstack->pvitem) + m_sstack->ntop));
  112.             }
  113.             else if(nty==2)
  114.             {
  115.                 *((char*)(m_sstack->pvitem) + m_sstack->ntop) = *(char*)pvelement;
  116.                 printf("\nvalue entered in the stack :%c",*((char*)(m_sstack->pvitem) + m_sstack->ntop));
  117.             }
  118.             else if(nty==3)
  119.             {
  120.                 *((float*)(m_sstack->pvitem) + m_sstack->ntop) = *(float*)pvelement;
  121.                 printf("\nvalue entered in the stack :%d",*((float*)(m_sstack->pvitem) + m_sstack->ntop));
  122.             }
  123.             else
  124.             {
  125.                 *((float*)(m_sstack->pvitem) + m_sstack->ntop) = *(float*)pvelement;
  126.                 printf("\nvalue entered in the stack :%d",*((float*)(m_sstack->pvitem) + m_sstack->ntop));
  127.             }
  128.     }
  129. }
  130.  
  131.  
  132.  
  133. /****************Operation on stack***************/
  134. /**Providing the menu for the operation to be performed**/
  135. /****no arguments to be given****/
  136. /***this function returns void***/
  137.  
  138. void Operation_On_Stack(Stack *m_stack,ettype type,int nsize)
  139. {
  140.  
  141.     int nwill=1;
  142.     int nty=type;
  143.     while(nwill==1)
  144.     {
  145.         printf("    \nMAIN MENU:    \n1.Pushing element to Stack\n2.Poping element from the Stack\n3.Peeking element from the Stack");
  146.         scanf("%d",&nwill);
  147.             switch(nwill)
  148.             {
  149.                 case 1:
  150.                     if(nty==1)
  151.                     {
  152.                         int nnum;
  153.                         printf("\nEnter the data... ");
  154.                         scanf("%d",&nnum);
  155.                         printf("\ndata entered");
  156.                         push(m_stack,(void*)&nnum,nsize,nty);
  157.                         break;
  158.                     }
  159.                     else if(nty==2)
  160.                     {
  161.                         char cnum;
  162.                         printf("\nEnter the data...");
  163.                         getchar();
  164.                         scanf("%c",&cnum);
  165.                         push(m_stack,(void*)&cnum,nsize,nty);
  166.                         break;
  167.                     }
  168.                     else if(nty==3)
  169.                     {
  170.                         float fnum;
  171.                         printf("\nEnter the data... ");
  172.                         scanf("%f",&fnum);
  173.                         push(m_stack,(void*)&fnum,nsize,nty);
  174.                         break;
  175.                     }
  176.                     else if(nty==4)
  177.                     {
  178.                         double dnum;
  179.                         printf("\nEnter the data... ");
  180.                         scanf("%f",&dnum);
  181.                         push(m_stack,(void*)&dnum,nsize,nty);
  182.                         break;
  183.                     }
  184.                     else
  185.                     {
  186.                         printf("\nenter the correct type");
  187.                         break;
  188.                     }
  189.                 case 2:
  190.                     pop(m_stack,nty);
  191.                     break;
  192.                 case 3:
  193.                     peek(m_stack,nty);
  194.                     break;
  195.                 default: printf("\nInvalid Choice . ");
  196.             }
  197.             printf(" \nDo you want to do more operations on Stack ( 1 for yes, any other key to exit) ");
  198.             scanf("%d" , &nwill);
  199.     } //end of  outer while
  200. }
  201.  
  202.  
  203. /*********main function*********/
  204.  
  205. int main(int argc,char*argv[])
  206. {
  207.     int *pnInteger;
  208.     char *pcChar;
  209.     float *pfFloat;
  210.     double *pdDouble;
  211.     int nsize;
  212.     ettype type;
  213.     Stack m_stack;
  214.     nsize=atoi(argv[2]);
  215.     if((strcmp(argv[1],"int")==0)||(strcmp(argv[1],"INT")==0))
  216.     {
  217.         type=in;
  218.     }
  219.     else if((strcmp(argv[1],"char")==0)||(strcmp(argv[1],"CHAR")==0))
  220.     {
  221.         type=chr;
  222.     }
  223.     else if((strcmp(argv[1],"float")==0)||(strcmp(argv[1],"FLOAT")==0))
  224.     {
  225.         type=flot;
  226.     }
  227.     else if((strcmp(argv[1],"double")==0)||(strcmp(argv[1],"DOUBLE")==0))
  228.     {
  229.         type=doble;
  230.     }
  231.     else
  232.     {
  233.         printf("Enter the correct type\n");
  234.         exit(0);
  235.     }
  236.  
  237.     switch(type)
  238.     {
  239.         case 1:
  240.             pnInteger=malloc(nsize*sizeof(int));
  241.             if(pnInteger)
  242.             {
  243.                 m_stack.pvitem=pnInteger;
  244.                 m_stack.ntop=-1;
  245.                 printf("\nint type stack created\n");
  246.                 printf("\n");
  247.                 Operation_On_Stack(&m_stack,type,nsize);
  248.  
  249.             }
  250.             else
  251.             {
  252.                 printf("\nThere is no sufficient memory on the heap\n");
  253.             }
  254.             break;
  255.  
  256.         case 2:
  257.  
  258.             pcChar=malloc(nsize*sizeof(char));
  259.             if(pcChar)
  260.             {
  261.                 m_stack.pvitem=pcChar;
  262.                 m_stack.ntop=-1;
  263.                 printf("char type stack created\n");
  264.                 printf("\n");
  265.                 Operation_On_Stack(&m_stack,type,nsize);
  266.             }
  267.             else
  268.             {
  269.                 printf("\nThere is no sufficient memory on the heap\n");
  270.             }
  271.             break;
  272.  
  273.         case 3:
  274.  
  275.             pfFloat=malloc(nsize*sizeof(float));
  276.             if(pfFloat)
  277.             {
  278.                 m_stack.pvitem=pfFloat;
  279.                 m_stack.ntop=-1;
  280.                 printf("float type stack created\n");
  281.                 printf("\n");
  282.                 Operation_On_Stack(&m_stack,type,nsize);
  283.             }
  284.             else
  285.             {
  286.                 printf("\nThere is no sufficient memory on the heap\n");
  287.             }
  288.             break;
  289.  
  290.         case 4:
  291.  
  292.             pdDouble=malloc(nsize*sizeof(double));
  293.             if(pdDouble)
  294.             {
  295.                 m_stack.pvitem=pdDouble;
  296.                 m_stack.ntop=-1;
  297.                 printf("double type stack created\n");
  298.                 printf("\n");
  299.                 Operation_On_Stack(&m_stack,type,nsize);
  300.             }
  301.             else
  302.             {
  303.                 printf("\nThere is no sufficient memory on the heap\n");
  304.             }
  305.             break;
  306.         default:
  307.         printf("\nType not defined");
  308.     }
  309.  
  310.     return 0;
  311. }
  312.  
Aug 30 '10 #3

Expert 100+
P: 2,396
Please summarize in words what you wish to accomplish.
Please explain what part of that is not working for you.
Aug 30 '10 #4

Banfa
Expert Mod 5K+
P: 8,916
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?
Aug 30 '10 #5

P: 6
its done....
Sep 1 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.