2016-05-07 1 views
-2

最初の質問はstackoverflowで、私はポインタで単語のシーケンスをソートする必要があります誰も助けてくれますか? それは>> NOD_urmator- nod_curent-何NOD_urmatorについて一言単語シーケンスをポインタでソートするC++

#include <stdio.h> 
#include <stdlib.h> 
#include <cstring> 

struct NOD 
{ 
    char word[20]; 
    struct NOD *NOD_next; 
}; 

void sort_list(struct NOD *prim) 
{ 
    char tmp[50]; 
    struct NOD *nod_curent;  

    nod_curent=prim; 

    while(nod_curent!=NULL||nod_curent->NOD_urmator!=NULL) 
    { 
     if(strcmp(nod_curent->cuvant,nod_word->NOD_urmator->word)>0) 
     { 
      strcpy(tmp,nod_curent->word); 
      strcpy(nod_curent->word,nod_curent->NOD_urmator->word); 
      strcpy(nod_curent->NOD_urmator->word,tmp); 
     } 
     nod_curent=nod_curent->NOD_urmator; 
    } 

    while(nod_curent!=NULL) 
    { 
     printf(" %s\n", nod_curent->cuvant); 
     nod_curent=nod_curent->NOD_urmator; 
    } 
} 
+0

と同じだということ

while ((nod_curent != NULL) && (nod_curent->NOD_urmator != NULL)) 

は、Cのように見えますが、あなたが明示的にC++を求めるように、私は間違ったタグを削除しました。 – Olaf

+0

あなたはどのようなエラーや予想される結果が何であるか説明できますか? –

答えて

1

1)大丈夫だ-notてください?あなたはそれを使用しますが、それはNODで定義されていません。 NOD_nextと同じですか?

2)について約cuvant?あなたはそれを使用しますが、それはNODで定義されていません。 wordと同じですか?

3)について約nod_word?あなたはそれを使用しますが、それはsort_list()で定義されていません。 nod_curentと同じですか?

4)次の試験では(私は)確信している間違った(私は仮定)と危険です

while(nod_curent!=NULL||nod_curent->NOD_urmator!=NULL) 

nod_curentNULLが第一の条件(nod_curent!=NULLを)失敗しているので、 "||" 状態が暗示する場合ので、 2番目のもの(nod_curent->NOD_urmator!=NULL)がテストされたが、nod_curent == NULLとすると、nullポインタを逆参照することになります。クラッシュ!

あなたは "and"演算子を使用するつもりでしたか?それは

while (nod_curent && nod_curent->NOD_urmator) 
関連する問題