我试图使用一个数组来保存一个调查的输入,每一边都有相等的正值,但有一个指向数组中心的指针,因此可以使用负指针值访问数组。指针,数组,printf
例如,数组的值将保持在0到30之间,指针将指向15,并且将提示用户输入介于-15和15之间的值,其中用户值的数组将增加。
如果我的逻辑还不完全正确,但是我现在遇到的问题是递增值(我不确定是否正确输入ptr[userInput]++
并输出这些值值printf
。我看到别人的帖子关于传递数组到printf
实际上是传递一个指针到数组,而且这个人说要用**ptr
或(*ptr)[0]
去引用它两次,但我的编译器(Mac XCode)似乎没有。?。喜欢它
任何想法这是我的代码,我评论在我的问题是:
#define ENDPOINT 15
#define TERMINATE 999
#define TEST_FILE "TestFile6.txt"
void RecordOpinions(void)
{
int record[2 * ENDPOINT + 1];
int *ptr = &record[ENDPOINT + 1];
int userInput;
int loopCount = -ENDPOINT;
printf("ptr:%d\n", *ptr); // this was a test for me trying to figure out how to
// print the value of the ptr.
printf("Please enter your opinion of the new TV show, Modern Family from ");
printf("-%d(worst) to 0 to +%d(best). Entering %d ", ENDPOINT, ENDPOINT, TERMINATE);
printf("will terminate and tabulate your results: \n");
scanf("%d", &userInput);
while (userInput != TERMINATE) {
if (userInput > ENDPOINT || userInput < -ENDPOINT) {
printf("Invalid entry. Enter rating again: ");
}
else {
printf("You entered: %d\n", userInput);
ptr[userInput]++; // not sure if this is the right way to increment
// the array at the user's input value.
}
scanf("%d", &userInput);
}
printf("Rating entry terminated.\n");
printf("Ratings:.\n");
for (; loopCount <= ENDPOINT;) {
printf("%d\n", ptr[loopCount++]); // this part is where I also need help
// in trying to print out the value of
// the ptr, not the address.
}
}
1+只是为了说明您的疑虑并向我们展示代码:) – Skurmedel 2010-01-29 17:43:54
输出是什么? – 2010-01-29 17:45:20