Why Use Pointers in C?

后端 未结 4 1599
南笙
南笙 2020-12-29 07:01

I\'m still wondering why in C you can\'t simply set something to be another thing using plain variables. A variable itself is a pointer to data, is it not? So why make poi

相关标签:
4条回答
  • 2020-12-29 07:09

    A variable itself is a pointer to data

    No, it is not. A variable represents an object, an lvalue. The concept of lvalue is fundamentally different from the concept of a pointer. You seem to be mixing the two.

    In C it is not possible to "rebind" an lvalue to make it "point" to a different location in memory. The binding between lvalues and their memory locations is determined and fixed at compile time. It is not always 100% specific (e.g. absolute location of a local variable is not known at compile time), but it is sufficiently specific to make it non-user-adjustable at run time.

    The whole idea of a pointer is that its value is generally determined at run time and can be made to point to different memory locations at run time.

    0 讨论(0)
  • 2020-12-29 07:10

    One common place where pointers are helpful is when you are writing functions. Functions take their arguments 'by value', which means that they get a copy of what is passed in and if a function assigns a new value to one of its arguments that will not affect the caller. This means that you couldn't write a "doubling" function like this:

    void doubling(int x)
    {
        x = x * 2;
    }
    

    This makes sense because otherwise what would the program do if you called doubling like this:

    doubling(5);
    

    Pointers provide a tool for solving this problem because they let you write functions that take the address of a variable, for example:

    void doubling2(int *x)
    {
        (*x) = (*x) * 2; 
    }
    

    The function above takes the address of an integer as its argument. The one line in the function body dereferences that address twice: on the left-hand side of the equal sign we are storing into that address and on the right-hand side we are getting the integer value from that address and then multiply it by 2. The end result is that the value found at that address is now doubled.

    As an aside, when we want to call this new function we can't pass in a literal value (e.g. doubling2(5)) as it won't compile because we are not properly giving the function an address. One way to give it an address would look like this:

    int a = 5;
    doubling2(&a);
    

    The end result of this would be that our variable a would contain 10.

    0 讨论(0)
  • 2020-12-29 07:11

    No, a variable is not a pointer to data. If you declare two integers with int x, y;, then there is no way to make x and y refer to the same thing; they are separate.

    Whenever you read or write from a variable, your computer has to somehow determine the exact location of that variable in your computer's memory. Your computer will look at the code you wrote and use that to determine where the variable is. A pointer can represent the situation where the location is not known at the time when you compile your code; the exact address is only computed later when you actually run your code.

    If you weren't allowed to use pointers or arrays, every line of code you write would have to access specific variables that are known at compile time. You couldn't write a general piece of code that reads and writes from different places in memory that are specified by the caller.

    Note: You can also use arrays with a variable index to access variables whose location is not known at compile time, but arrays are mostly just syntactical sugar for pointers. You can think about all array operations in terms of pointer operations instead. Arrays are not as flexible as pointers.

    Another caveat: As AnT points out, the location of local variables is usually on the stack, so they are a type of variable where the location isn't known at compile time. But the only reason that the stack works for storing local variables in a reentrant function is that your compiler implements hidden pointers called the stack pointer and/or frame pointer, and functions use these pointers to find out which part of memory holds their arguments and local variables. Pointers are so useful that the compiler actually uses them behind your back without telling you.

    0 讨论(0)
  • 2020-12-29 07:26

    Another reason: C was designed to build operating systems and lots of low level code that deals with hardware. Every piece of hardware exposes its interface by means of registers, and on nearly all architectures, registers are mapped into the CPU memory space, and they have not to be in the same address always (thanks to jumper settings, PnP, autoconfig, and so on)

    So the OS writer, while writing a driver for instance, needs a way to deal with what seems memory locations, only that they don't refer RAM cells.

    Pointers serve to this purpose by allowing the OS writer to specify what memory location he or she wants to access to.

    0 讨论(0)
提交回复
热议问题