简体   繁体   English

C中的指针地址怎么得到?

[英]How do you get the pointer address in C?

I finished three programs today, I am a student and I tend to overthink I can do more than I did.我今天完成了三个程序,我是一名学生,我倾向于过度认为我可以做的比我做的更多。 The problem is when I input 3 numbers, shouldn't there be three memory addresses?问题是当我输入3个数字时,不应该有3个memory地址吗? I feel like my program is missing a component that moves the pointer.我觉得我的程序缺少一个移动指针的组件。 The code is provided below.下面提供了代码。 Why in our career would memory address be used?为什么在我们的职业生涯中会使用 memory 地址?

#include <stdio.h>
#include <stdlib.h>


void options()
{
    puts("\t\t\tMain Menu");
    puts("\t\ta. Enter New Integer Value");
    puts("\t\tb. Print Pointer Address");
    puts("\t\tc. Print Integer Address");
    puts("\t\td. Print Integer Value");
    puts("\t\te. Exit");
    puts(" \tPlease enter an option from the menu: "); 
}//end options
    

int main(void) {
    //Intialize and Declare variables
    char menuOption;
    char valueStr [50];
    int i; 
    int *iPtr= NULL;

    while(menuOption != 'e')
    {
    //Four Menu Options
    options();
    menuOption = getc(stdin);
    getc(stdin);
    

    switch(menuOption){

    case 'a':
    //Enter New Integer Value 
    printf("Enter an Integer: ");
    gets(valueStr);
    i = atoi(valueStr);
        break;
    case 'b':
        //Print Pointer Address
    iPtr = &i;
    printf("Address of Pointer is %p\n", (void *) iPtr);
        break;
    case'c':
    //Print Integer Address 
    printf("Address of integer is %p\n", (void *) &i);
        break;
    case'd':
    //Print Integer Value 
    printf("The integer value is %d\n", i);
    break;
    case'e':
        break;
    default:
    printf("Invalid Option, try again\n");
    break;
    }//end Switch Statement
    }//end while statement
    return 0;

}

The variable i is always at the same location in memory throughout the execution of main , so no matter how many times you print its address, you'll see the same value.main的整个执行过程中,变量i始终位于 memory 中的同一位置,因此无论打印多少次它的地址,您都会看到相同的值。 That's an important property of the way pointers work in C: any given variable keeps the same address throughout its lifetime .这是 C 中指针工作方式的一个重要属性:任何给定变量在其整个生命周期中都保持相同的地址。 If this were not true, then pointers would be impossible to use reliably, because even if the pointer stayed the same, you'd have no way of knowing if the variable had moved out from under it.如果这不是真的,那么指针将无法可靠地使用,因为即使指针保持不变,您也无法知道变量是否已从其下方移出。

When the loop iterates for a second time, assigning to i overwrites the previous value at that location with the new value.当循环第二次迭代时,分配给i会用新值覆盖该位置的先前值。 It doesn't allocate a separate location for it, if that's what you were thinking.如果这是您的想法,它不会为其分配单独的位置。 Besides violating the above rule, that would also mean that any significant loop would run you out of memory almost immediately.除了违反上述规则外,这还意味着任何重要的循环都会让您几乎立即用完 memory。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM