C language portal - Pointer

And finally to the essence of the place, this is indeed a bit ignorant, always I feel it's too much trouble, but also unhappy that says, still miss py or java, but there is no way, or to keep learning.

First, the operator &

  1. scanf("%d" , &i); 里的&
  2. Get address of a variable, its operand must be a variable
  3. Int address whether the size of the same depends on the compiler
#include <stdio.h>
int main(void)
{
    int i = 0;
    printf("0x%x\n", &i);
    // 0x62fe4c
    return 0;
}

& Can not take the address

& Can not take the address of something without an address

  1. &(a+b) ?
  2. &(a++) ?

Second, Pointer

  1. If we can get the address of a variable of a transfer function, you can access this variable through the address within that function?
  2. scanf("%d" , &i);
  3. scanf () prototype should be like? We need a parameter save another variable address, how to express variable can hold the address

pointer

Is the variable that holds the address , * the p-

// p是一个指针,现在把i的地址交给了p
int i;
int* p = &i;

// 下面两种形式一样,p是一个指针,而q是一个普通的int变量
int* p , q;
int *p , q;

Pointer variable

  1. The value of the variable is the memory address
  2. The value of common variable is the actual value
  3. The value of the address pointer variable is a variable actual value

As indicator parameter

  1. void f(int *p);
  2. When it is invoked get the address of a variable
  3. int i = 0;f(&i);
  4. This pointer can be accessed through the function inside outside this i
#include <stdio.h>

void f(int *p);

int main(void)
{
    int i = 6;
    printf("&i=%p\n", &i);
    f(&i);

    return 0;
}

void f(int *p)
{
    printf(" p=%p\n", p);
}

// 可以看到这里获取的地址是相同的
// &i=000000000062FE4C
//  p=000000000062FE4C

Access variables on the address that *

  1. * Is a unary operator, the address value used to access variables indicated by the pointer
  2. Value may be a right or left-hand value
  3. int k = *p;
  4. *p = k + 1;

* Left value was called lvalue

  1. Because the number appears in the left side of the assignment is not a variable, but the value is the result of the calculation of expression
  2. a[0] = 2;
  3. *p = 3;
  4. Is a special value, so called lvalue
#include <stdio.h>

// 声明两个函数
void f(int *p);
void g(int k);


int main(void)
{
    int i = 6;
    printf("&i=%p\n", &i);
    f(&i);
    
    // 此时i的值已经发生了变化
    g(i);

    return 0;
}


// 传入的是地址
void f(int *p)
{
    printf(" p=%p\n", p);
    printf("*p=%d\n", *p);
    *p = 66;
}

// 传入的普通int
void g(int k){
    printf("k=%d\n", k);
}


// &i=000000000062FE4C
//  p=000000000062FE4C
// *p=6
// k=66

Third, the use of pointers

A pointer scenarios

Exchange of the value of two variables

#include <stdio.h>

void swap(int *pa , int *pb);

int main()
{
    int a = 5;
    int b = 10;
    swap(&a , &b);
    printf("a=%d , b=%d\n", a , b);
    return 0;
}

void swap(int *pa , int *pb){
    int t = *pa;
    *pa = *pb;
    *pb = t;
}

Scene Two pointer Application

  1. Function returns a plurality of values, some values ​​can only be returned by the pointer
  2. Incoming parameters actually need to save the results back to the variables
#include <stdio.h>

void minmax(int a[] , int len , int *min , int *max);

int main(void)
{
    int a[] = {1,2,3,4,5,6,7,8,9,12,13,14,15,34,35,66,};
    int min , max;
    minmax(a , sizeof(a)/sizeof(a[0]) , &min , &max);
    printf("min = %d , max = %d \n", min , max);

    return 0;
}


void minmax(int a[] , int len , int *min , int *max)
{
    int i;
    *min = *max = a[0];
    for (i = 0; i < len; i++)
    {
        if (a[i] > *max)
        {
            *max = a[i];
        }
        if (a[i] < *min)
        {
            *min = a[i];
        }
    }
}

