dereferencing âvoid *â pointer stack_implementation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chirayu143
    New Member
    • Aug 2010
    • 6

    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......

    Code:
    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...
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Are you getting compiler errors with what you already have? If so post them.

    Comment

    • chirayu143
      New Member
      • Aug 2010
      • 6

      #3
      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>typ e size
      Code:
      /******************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;
      }
      Last edited by Banfa; Aug 30 '10, 08:02 PM. Reason: Corrected code tags

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Please summarize in words what you wish to accomplish.
        Please explain what part of that is not working for you.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          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?

          Comment

          • chirayu143
            New Member
            • Aug 2010
            • 6

            #6
            its done....

            Comment

            Working...