c language pointer study notes

pointer

1. Operator &
  1. Function: To obtain the address of a variable, the operand must be a variable.
  2. Whether the size of the address is the same as int depends on the compiler. 32 bits are 4 bytes, and 64 bits are 8 bytes.
  3. So the output of the address cannot be %x, it should be %p.
2. Pointer
  • Is the variable that holds the address
int i;
int* p = &i;
int* p,q;
int *p,q;
  • Here p is a pointer variable, which puts the address of i, pointing to i.
  • Note that the expressions 3 and 4 above are equivalent, * is added to p, p is a pointer variable, and q is just an ordinary variable.
3. Pointer as a reference
  1. Function: Pass the address of a variable to the function, and the function has the ability to access the variable outside the function.
  2. To access the variable at this address, you need to use the unary operator *, and then you can use and rewrite this variable through the pointer.
int i = 10;
f(&i);
void f(int *p)
{
    
    
   int k = *p;
   *p = k+1;
}

Finally k = 10, i = 11

  • The internal storage of p is the address of variable i, and *p is i, which are equivalent.
4. Application of pointers
  1. Scenario 1: Functions need to return multiple values, some of which can only be returned by pointers. The parameters passed in are actually variables that need to save the results brought back. Such as swapping the values ​​of the two:
int a = 5;
int b = 6;
void swap(int *pa, int *pb)
{
    
    
    int t = *pa;
    *pa = *pb;
    *pb = t;
}
  1. Scene 2: Return separately. The function returns the status of the operation, and the result is returned via a pointer.
5. Common mistakes in pointers
  • Define the pointer variable and start using it without pointing to any variable.
6. Pointers and arrays
  • The array name is a special pointer, which represents the first address of the array, no need to use & to get the address.
  • But what the array unit expresses is the variable, need to use & to get the address.
  • The [] operator can be done on arrays or pointers.
  • The * operator can be done on pointers or on arrays.
  • Array variables are const pointers, so they cannot be assigned.

int a[ ] <==>int * const a


int a[5], b[5];
a = b;
// This is not possible

7. Pointer and const
  • There are two functions of const here.
  1. The pointer is const, which means that once the pointer gets the address of a variable, it can no longer point to other variables. It can be understood that the value of pointer p can no longer be changed, but the value of *p can be changed.

int* const q = &i; // q是const
*q = 26; // ok
q++; // error

  1. *p is const, which means that the pointed variable cannot be modified through this. Note that neither the pointer nor the pointing variable is const.

const int *p = &i;
*p = 26; // error (*p是const)
i = 26; // ok
p = &j; // ok

  • So there are only two ways to write, depending on whether const is before or after *. Either the pointer cannot be modified, or the pointer cannot be modified.

int *const p1 = &i;


const int *p1 = &i;
int const *p1 = &i;

  • It is always possible to convert a non-const value to const.

void f(const int *x)
This can not only pass the value to the parameter with less bytes, but also avoid the function to modify the external variables

8. Pointer operations
  • Operations that pointers can perform: +, -, +=, -=, ++,--.
  • You can also subtract between two pointers. The result of the subtraction is in addition to sizeof, and the gap between the two is obtained.
  • Adding one to the pointer is not actually adding one, but adding the sizeof of the pointer type to move down one unit.
  • *p++ means to take out the data of the pointed variable, and then move p to the next position.
9. Pointer comparison
  • <, <=, ==, >, >=, != can do all pointers.
10. Types of pointers
  • No matter what type it points to, all pointers are the same size because they are all addresses.
  • Pointers to different types cannot be directly assigned to each other. This is to avoid using the wrong pointer.
11. Dynamic memory allocation
  • When inputting data, if the number of data is a variable, variables can be used as the size of the array definition in C99, and dynamic memory allocation can also be used.
  • The header file <stdlib.h> needs to be included when used, in the following form:
int *a = (int*)malloc(n*sizeof(int);

n is the number of data (array size), where space is allocated in bytes, and a can be used as an array.

  • malloc function
  1. Header #include <stdlib.h>
  2. void* malloc(size_t size), because the return type is void*, the corresponding type coercion must be added.
  3. The requested space is in bytes.
  4. Release the space after applying for it.
free (a); 
  • free function
  1. Return the requested space to the system for use with malloc.
  2. You can only return the first address of the space you applied for.

Guess you like

Origin blog.csdn.net/weixin_45688536/article/details/104193729