2011-04-05 86 views
1

我得到印錯字母,當我運行這樣的功能:在功能設置指針

#include <stdio.h> 

void getletters(char *one, char *two) { 
    scanf("%c %c",&one, &two); 

    /* if i print one and two here, they are correct). */ 
} 

int main(void) { 
    char one, two; 
    getinput(&one, &two); 
    char *pone = &one; 
    char *ptwo = &two; 
    printf("your letters are %c and %c", *pone, *ptwo); /* These are coming out as the wrong characters */ 
} 

我有不正確的語法?我是否正確使用指針?

+0

僅供參考這裏沒有任何東西被「返回」。你的意思是「打印」我懷疑。 – 2011-04-05 15:52:34

+0

'getletters' /'getinput'是否與拼寫錯配? – Jon 2011-04-05 15:53:29

+0

什麼是getinput? – 2011-04-05 15:54:24

回答

1
void getletters(char *one, char *two) { 
    scanf("%c %c",&one, &two"); 
} 

您接受指針到字符,然後使用&接線員給指針到指針到字符,然後將其提供給一個函數期待指針到字符。

你也有一個錯字。

你應該寫:

void getletters(char *one, char *two) { 
    scanf("%c %c",one, two); 
} 
1

在您的scanf函數中,您不需要獲取變量的地址。嘗試:

void getletters(char *one, char *two) { 
    scanf("%c %c", one, two); // one and two are already pointers... 
} 
0

試試這個

void getletters(char *one, char *two) { 
    scanf("%c %c",one, two); 

    /* if i pint one and two here, they are correct). */ 
} 

一個和兩個已經指針在getLetters所以沒有必要通過他們的地址SCANF

此外,在主,你正在嘗試將指針傳遞給printf函數,這是錯誤的,你必須通過值傳遞參數:

printf("your letters are %c and %c", *pone, *ptwo); 
+0

不只是「不需要」;這樣做是錯誤的。 – 2011-04-05 15:55:10

+0

@Tomalak確實 – greydet 2011-04-05 15:56:23

0

試試這個。

#include <stdio.h> 
void getletters(char *one, char *two) { 
    scanf("%c %c",one, two); 
} 

int main (int argc, char const* argv[]) 
{ 
    char one, two; 
    getletters(&one, &two); 
    printf("your letters are %c and %c\n",one, two); /* These are coming out as the wrong characters */ 
    return 0; 
} 
0
scanf("%c %c",&one, &two"); 

什麼類型的說法確實在轉換說明%c期待?將其與&one&two的類型進行比較。