Get the App
SLTechnology News&Howtos  ›  Development  › 

Why there are no references in C language, only pointers.

Shulou Source: shulou.com Published: 2022-06-02 22:56:51 10月03日 Update

This article mainly explains why there are no references in C language and there are only pointers. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn why there are no references in C language and there are only pointers.

What is a citation?

To put it bluntly, a reference is to create a new name for an existing variable, and if the program does something about the reference alias, it is actually a change to the original target.

C + + reference instance

# include # include void exchange (int & x, int & y) {int t; t = x; x = y; y = t;} int main () {int awagh b; scanf ("% d% d", & a, & b); exchange (ameme b); printf ("% d% d\ n", amaine b); getchar (); return 0;}

Program output

12 34 34 12-Process exited after 3.121 seconds with return value 0 Please press any key to continue. . .

What is the C language?

The C language is an ever-changing pointer, and when references appear in C++, it makes programming very friendly. If you look at the operation above, it looks very clear.

But we can also use pointers to do the above.

Instance code

12 34 34 12-Process exited after 3.121 seconds with return value 0 Please press any key to continue. . .

Program output

12 56 56 12-Process exited after 2.477 seconds with return value 0 Please press any key to continue. . .

Leave a topic for discussion

Discuss what the following program outputs?

# include # include void exchange (int * x, int * y) {int * t = x; * x = * y; * y = * t;} int main () {int a, b); scanf ("% d% d", & a, & b); exchange (& a, & b); printf ("% d% d\ n", aMagna b); getchar (); return 0 At this point, I believe you have a deeper understanding of why there is no reference to the C language and only pointers. You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Pointers languages programs only surface output content examples that is learning practical deeper unchanged to put it bluntly code interests aliases variables names practicality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker Apple NVidia OPPO Reno