2017-07-11 5 views
0

私はCでプログラムを書いています。c-関数内で構造体メンバを代入する際のエラー

これの一部として、私は最初にすべての可能なシフト、0-26を解読するメッセージで実行します。私はシフトとメッセージを格納する構造体を使用します。これを行うために、構造体をポインタとして関数に渡しました。しかし、構造体のメッセージメンバーを解読したメッセージに変更しようとすると、次のエラーが表示されます。 'strcpy(s-> message、cipherText)'行に ' - >'( 'int' '

私は構造体メンバにもローカル変数を割り当てていますが、これは問題なく動作します。

コード:

#include <stdio.h> 
#include <string.h> 
#define ENCRYPT 0 
#define DECRYPT 1 

struct Solution { 
    int key; 
    char message[]; 
}; 

void Ceaser(struct Solution *s, char cipherText[], int mode); 

void main(){ 
    struct Solution solutions[26]; 
    char cipherText[] = "lipps, asvph."; 

    for (int i = 0; i <= 26; ++i) { 
     solutions[i].key = i; 
     Ceaser(&solutions[i], cipherText, DECRYPT); 
     printf("Key: %d\tPlain text: %s\n", solutions[i].key, 
     solutions[i].message); 
    } 
} 

void Ceaser(struct Solution *s, char cipherText[], int mode) { 

    int len = strlen(cipherText); 
    int c; 
    int key = s->key; 

    for (int s = 0; s <= 26; ++s) { 
     if (mode == DECRYPT) { 
      key *= -1; 
     } 

     for (int i = 0; i < len; ++i) { 
      c = cipherText[i]; 

      if (c >= 'A' && c <= 'Z') { 
       cipherText[i] = 'A' + ((c + key - 'A') % 26);   
      } else if (c >= 'a' && c <= 'z') { 
       cipherText[i] = 'a' + ((c + key - 'a') % 26);   
      } 
     } 
    //Error occurs below 
    strcpy(s->message, cipherText); 
    } 
} 
+1

's-> message':' char message []; 'にはスペースがありません。 – BLUEPIXY

+2

問題は、sという名前の2つの変数があることです。内側のintは、外側のSolution *をシャドウします。 gccを使用している場合、-Wshadowフラグはこのような問題を見つけるのに気の利いたものです。 @BjornA。 –

+0

ありがとう、私はそれが私が紛争に気付かなかったと信じることができない単純なものになるだろうが。コンパイラのヒントもありがとう。 – Henry

答えて

0

問題は、あなたが正しくfor(int s=...を閉じていないと、コンパイラはs->によってあなたの代わりにSolution* s関数パラメータのループ変数sを参照していると考えていることです。

無効なタイプエラーが発生します。

次は、固定された(そして、より良いインデント)バージョンです:

void Ceaser(struct Solution *s, char cipherText[], int mode) { 
    int len = strlen(cipherText); 
    int c; 
    int key = s->key; 

    for (int s = 0; s <= 26; ++s) { 
    if (mode == DECRYPT) { 
     key *= -1; 
    } 

    for (int i = 0; i < len; ++i) { 
     c = cipherText[i]; 

     if (c >= 'A' && c <= 'Z') { 
     cipherText[i] = 'A' + ((c + key - 'A') % 26); 
     } else if (c >= 'a' && c <= 'z') { 
     cipherText[i] = 'a' + ((c + key - 'a') % 26); 
     } 
    } 
    } //<--------was missing 

    strcpy(s->message, cipherText); 
} 

あなたは何を得ることは非常に有益である働く-Wshadowコンパイラの警告を聞かせてください。

g++ 
test.cpp:65:30: note: shadowed declaration is here 
void Ceaser(struct Solution *s, char cipherText[], int mode) { 

clang++ 
note: previous declaration is here 
void Ceaser(struct Solution *s, char cipherText[], int mode) { 


icpc 
warning #1599: declaration hides parameter "s" (declared at line 65) 
    for (int s = 0; s <= 26; ++s) { 
0
void Ceaser(struct Solution *s, char cipherText[], int mode){ 
.... 
for (int s = 0; s <= 26; ++s){ 

あなたがここに明らかに競合を見ることができない - あなたは二度同じ変数名を使用しています。 int sは、forループのスコープについて前の宣言sをオーバーライドします。これにより、コードは以前に宣言されたコードと対話できなくなります。

最初のsを適切な変数名(つまり「解決策」)に変更して、競合を避けるとともに、変数の目的が明らかであることも明白です。単一文字変数は、たとえforループで使用されているとしても、それが何であるかはあまり明確ではありません。

関連する問題