用C实现栈遇指针报错,求双指针正确用法指导
->) Error in Your Stack Implementation Hey there! Let's break down why you're seeing that confusing error with s->head->data or s->head->next—this is a super common pitfall when you're first working with structs and pointers in C, so we'll get it sorted out quickly.
First, Let's Clarify the Difference Between . and ->
Before diving into your code, let's recap the basics:
- Use
.to access members of a regular struct variable (not a pointer). For example:my_struct.member. - Use
->to access members of a struct that's pointed to by a pointer. For example:my_struct_ptr->memberis equivalent to(*my_struct_ptr).member.
So a double arrow like a->b->c only works if:
ais a pointer to a struct,bis a member of that struct, andbitself is a pointer to another struct,cis a member of the struct thatbpoints to.
Why Your Code Is Throwing the Error
The error message ('is a pointer; did you mean to use ‘->’?') sounds counterintuitive because you are using ->—but it means one of two things is wrong with your struct definitions or variable usage:
1. Your Stack's head Member Isn't a Pointer
Let's say you defined your structs like this (the mistake here is head is a regular Node, not a pointer):
// Wrong: Node is a regular struct, not a pointer typedef struct Node { char data; struct Node *next; } Node; typedef struct Stack { Node head; // ❌ head is a Node variable, not a Node* pointer } Stack;
In this case, s->head is a regular Node variable, so you can't use -> to access its data or next—you need to use . instead:
// Correct for this struct definition s->head.data; // Not s->head->data s->head.next; // Not s->head->next
But for a stack implementation, you almost always want head to be a pointer to the top node (so you can easily reassign it when pushing/popping). So fix your stack struct to make head a pointer:
// Correct: head is a pointer to Node typedef struct Stack { Node *head; // ✅ Now head is a pointer int size; // Optional: track stack size for convenience } Stack;
2. You're Using -> on a Regular Stack Variable (Not a Pointer)
If you declared your stack as a regular variable instead of a pointer:
Stack s; // Regular variable, not a pointer
Then accessing its members needs to use . first, not ->:
// Correct for a regular Stack variable s.head->data; // Not s->head->data s.head->next; // Not s->head->next
Most stack implementations use a pointer to the stack struct (especially if you're dynamically allocating memory with malloc), so if you have:
Stack *s = malloc(sizeof(Stack)); // Stack pointer
Then s->head->data is perfectly valid—as long as head is a Node* pointer (like we fixed in the first case).
Example Working Stack Code for Bracket Matching
Here's a quick snippet of how your structs and stack operations should look to avoid the error:
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> // Define the node struct for the linked list stack typedef struct Node { char data; struct Node *next; } Node; // Define the stack struct (head points to top of stack) typedef struct Stack { Node *head; int size; } Stack; // Initialize an empty stack void initStack(Stack *s) { s->head = NULL; s->size = 0; } // Push a character onto the stack void push(Stack *s, char c) { Node *newNode = malloc(sizeof(Node)); newNode->data = c; newNode->next = s->head; // Point new node to current top s->head = newNode; // Update top to new node s->size++; } // Pop the top character from the stack (returns '\0' if empty) char pop(Stack *s) { if (s->head == NULL) { return '\0'; } Node *temp = s->head; char popped = temp->data; s->head = temp->next; // Move top to next node free(temp); s->size--; return popped; } // Helper function to check if brackets match bool isMatching(char open, char close) { return (open == '(' && close == ')') || (open == '[' && close == ']') || (open == '{' && close == '}'); } // Bracket matching function bool checkBrackets(const char *str) { Stack s; initStack(&s); // Pass pointer to stack to init function for (int i = 0; str[i] != '\0'; i++) { char c = str[i]; if (c == '(' || c == '[' || c == '{') { push(&s, c); // Push opening brackets onto stack } else if (c == ')' || c == ']' || c == '}') { char top = pop(&s); if (!isMatching(top, c)) { return false; // Mismatched brackets } } } // Stack should be empty if all brackets matched return s.head == NULL; } int main() { char test[] = "{[()]}"; if (checkBrackets(test)) { printf("Brackets are matched!\n"); } else { printf("Brackets are NOT matched!\n"); } return 0; }
Quick Checklist to Fix Your Code
- Double-check your
Stackstruct: make sureheadis declared asNode *head;(a pointer). - Check how you're accessing the stack: if it's a regular variable, use
.head->data; if it's a pointer, use->head->data. - Ensure every time you use
->, the variable before it is a pointer to a struct.
内容的提问来源于stack exchange,提问作者neorus

