2014-04-01 5 views
0

다른 종류의 구조체 (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; 
} 
+1

"오류가 무한합니다"라는 걱정을하지 마십시오. 종종 작은 오류 하나가 발생하면 컴파일러가 첫 번째 오류 후 컴파일되는 오류로드를 생성 할 수 있습니다. – CatsLoveJazz

+1

오류를 표시하십시오. – OldProgrammer

+0

나는 오류를 수정했다. 실제로 프로그램이 충돌한다. – CosmaOne

답변

0

귀하의 InitStack 기능

int InitStack(int size,struct MyStack *stack) 
{ 
    stack->size = size; 
    stack->stack= malloc(size * sizeof(void*)); 
    stack->head=-1; 
    return 0; 
} 

해야 푸시 기능은

int Push(struct MyStack *stack, void *str) 
{ 
    /* Check if stack is full */ 
    stack->head++; 
    stack->stack[stack->head] = str; 
    return 0; 
} 

등 뭔가 당신이

으로 사용할 수 있어야한다 0
struct stud s1; 
struct prof p1; 

Push(&NewStack, &s1); 
Push(&NewStack, &p1); 
+0

나는 UR 방법을 시도했다. 그러나 내가 무언가를 푸시하려고 할 때. .. 나는 대답을 편집했다 ... 만약 당신이 함수를 반환 할 때 삭제 된 함수에서 구조체를 만들고 있기 때문에 – CosmaOne

+0

을 원한다면 더 많은 코드를 게시 할 수있다. 글로벌 변수 – obareey

+0

을 생성해야합니다. 글로벌 변수가 없으면이 변수를 사용할 수 없습니까? – CosmaOne

관련 문제