In my program, I want to input some numbers until I input 0. When I input 0, the program must stop and show the numbers in order. It's almost finish but I have one problem. I must not use an array
, it's forbidden .
在我的程序中,我想输入一些数字,直到我输入0.当我输入0时,程序必须停止并按顺序显示数字。它几乎完成但我有一个问题。我不能使用数组,这是禁止的。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
int number;
int *ptr;
int i = 0, j = 0;
ptr = &number;
number = (int*)malloc(2000);
do{
printf("Enter a number : ");
scanf("%d",ptr);
printf("\n######\n");
printf("%d. number = %d \t%p\n",i+1,*(ptr),(ptr+i));
printf("\n######\n");
i++;
} while(((number)) != 0);
printf("\n!!!!############!!!!\n");
for(j = 0 ; j < i; j++){
number=number+j;
printf("%d. number = %d \t%p\n",j+1,(number),&(number));
}
return 0;
}
1 个解决方案
#1
0
You code has a number of problems. I think you tried something too challenging for your level of understanding right now.
你的代码有很多问题。我觉得你现在尝试了一些对你的理解水平来说太具挑战性的事情。
You should learn about pointers from the beginning and make sure you understand the *
and &
operators. Try writing some small simple programs so you have a good grasp on them.
您应该从头开始学习指针,并确保您了解*和&运算符。尝试编写一些简单的小程序,以便掌握它们。
From there you can move on to scanf
and malloc
, since they rely on heavily pointers.
从那里你可以继续使用scanf和malloc,因为它们依赖于大量的指针。
Here is a working version of your code:
这是您的代码的工作版本:
int main()
{
int* number; // this is should be a pointer
//int *ptr;
int i = 0, j = 0;
//ptr = &number;
number = (int*)malloc(2000);
do{
printf("Enter a number : ");
// scan an integer into the ith place in memory
// after the address pointed to by "number"
scanf("%d",number+i); //number+i is already an address, don't use &
printf("\n######\n");
printf("%d. number = %d \t%p\n",i+1,*(number+i),number+i);
printf("\n######\n");
i++;
// need to use a -1 since we incremented i
}while(*(number+i-1) != 0);
printf("\n!!!!############!!!!\n");
for(j = 0 ; j < i; j++){
//=number+j;
printf("%d. number = %d \t%p\n",j+1,*(number+j),number+j);
}
return 0;
}
#1
0
You code has a number of problems. I think you tried something too challenging for your level of understanding right now.
你的代码有很多问题。我觉得你现在尝试了一些对你的理解水平来说太具挑战性的事情。
You should learn about pointers from the beginning and make sure you understand the *
and &
operators. Try writing some small simple programs so you have a good grasp on them.
您应该从头开始学习指针,并确保您了解*和&运算符。尝试编写一些简单的小程序,以便掌握它们。
From there you can move on to scanf
and malloc
, since they rely on heavily pointers.
从那里你可以继续使用scanf和malloc,因为它们依赖于大量的指针。
Here is a working version of your code:
这是您的代码的工作版本:
int main()
{
int* number; // this is should be a pointer
//int *ptr;
int i = 0, j = 0;
//ptr = &number;
number = (int*)malloc(2000);
do{
printf("Enter a number : ");
// scan an integer into the ith place in memory
// after the address pointed to by "number"
scanf("%d",number+i); //number+i is already an address, don't use &
printf("\n######\n");
printf("%d. number = %d \t%p\n",i+1,*(number+i),number+i);
printf("\n######\n");
i++;
// need to use a -1 since we incremented i
}while(*(number+i-1) != 0);
printf("\n!!!!############!!!!\n");
for(j = 0 ; j < i; j++){
//=number+j;
printf("%d. number = %d \t%p\n",j+1,*(number+j),number+j);
}
return 0;
}