How & a is a pointer to a if it generates the address a?

I am new to programming and am currently learning pointers in C.

  • I know that pointers are variables that contain or contain the address of another variable. Today, when I learned more about this, K & RI got confused in the line where “& a is a pointer to” in this swap function (& a, & b) on page 80. How is & a a pointer? This is not a variable, this is the address of a variable. I'm right?
  • I know that arguments can be passed to functions in two ways: call by value and call by reference. The value of the caller of the argument does not change in the first, but it can be changed in the second.

My question is: I read that if we want to change the value of a variable, we need to pass a pointer to it (that is, the location of the value that we want to change). What is meant by this? I mean, do we need to pass pointers to a function? And what is the meaning of this statement, "we must pass pointers to the place we want to change."

+4
source share
7 answers

A pointer is not a variable. A pointer is a value.

, C, , . , a, int a, a - , . int *x, x - , .

&. , &a - , a, a, ( ) . , :

int  a = 42;  /* a is a variable of type int,  and has value 42 */
int* x = &a;  /* x is a variable of type int*, and has value &a */

, , . - , , . , . :

actual_page p = ...; /* a page structure */
page_number n = &p;  /* a page number    */
+8

- . - , .

, 1 int a. a, 1 , , &a int* p .

, &a lvalue - , , -.

+4

a - - T, &a - , , - . &a T*, " T".

int x = 4;   // 4 has type int, so we can assign it to an int variable.
int *p = &x; // &x has type int*, so we can assign it to an int* variable.

, , , , . , ( ), ( ).

// modifying x modifies *p.
++x;
printf("%d %d\n", x, *p);

// modifying *p modifies x.
++*p;
printf("%d %d\n", x, *p);
+2

, , .

:

  • " " C. , , , .

  • , , C . C, , () , . , swap(&a, &b): , a b ( ).

+2

& a a, a?

C .

:

int a = 0; 

1 int.

& a , .

& , :

int a = 0;//a now exists in memory at a specific location;
int *b = {0}; // b is created in memory as a pointer, and can be assigned a location

b = &a; //b is assigned the location (address) of the variable a  

, & address-of, :

b = &x; : b () a.

+1
  • - .
  • When you pass by reference, you pass by address - to directly change the value.
+1
source

A pointer is a type of variable that stores the address for an object.

Basically, a pointer is an address.

Think of it as a piece of paper. When a number is printed on it, it integer(or another digital type).
A pointer is a piece of paper that says: “Data is on paper at location x,” where “location x” is the address of the item.

+1
source

All Articles