& Address operators in C Language

Source: Internet
Author: User

Example 1

# Include <stdio. h> int find (int * a); main () {int B = 1; int wait, test; test = find (& B); printf ("the memory address is: % d ", & test); scanf (" % d ", & wait) ;}int find (int * a) {return * ;}

Int B = 1; int test = find (& B); in this way, the call is actually performed: int * a = & B, a new integer pointer to B is created as a local variable within the scope of the find function.

Example 2

Function declaration: int find (int & a); function call: int B = 1; int n = find (B );

In this way, the call is actually performed: int & a = B, giving variable B an alias a, that is, a and B actually represent the same variable, no new variables were created during the call.

Example 3

Function declaration: int find (int a); function call: int B = 1; int n = find (B );

In this way, the call is actually performed: int a = B, that is, the normal variable value assignment operation. During the call, the local variable a within the scope of the find function is created, it has the same value as B.

C *, & Usage

& The variable address is used for calculation. For example, int * a = & B is used to retrieve the address of integer variable B and assign it to integer pointer, the content in a is the address of B, so a points to B.

* When used for calculation, it is to take the value in the address pointed to by the pointer, for example, int B = *, it is to assign the value of the content stored in the address pointed by integer pointer a to integer variable B.

In C language, & symbols are familiar to everyone. In addition to bitwise operations, & symbols can also be used to obtain variable addresses.

#include <stdio.h>main(){int a = 0;int wait;int *p = &a;printf("The value is: %d\n", *p);scanf("%d", &wait);}

In the above Code, the pointer p points to the address of variable. In C/C ++, each variable has its corresponding address. You can get the address of the variable by adding the & symbol before the variable identifier.

So can we write this? Int * p = & 0x01000;

This is obviously not acceptable. For a numeric constant, it has no address. The reason why a variable has an address is that it requires a storage unit to identify the variable (of course, the variable can also be directly mapped to a register ).

Let's look at the following code:

#include "stdio.h"int main(void)  {      int a = 0; // &a = 0x0012ff60      int *p = &*(int*)0x0012ff60;      printf("The value is: %d\n", *p);      return 0;  }

What is the above Code?

We have previously investigated the address of variable a -- 0x0012ff60, so the pointer p here actually points to the address of variable.

First, use 0x0012ff60 as the int *. In this case, it is equivalent to &.

Then * (int *) 0x0012ff60 indicates getting the content of variable.

Finally, "& * (int *) 0x0012ff60" indicates that the X (int *) 0x0012ff60 is removed, which is equivalent to (int *) &.

Therefore, the & here is different from the & In the first example. Here & is not the address, because a * (int *) 0x0012ff60 is not a variable and has no address. During compilation, the compiler creates a symbol table for each variable identifier, which stores various attributes related to the variable identifier, such as the type and address identifier. The logical address value can be determined after the connection. In short, & acts as an address fetch operation. If & is followed by a variable or function identifier. So here '&' indicates removing the Unquote.

As a result, we can conclude that the behavior result is determined at the time of Compilation When & is used as the address fetch operation, and *, The unreference operation (or content fetch) operation, the behavior result can only be determined at runtime.

Let's take a look at the following example to deepen our impression:

#include "stdio.h"int main(void) {     int a = 0;    int *p = &*&*&a;    printf("The value is: %d\n", *p);    return 0; }

In C ++, & can also represent references. This is not much to say.

#include "iostream" using namespace std;int main(void) {     int a = 0;    int &r = a;    cout << "The value is: " << r << endl;    return 0; }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.