עבור לתוכן

שאלה בC

Featured Replies

פורסם

למה אני מקבל את השגיאה (הפרדוקסלית) הזאת ?

'MakeEmpty' : 'void (Stack)' differs in levels of indirection from 'void (Stack)'

זה הפרוטוטייפ:

 void MakeEmpty(Stack S);

זה הפונקציה:

void MakeEmpty(Stack S)
{
if (S == NULL) { perror("must use CreateStack First"); exit(4); }
else
while (!IsEmpty(S))
Pop(S);
}

פורסם

הגדרת את STACK כמצביע?

איך קראת לפונקציה הזו?

פורסם

ראשית הרשה לי לברך אותך על השימוש ב-perror.

שנית, כדאי שתסדר את ה-indentation כי כמו שהוא כתוב כרגע בפורום הוא די מטורף.

הניחוש שלי זה שיש לך בעיה של מקרו.

יתכן ש-Stack מוגדר איפהשהו (אפילו בלי ידיעתך) כמקרו. אולי MakeEmpty מוגדר כמקרו. אולי אפילו S.

תזכור שזה יכול להיות אפילו בלי ידעתך (מאיזה קובץ של מישהו אחר) או בלי ששמת לב.

בכל מקרה כדי לעזור לך הלאה נצטרך את ההגדרה של Stack.

פורסם
  • מחבר

אין קריאה לפונקציה, זה קובץ h וקובץ מימוש שלו.

stack הוא מצביע לסטראקט

 typedef struct Node *PtrToNode;
typedef PtrToNode Stack;

node הוא סטראקט.

S לא מוגדר כמאקרו. (גם לא בלי ידיעתי)

פורסם

על איזו שורה בדיוק מצביעה השגיאה?

מה ההגדרה של IsEmpty ן-Pop?

(יעזור אם פשוט תעלה את כל הקוד...)

פורסם
  • מחבר

השגיאה מצביעה על השורה שמסומנת בכוכביות

stack.h

 #ifndef _Stack_h
#define _Stack_h
struct Node;
typedef int ElementType ;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;
int IsEmpty(Stack S);
Stack CreateStack(void);
void DisposeStack(Stack S);
void MakeEmpty(Stack S); //*********************
void Push(ElementType X, Stack S);
ElementType Top(Stack S);
void Pop(Stack S);
#endif

stack.c

#include "STACK.h"
#include <stdio.h>
#include <stdlib.h>
struct Node
{
ElementType Element;
PtrToNode Next;
};




/* routine to test whether a stack is empty - linked list implementation */
int IsEmpty(Stack S)
{
return S->Next == NULL;
}
/* routine to create an empty stack - linked list implementation */
Stack CreateStack( void )
{
Stack S;
S = (Stack)malloc( sizeof( struct Node)); //casting
if (S == NULL) { perror("Out of Space!"); exit(1); }
S->Next = NULL;
return S;
}




/* routine to empty an existing stack linked list implementation */
void MakeEmpty(Stack S)
{
if (S == NULL) { perror("must use CreateStack First"); exit(4); }
else
while (!IsEmpty(S))
Pop(S);
}



/* routine to push onto a stack - linked list implementation */
void Push(ElementType X, Stack S)
{
PtrToNode TmpCell;
TmpCell = (Stack)malloc(sizeof( struct Node)); //casting
if (TmpCell == NULL) { perror("Out of Space!"); exit(2); }
else
{
TmpCell->Element=X;
TmpCell->Next = S->Next;
S->Next = TmpCell;
}
}




/* routine to return top element in a stack - linked list implementation */
ElementType Top(Stack S)
{
if (!IsEmpty( S )) return S->Next->Element;
{ perror("Empty stack"); exit(2); }
return 0; /* return value used to avoid warning */
}




/* routine to pop from a stack - linked list implementation */
void Pop (Stack S)
{
PtrToNode FirstCell;
if (IsEmpty( S )) { perror("Empty stack"); exit(3); }
else
{
FirstCell = S->Next;
S->Next = S->Next->Next;
free (FirstCell);
}
}

פורסם

ואללה, אין לי מושג.

תגיד, למה הגדרת את Node בתוך הקובץ c ולא הקובץ h?

(אני בספק אם זו הבעיה, אבל זה סתם מתמיה)

פורסם
  • מחבר

מטעמי נוחות, הגדרתי אותו גם בh ועדיין מופיעה השגיאה ההזויה...

פורסם

באיזה קומפיילר אתה משתמש?

ניסיתי לקמפל אצלי (VS2005) וזה התקמפל (לא התלנקג' כי אין main, אבל זה לא רלוונטי).

פורסם
  • מחבר

יש לי 2003 והבדיקה של התרגיל מתבצעת על 2003

ותודה על המאמץ

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים