I am new to programming and am currently learning pointers in C.
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."
A pointer is not a variable. A pointer is a value.
, C, , . , a, int a, a - , . int *x, x - , .
a
int a
int *x
x
&. , &a - , 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 */
- . - , .
, 1 int a. a, 1 , , &a int* p .
1
int* p
, &a lvalue - , , -.
a - - T, &a - , , - . &a T*, " T".
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);
, , .
:
" " C. , , , .
, , C . C, , () , . , swap(&a, &b): , a b ( ).
swap(&a, &b)
b
& a a, a?
C .
int a = 0;
1 int.
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.
b = &x;
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.
integer