2017-03-12 12 views
0

簡単な質問。私は文字列の名前を受け取ってその2番目の文字を出力する関数を書こうとしていますが、それはコンパイルされません(http://ideone.com)、問題に対処できますか?私は関数にアドレスを送り、そのアドレスの文字にアクセスするように要求しているので、問題は見えません。私は取得していますC関数内の文字列へのポインタを使用して

#include <stdio.h> 

int main(void) { 
    char line[4] = "abc"; 
    test(line); 
    return 0; 
} 

void test(int point) { 
    point++; 
    printf("%c",*point); 
    return; 
} 

コンパイルエラーがある - ここ

Compilation error time: 0 memory: 10320 signal:0 
prog.c: In function ‘main’: 
prog.c:5:2: warning: implicit declaration of function ‘test’ [-Wimplicit-function-declaration] 
    test(line); 
    ^~~~ 
prog.c: At top level: 
prog.c:9:6: warning: conflicting types for ‘test’ 
void test(int point) { 
     ^~~~ 
prog.c:5:2: note: previous implicit declaration of ‘test’ was here 
    test(line); 
    ^~~~ 
prog.c: In function ‘test’: 
prog.c:11:14: error: invalid type argument of unary ‘*’ (have ‘int’) 
    printf("%c",*point); 
       ^~~~~~ 
+2

なぜ、 'char *'の代わりに 'int'を使うのですか?また、 'int'は参照を解除することもできません。 – BLUEPIXY

+1

'test(char * point) 'にする必要があります。 – smttsp

+1

エラーメッセージの単語を実際に読むと、それらは非常に特殊です。あなたは**実際にそれらを読む必要があります**。彼らはスクリーン上にスペースを取るだけではありません。彼らは実際に情報を伝える。 –

答えて

4

2つの問題。

まず、宣言される前に関数testを使用します。その関数は暗黙のうちに、指定されていない数の引数をとり、intを返す、つまりint test()と宣言されます。これは後で表示される定義void test(int point)と競合します。

第二の問題は、あなたがtestに(char *に減衰する)char配列を渡すということですが、機能はintを期待しています。

mainがそうそれはそれが使われる前に定義される前に testの定義を移動し、 intから char *pointパラメータを変更

#include <stdio.h> 

void test(char *point) { 
    point++; 
    printf("%c",*point); 
    return; 
} 

int main(void) { 
    char line[4] = "abc"; 
    test(line); 
    return 0; 
} 
0

1)関数プロトタイプが必要です。または、関数がメインの上に宣言される必要があります。

void test (int point) 

2)パラメータは、ポインタではなく、INTです。

関連する問題