tags:

views:

105

answers:

2

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.
    }
}
+3  A: 

As far as the immediate issues you raised in your question are concerned, your code is perfectly fine. I.e. you are working with your "two-sided" array correctly (and no, you don't need any additional dereferences when you printf the values in the array).

The one problem I see is that you have forgotten to initialize (to assign initial values to) your record array, which means that the output will be garbage anyway, regardelss of how you work with it.

Also, as Dave Hinton noted in the comments, if you want to use the -ENDPOINT to +ENDPOINT range from the ptr origin, you need to initialize your ptr with &record[ENDPOINT], not with &record[ENDPOINT + 1]. Otherwise, if user enters the ENDPOINT value as the index, you'll end up with out-of-bounds access on the right end. (And the value of record[0] will always remain unused on the left end.)

P.S. I'd make quite a few "stylistic" changes, but they are beside the point in this case.

AndreyT
I think also they might mean to initialise `int *ptr = ` instead of `int *ptr = `.
Dave Hinton
@Dave Hinton: Yes, good catch!
AndreyT
Thanks for the help of initializing. That makes sense now. The problem I have now is convincing myself of what Dave said. I'm trying to draw it out, and I have 31 values for my arrary record from 0 to 30. But then if I want my origin for the ptr to have +15 and -15, doesn't it have to point to the number 15 on the record array, which is actually the 16th index of the array? I mean I see it does not in fact work with it equal to ENDPOINT + 1 and Dave is correct, but I don't see why it isn't correct equal to ENDPOINT + 1. Thanks again!
Crystal
A: 

Why don't you just subtract 15 from the number the user inputs? I.E.

ptr[userInput - ENDPOINT]++;

Where ptr points to the beginning of the array? This is much easier to understand and is more conventional.

rlbond
Quite the opposite. If the nature of the problem calls for mapping both negatibe and positive indices to the data, the approach used by the OP is significantly more elegant and readable. In fact, it is idiomatic.
AndreyT