Duck
Duck

Reputation: 53

Getting request for member not a structure or union error

I am trying to implement a stack. I have the following stack struct:

struct stackNode 
{
  char data;
  struct stackNode *nextPtr;
};

typedef struct stackNode StackNode; 
typedef StackNode *StackNodePtr;

When I try to use this for my pop method, I get a number of error messages. My pop method is:

char pop(StackNodePtr *topPtr )
{       
  if (IsEmpty(topPtr)) 
  {
    printf("Can't pop element from stack: stack is empty.\n");
    return 'n'; // arbitrary char to end if, will adjust this later.
  }
  char c = topPtr->data; //save data to be returned

  // temporary StructNodePtr to save data
  StackNodePtr temp; // temporary StackNodePtr
  temp = malloc(sizeof(StackNodePtr));
  temp->data = topPtr->data;    //line 52, first error
  temp->nextPtr = topPtr->nextPtr;

  //replace values in topPtr, this section I have yet to debug, is likely faulty.
  topPtr->data = temp->nextPtr->data;   //line 56, third error
  topPtr->nextPtr = temp->nextPtr; 

  free(temp);      
  return (c);
}

I get the following error messages:

52:22: error: request for member ‘data’ in something not a structure or union
53:25: error: request for member ‘nextPtr’ in something not a structure or union
56:10: error: request for member ‘data’ in something not a structure or union
57:10: error: request for member ‘nextPtr’ in something not a structure or union

If I make temp a StackNode (and adjust -> to . accordingly), I get the error "request for member ‘nextPtr’ or ’data’ in something not a structure or union". In the question given to me, topPtr must me a StackNodePtr.

Can someone help me trouble shoot this?

Upvotes: 0

Views: 4092

Answers (1)

nullptr
nullptr

Reputation: 11058

Your topPtr is pointer to pointer to struct (StackNodePtr *topPtr = struct stackNode **topPtr). So you should write (*topPtr) -> data instead of topPtr -> data.

In fact, the line char c = topPtr->data; should also cause an error.

Upvotes: 2

Related Questions