我是C的新手。我想創建一個程序,用戶可以用數字填充數組,將特定數字設置爲某個數組寄存器或讀取寄存器的值。 除閱讀價值外,一切工作至今。我總是得到0回來,爲什麼? 這裏是我的代碼的getValue();爲什麼我的數組中的值不安全?
void getValues(int getArray[]){
fflush(stdin);
printf("which slot do you want to read?\n");
gvinput = getchar();
ptr = getArray[gvinput];
printf("The value is: %d \n",ptr);
start();
}
,這裏是整個代碼..
#include <stdio.h>
#include <stdlib.h>
int* ptr;
int list[200];
int x = 0;
int i = 0; // variable used by for-Loop in setList
int j = 0; // variable used by for-Loop in getList
int c; // C used for new Game
int input;
int g1; //Value for getValue
int option; //start Value
int gvinput;
int main()
{
start();
return 0;
}
void setList(int sizeOfList)
{
for (i = x; i <= sizeOfList; i++)
{
list[i] = i;
}
}
void getList()
{
for(j = x; j < i ; j++)
{
printf("At %d we got the value %d with the adress %d\n",j,list[j],&list[j]);
}
}
void startList()
{
fflush(stdin);
printf("Please enter number between 0 and 30\n ");
scanf("%d",&input);
if(input > 30 || input == 0)
{
printf("The Number is not between 0 and 30\n");
startList();
}
setList(input);
getList();
fflush(stdin);
start();
}
void setValues(int l[])
{
fflush(stdin);
int v;
int loc;
printf("please enter what value you want to safe\n");
scanf("%d",&v);
fflush(stdin);
printf("Where do you want to save it?\n");
scanf("%d",&loc);
l[loc] = v;
printf("we got at slot %d the value %d\nThe Adress is: %d.",loc,l[loc],&l[loc]);
start();
}
void getValues(int getArray[]){
fflush(stdin);
printf("which slot do you want to read?\n");
gvinput = getchar();
ptr = getArray[gvinput];
printf("The value is: %d \n",ptr);
start();
}
void start(){
fflush(stdin);
printf("[L] = generate Slots\n");
printf("[S] = set a Value at specific slot\n");
printf("[G] = get a Value from a specific slot\n");
option=getchar();
if(option == 'L'){
startList();
}
if(option == 'S'){
setValues(list);
}
if (option =='G'){
getValues(list);
}
}
將是巨大的,如果有人可以幫助並給出提示
'的printf( 「的值是:%d \ n」,* PTR);' – moeCake
fflush(標準輸入) - >未定義的行爲 –
@KaustavRay:不在Linux上;不在Windows上。 –