I'm trying to use an array to hold inputs for a survey that will have equal positive values on each side, but have a pointer point to the center of the array so negative pointer values can be used to access the array.
For example, the array would hold values from 0 to 30, the pointer would point to 15, and the user would be prompted to enter values between -15 and 15 where the array at the user's value would be incremented.
I'm ok if my logic isn't completely correct yet, but the problem I am having right now is incrementing the value (which I'm not sure if I'm doing it right by ptr[userInput]++
, and outputting those values with printf
. I saw someone else's post about passing the array to printf
actually is passing a pointer to the array, and that person said to de-reference it twice with either **ptr
or (*ptr)[0]
, but my compiler (Mac XCode) does not seem to like it.
Any thoughts? Here is my code. I commented where my questions were:
#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.
}
}