我的代碼「兼容的指針類型」 編譯用C
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void getData(short int *number, char *string)
{
printf("\nPlease enter a number greater than zero: ");
scanf("%hd", number);
printf("Please enter a character string: ");
scanf("%s", string);
}
void echoPair(short int *number, char *string)
{
printf("Number: %hd Character(s): %s\n", *number, string);
}
int main()
{
short int *number = 0;
char string[32] = {0};
printf("This program will ask you to enter a number greater than zero and \na character string with less than 32 characters \ninput.");
getData(&number, &string);
echoPair(&number, &string);
return(0);
}
的代碼工作正常,但我收到這些編譯器警告
warning: passing argument 1 of ‘getData’ from incompatible pointer type
warning: passing argument 2 of ‘getData’ from incompatible pointer type
warning: passing argument 1 of ‘echoPair’ from incompatible pointer type
warning: passing argument 2 of ‘echoPair’ from incompatible pointer type
如果做到這一點
getData(number, string);
echoPair(number, string);
的警告走開,但在我輸入getData函數中的第一個數字後,程序得到「分段錯誤:11」。
任何人都知道如何刪除警告並保持程序正常工作?
感謝
非常感謝,修復了一切。正如你可能知道的那樣,我是編程的新手,這隻會用於練習,但是感謝你對可能溢出的意見,請記住以後再使用! – afiser