指针,数组,printf [英] Pointers, Arrays, printf

查看:90
本文介绍了指针,数组,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.

例如,数组将保存从0到30的值,指针将指向15,并且将提示用户输入介于-15和15之间的值,其中将以用户的值递增数组.

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.

我还可以,如果我的逻辑还不完全正确,但是我现在遇到的问题是增加值(我不确定我是否按ptr[userInput]++正确地执行了该操作,然后输出我看过别人关于将数组传递给printf的帖子,实际上是在传递指向数组的指针,那个人说用**ptr(*ptr)[0]取消引用两次,但是我的编译器(Mac XCode)似乎不喜欢它.

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

推荐答案

就您在问题中提出的直接问题而言,您的代码非常好. IE.您正在正确地使用双面"数组(并且不,在printf数组中的值时,您不需要任何其他的取消引用).

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

我看到的一个问题是您忘记了初始化record数组(向其分配初始值),这意味着无论如何,输出都将是垃圾,这与您的工作方式有关. /strong>

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.

此外,正如Dave Hinton在评论中指出的那样,如果要使用从ptr起源到-ENDPOINT+ENDPOINT的范围,则需要使用&record[ENDPOINT]而不是&record[ENDPOINT + 1].否则,如果用户输入ENDPOINT值作为索引,那么您将最终在右侧获得越界访问权限. (record[0]的值将始终在左端保持未使用状态.)

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.我会进行很多样式"更改,但是在这种情况下,它们是不重要的.

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

这篇关于指针,数组,printf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