Two pointer scenarios b

  1. Function returns the status of the operation, the result returned by the pointer
  2. Common routine is to return a special function does not belong to a value in the range indicates an error
    • -1 or 0
  3. But when values ​​are valid any possible results, you have to separate returns
#include <stdio.h>

// 如果成功就返回1,否则就是0
int divide(int a , int b , int *result);

int main(void)
{
    int a = 5;
    int b = 2;
    int c;
    if (divide(a,b,&c))
    {
        printf("%d/%d = %d\n", a, b, c);
    }

    return 0;
}

int divide(int a , int b , int *result)
{
    int ret = 1;
    if ( b== 0)
    {
        ret = 0;
    }else{
        *result = a/b;
    }

    return ret;
}

Pointer common errors

Defines a pointer variable, there is no point to any variable, you begin to use

Fourth, pointers and arrays

What passed into the function of the number of make up?

  1. Array function parameters of the table pointer is actually
  2. sizeof(a) == sizeof(int*)
  3. But [] array for operation with operator

The following four kinds of function prototype is equivalent to

int sum(int *ar , int n);
int sum(int* , int);
int sum(int ar[] , int n);
int sum(int[] , int);

Array variable is a special pointer

Array Variables expressing itself address,

  1. int a [10]; int * p = a; // & fetch address without using
  2. However, the expression of cell array is a variable, & need to take address
  3. a == &a[0]

[] Operator can make an array, a pointer can be done

  1. p[0] <===> a[0]

* Operator can make the pointer, the array may be made

  1. *a = 25

Const array variable is a pointer, it can not be assigned

V. pointer and const

  1. Once represent the address of a variable, you can not point to other variables
    // q内写的地址不能被改变
    int *const q = &i;
    *q = 26; // ok
    q++;   // error
  1. Not represented by this pointer to modify that variable (variable does not become such that const)
    const int *p = &i;
    *p = 26; // error  (*p是不能变的)
    i = 26; // ok
    p = &j; // ok
  1. Understand the meaning of the following?
    const int * p1 = &i;
    int const * p2 = &i;

    int *const  p3 = &i;

That judgment is const const logo is in front of or behind the
front two
p can not be modified
, just like the second case

const array

const int a[] = {1,2,3,4,5,6,};
  1. Const array variable is a pointer to the already, const here means that each element of the array are const int
  2. It must be initialized by assignment

Protection array of values

  1. Because the transfer function of the array is an incoming address, so that the internal function can modify the value of the array
  2. In order to protect the array is not destroyed function, it may be provided as a parameter const
int sum(const int a[] , int length);

Six pointer arithmetic

1 + 1 = 2? So pointer plus 1 equals what it

#include <stdio.h>

int main(void)
{
    char ac[] = {0,1,2,3,4,5,6,7,8,9,};
    char *p = ac;

    printf("p =%p\n", p);
    printf("p + 1 =%p\n\n", p+1);
    // p =000000000062FE30
    // p + 1 =000000000062FE31
    // 相差了1
    
    int ai[] = {0,1,2,3,4,5,6,7,8,9,};
    int *q = ai;

    printf("q =%p\n", q);
    printf("q + 1 =%p\n\n", q+1);
    //q =000000000062FE00
    //q + 1 =000000000062FE04
    // 相差了4

    return 0;
}

We find that, in the char, a difference of 1, a difference of 4 in an int, how is this going?

sizeof(char) = 1 , sizeof(int) = 4

Pointers 1 to a plus operation, meaning that it should move to the next cell to

int a[10];
int *p = a;
*(p+1) --> a[1]

If the pointer is not pointing to a continuous distribution of space, such as an array, then this operation does not make sense

The pointer arithmetic can be

  1. Pointer to add, subtract, an integer (+, + =, -, - =)
  2. Gradual increase and decrease (+ / -)
  3. Subtracting two pointers
