Why does this give an error saying "request for data element in something not a structure"?
What's the problem with the commented line?
#include<stdio.h>
typedef struct _stack
{
char data;
struct stack* next;开发者_StackOverflow中文版
}stack;
int main()
{
char *s="4$2*3-3+8/4/(1+1)";
char * prefix= (char*) malloc(strlen(s+1));
convertinfixtoprefix(s,prefix);
int len=strlen(prefix);
int i;
for(i=0;i<len/2;i++)
{
char temp;
temp=prefix[i];
prefix[i]=prefix[len-i-1];
prefix[len-i-1]=prefix[i];
}
printf("The Prefix expression is %s",prefix);
}
void convertinfixtoprefix(char* infix,char*prefix)
{
stack *s=NULL;
int pr;
char c=NULL;
char d;
int i=strlen(infix)-1;
int index=0;
while(i>=0)
{
c=infix[i];
if(c==')')
{
push(&s,c);
}
else
if(c=='(')
{
while((d=pop(&s))!=')')
{
prefix[index]=d;
index++;
}
}
if( (c!='+') && (c!='-') && (c!='*') && (c!='/') && (c!='$') )
{
prefix[index]=c;
index++;
}
else
{
pr=priority(d=pop(&s));
while(pr>priority(c))
{
prefix[index]=d;
index++;
pr=priority(d=pop(&s));
}
push(&s,c);
}
}
}
void push(stack** s,char c)
{
if(*s==NULL)
{
*s=(stack*)malloc(sizeof(stack));
((*s)->data)=c;// HERE ITS SHOWS ERROR WHY?
((*s)->next)=NULL;
}
else
{
stack* temp=(stack*)malloc(sizeof(stack));
temp->data=c;
temp->next=*s;
(*s)=temp;
}
}
char pop(stack**s)
{
if(*s==NULL)
printf("Stack khaali hai bhai\n");
else
{
char c=*s->data;
stack* temp=*s;
*s=*s->next;
free(temp);
return c;
}
return 0;
}
int priority(char c)
{
if(c=='+'||c=='-')
{ return 1;
}
else if(c=='*'||c=='/'||c=='%')
{ return 2;
}
else if(c=='$')
{
return 3;
}
}
Your error is not where you indicated it.
It's these lines:
char c=*s->data;
stack* temp=*s;
*s=*s->next;
Which should be:
char c=(*s)->data;
stack* temp=*s;
*s=(*s)->next;
There's a handful other bad stuff too, e.g. you need to incude the stdlib.h and string.h header - and a handful of your functions needs a prototype before you call them. Compile with warnings on, and you'll see these errors.
I'm afraid the recursion in your struct _stack isn't right. The pointer to next should be also declared as struct _stack *next, instead of referencing to the type stack you created...
Very often that error pops up when your struct/class (C/C++/ObjectiveC/...) isn't defined in the scope of the access to a member.
Check your included files, might be the simple cause.
精彩评论