我需要顯示使用指針爲我的作業,以獲得完整的功勞。問題與C指針
我有一個指針的問題。我的作業是模擬兔子和烏龜種族。比賽從1開始,在70完成。根據已經過去的時間百分比,兔子和烏龜可能會快速或緩慢地前進,可能會掉下一小塊或一大塊山,或者兔子可能會睡着。當一個人達到70歲時,比賽結束,並且該位置不可能是負面的(上一次和如果)。開關完成與運動有關的所有事情。你會注意到,如果開關前進或掉下道路,開關的價值就會被忽視。
我的問題是與指針。我必須使用他們,他們是我**!請幫助:
代碼:
#include <stdio.h>
int main(void)
{
int x, post, posl, num1, num2;
int *ptrt;
int *ptrl;
ptrt=&post;
ptrl=&posl;
printf("BANG!!!!\nY ARRANCAN\n");
post=posl=1;
do{
num1 = rand() % 10;
num2 = rand() % 10;
switch (num1){
case '0': *ptrt=&post+3; break;
case '1': *ptrt=&post+3; break;
case '2': *ptrt=&post+3; break;
case '3': *ptrt=&post+3; break;
case '4': *ptrt=&post+3; break;
case '5': *ptrt-=&post+6; break;
case '6': *ptrt-=&post+6; break;
case '7': *ptrt+=&post+1; break;
case '8': *ptrt+=&post+1; break;
case '9': *ptrt+=&post+1; break;
}
switch (num2){
case '2': *ptrl=&posl+9; break;
case '3': *ptrl=&posl+9; break;
case '4': *ptrl=&posl-12; break;
case '5': *ptrl=&posl+1; break;
case '6': *ptrl=&posl+1; break;
case '7': *ptrl=&posl+1; break;
case '8': *ptrl=&posl-2; break;
case '9': *ptrl=&posl-2; break;
}
if (post<1)
post=1;
if (posl<1)
posl=1;
}while (*ptrt<70||*ptrl<70);
printf("tortuga= %d\nliebre=%d\n" ,post ,posl);
return 0;
}
編譯器輸出:
tarea.c: In function ‘main’:
tarea.c:17: warning: assignment makes integer from pointer without a cast
tarea.c:18: warning: assignment makes integer from pointer without a cast
tarea.c:19: warning: assignment makes integer from pointer without a cast
tarea.c:20: warning: assignment makes integer from pointer without a cast
tarea.c:21: warning: assignment makes integer from pointer without a cast
tarea.c:22: error: invalid operands to binary - (have ‘int’ and ‘int *’)
tarea.c:23: error: invalid operands to binary - (have ‘int’ and ‘int *’)
tarea.c:24: warning: assignment makes integer from pointer without a cast
tarea.c:25: warning: assignment makes integer from pointer without a cast
tarea.c:26: warning: assignment makes integer from pointer without a cast
tarea.c:29: warning: assignment makes integer from pointer without a cast
tarea.c:30: warning: assignment makes integer from pointer without a cast
tarea.c:31: warning: assignment makes integer from pointer without a cast
tarea.c:32: warning: assignment makes integer from pointer without a cast
tarea.c:33: warning: assignment makes integer from pointer without a cast
tarea.c:34: warning: assignment makes integer from pointer without a cast
tarea.c:35: warning: assignment makes integer from pointer without a cast
tarea.c:36: warning: assignment makes integer from pointer without a cast
感謝您的幫助提前
我認爲這是因爲在嘗試操縱它們之前需要對它們進行去偏移。 – clamchoda
開關中所有'posl'和'post'之前的'&'字母不需要。 – osgx
你爲什麼使用指針? – outis