다른 종류의 구조체 (Prof 또는 Stud)를 가리키는 포인터 (주소 의미)가 포함 된 스택을 만들려고했습니다. 그러나 나는 그것을 관리하는 것처럼 보이지 않는다. 오류는 무한합니다. 코드는 다음과 같습니다.다른 구조체에 대한 포인터를 포함하는 스택
struct MyStack
{
int head;
void **stack;
int size;
};
struct stud
{
char flag;
char fname[50];
int semester;
};
struct prof
{
char flag;
char fname[50];
char course[30];
};
int InitStack(int size,struct MyStack *stack);
int InitStack(int size,struct MyStack *stack)
{
stack->size = size;
*stack->stack=(int *) malloc(size*sizeof(int)); //Is this RIGHT?
stack->head=-1;
return 0;
}
int main()
{
int size,sel;
size = GiveSize();
struct MyStack NewStack;
InitStack(size,&NewStack);
do{
sel=Menu();
Select(sel,NewStack.head,&NewStack);
}while (sel!=0);
return 0;
}
스택 (스텁과 Profs를 가리키는 포인터)을 스택에 어떻게 밀어 넣을 수 있습니까?
을 Heres 코드 :
는int CreateStud(struct MyStack *stack,char *name,int sem,int *head,int n)
{
struct stud newStud;
int thead=*head;
newStud.flag='s';
strcpy(newStud.fname,name);
newStud.semester=sem;
Push(stack,&thead,&newStud,n);
*head=thead;
return 0;
}
int Push(struct MyStack *stack,int *head, void *elem,int n)
{
if(*head>=n-1)
return 0;
stack->stack[++*head]=elem;
return 1;
}
"오류가 무한합니다"라는 걱정을하지 마십시오. 종종 작은 오류 하나가 발생하면 컴파일러가 첫 번째 오류 후 컴파일되는 오류로드를 생성 할 수 있습니다. – CatsLoveJazz
오류를 표시하십시오. – OldProgrammer
나는 오류를 수정했다. 실제로 프로그램이 충돌한다. – CosmaOne