#include <stdio.h>

int main(void)
{
    char ac[] = {0,1,2,3,4,20,6,7,8,9,};
    char *p1 = &ac[5];
    char *p2 = &ac[4];

    printf("p1 - p2 = %d\n" , p1 - p2);
    printf("*p1 - *p2 = %d\n" , *p1 - *p2);
    // p1 - p2 = 1   也就是 5 -4
    // *p1 - *p2 = 16  也就是 20 - 4

    
    int ai[] = {0,1,2,3,4,5,6,7,8,18,};
    int *q1 = &ai[9];
    int *q2 = &ai[2];

    printf("q2 - q1 = %d\n", q2 - q1);
    printf("*q2 - *q1 = %d\n", *q2 - *q1);
    // q2 - q1 = -7      也就是 2 - 9
    // *q2 - *q1 = -16   也就是 2 - 18

    return 0;
}

*p++

  1. Data indicated that p be removed, after the bin is moved to the next the way to a position p
  2. * Although high priority, but not high ++
  3. Commonly used in the operation of the array type continuous space
  4. In some cpu, which can be directly translated into an assembly instruction
    char ac[] = {0,1,2,3,4,20,6,7,8,9,-1};
    char *p = &ac[0];

    // 普通方法遍历数组
    // int i ;
    // for (i = 0; i < sizeof(ac)/sizeof(ac[0]); i++)
    // {
    //  printf("%d\n", ac[i]);
    // }
    
    // 普通指针遍历数组
    // for (p = ac; *p != -1; p++)
    // {
    //  printf("%d\n", *p);
    // }

    // printf("---------------\n");
    
    // 使用*p++,快
    while(*p != -1){
        printf("%d\n", *p++);
    }

0 Address

  1. Of course, you have the memory address 0, address 0 but usually not just a touch of address
  2. So you should not have a pointer to a value of 0
  3. So you can use 0 address to represent special things
    • The returned pointer is invalid
    • Pointer not really initialized (initialized to 0)
  4. NULL is a predetermined symbol definition, represents the address 0

Pointer types

  1. No matter what type of points, the size of all the pointers are the same, because all address
  2. However pointers pointing to a different type of assignment is not directly to each other
  3. Just to avoid using the wrong hands

What to do with a pointer

  1. When used as a parameter data need to pass a large
  2. After passing an array of arrays to do the operation
  3. Function returns more than one result
  4. We need to change more than one variable with function
  5. Dynamic Application Memory

Seven, dynamic memory allocation

Input data

  1. If the input data, to tell you the number, then enter, each data to be recorded
  2. C99 can do with the size of the array defined variables, C99 before it?
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int number;
    int *a;
    int i;
    printf("请输入数量:\n");
    scanf("%d" , &number);

    // 动态内存分配用malloc,此时a就相当于数组
    a = (int*)malloc(number*sizeof(int));

    for (i = 0; i < number; i++)
    {
        scanf("%d" , &a[i]);
    }

    for (i = number - 1; i >=0; i--)
    {
        printf("%d\n", a[i]);
    }

    // 最后释放空间
    free(a);

    return 0;
}

malloc function

#include <stdlib.h>
void* malloc(size_t size);
  1. Size in bytes of space to malloc apply as a unit, what type need to write what type inside sizeof
  2. Returned result is void *, you need to type to type their needs
  3. Finally free up space
(int*)malloc(n*sizeof(int));

No room for?

  1. If the application fails to return 0, otherwise known as NULL
  2. Your system can give you much space?
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    void *p;
    int cnt = 0;

    while((p = malloc(100 * 1024 *1024))){
        cnt ++ ;
    }

    printf("分配了%d00MB的空间\n", cnt);

    return 0;
}
// 分配了27200MB的空间

free()

  1. The application was returned to space systems
  2. You can also apply for the first address space
  3. A corresponds to a free malloc

Guess you like

Origin www.cnblogs.com/mengd/p/11617982.html