开发者

Pointers, Arrays, printf

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.
    }
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