473,385 Members | 1,185 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,385 software developers and data experts.

dereferencing âvoid *â pointer stack_implementation

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?

5 2195
Banfa
9,065 Expert Mod 8TB
Are you getting compiler errors with what you already have? If so post them.
Aug 30 '10 #2
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
donbock
2,426 Expert 2GB
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
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?
Aug 30 '10 #5
its done....
Sep 1 '10 #6

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

Similar topics

1
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();...
3
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;
4
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...
110
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...
4
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...
1
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...
204
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 =...
13
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...
3
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() ) { ...
12
by: viza | last post by:
Hi I have program1.c: typedef int (*fn_t)(int); int fn( int f ){ return f; }
1
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...
0
isladogs
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...
0
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...
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: 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$) { } ...
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: 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?
0
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...

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.