2015-01-14 136 views
-2

我在嘗試打印* p值時遇到此問題,而p指向列表的節點(顯然我想打印nodo.info值) 這裏是代碼,希望你明白:打印指針值

struct nodo { 
    int info; 
    struct nodo *prec; 
    struct nodo *succ; 
} ; 
typedef struct nodo nodo; 

int main (void) {  // just declaring my 3 nodos 
    struct nodo *p; 

    struct nodo anodo; 
    struct nodo bnodo; 
    struct nodo cnodo; 

    anodo.info = 200; 
    anodo.prec = NULL; 
    anodo.succ = NULL; 

    bnodo.info = 22; 
    bnodo.prec = NULL; 
    bnodo.succ = NULL; 

    cnodo.info = 2000; 
    cnodo.prec = NULL; 
    cnodo.succ = NULL; 

    anodo.succ = &bnodo;  
    bnodo.prec = &anodo; 
    bnodo.succ = &cnodo;  
    cnodo.prec = &bnodo; 

    p = &anodo; 

    printf("\n%d\n", checklist (p));  // calling function 

    return 0; 
} 

nodo *checklist (struct nodo *p) { 
    int j=0; 

    while (p != NULL) { 
     if (p->info >= p->succ->info) {  //if first element is major or same than next 
      p=p->succ; 

     } else { 
      while (p != NULL) { 
       if (p->info >= p->succ->info) {  //same 
        p=p->succ; 
        j++; 
       } else { 
        p = NULL; 
       } 
      } 
     } 
    } 

    while (j != 0) { 
     p = p->prec;  //used a counter to get back to the first nodo in wich next was less than prev 
     j--; 
    } 

    return p; 
} 

隨意問任何細節

+0

您需要取消引用指針從函數返回,並傳遞給'printf'。你可以通過使用'*(清單(p))...' –

+0

那麼打印'p-> info'? – 5gon12eder

回答

1

您需要返回nodo.info然後

int checklist (struct nodo *p) { 
    int j=0; 

    while (p != NULL) { 
     /* avoid this p->succ->info */ 
     if (p->info >= p->succ->info) {  //if first element is major or same than next 
      p = p->succ; 
     } else { 
      while (p != NULL) { 
       if (p->info >= p->succ->info) {  //same 
        p = p->succ; 
        j++; 
       } else { 
        p = NULL;       
       }   
      } 
     } 
    } 

    while (j != 0) { p = p->prec;  //used a counter to get back to the first nodo in wich next was less than prev 
     j--; 
    } 
    return p->info; 
} 

或主

int info 
struct nodo *found; 

found = checklist(p); 
if (found != NULL) 
    printf("\n%d\n", found->info); 
3
p = checklist (p); 
if (p) 
    printf("\n%d\n", p->info);