OBJECTIVE-C Study Preparation __c language 5

Source: Internet
Author: User



Continue learning Pointers:






Here's a pointer to the pointer



As the name implies pointers to the pointer is a pointer to the address of a needle is also called a two-level pointer, nonsense say we see the code definition and effect


#include <stdio.h> int main(){ int a=10; int* p=&a; int** s=&p;
    printf("%d\n",*p);
    printf("%d\n",**s);
    printf("------------\n");
    printf("%p\n",p);
    printf("%p\n",*s);
    printf("------------\n");
    printf("%p\n",s);
}


I believe my friends already know that the level two pointer is defined, that is, the type * * variable name is more than a primary pointer, and the pointer as a pointer does point to the first-level pointer address &p,



So have friends carefully observe the output of the five values? The first two values are two pointers to the data in the &a address, and the third fourth value is the address of variable A, and the value of variable A is stored in the address, which is 10.



You might as well guess what the last address is? What is stored?



The last address is the address of level two pointer s, and the stored value is the address of the primary pointer p



So *s can access the address of P and **s can access the value of the address that P points to, which is 10;



In fact if there is a need to have pointers to pointers of pointers: That is, three-level pointers and even four-level pointers, of course, this thing more and more prone to chaos, generally only used to two-level pointers when to use the two-level pointer? When you call a function in the main function if a variable is passed in, whatever changes you make to the variable within the function will not change the main function.



The value of the variable in the number: A first-level pointer can change the value of a variable


#include <stdio.h> void change(int* a){ *a=30;
} int main(){ int a=10;
    change(&a);
    printf("%d\n",a);
}


What about the second-level pointers? It can not only change the value of a primary pointer or variable, but also change the address pointed to by a first-level pointer


#include <stdio.h> void change(int* a,int* b){
    printf("%p\n",a);
    printf("%p\n",b);
    a=b;
} int main(){ int a=10; int b=20; int *p=&a;
    change(p,&b);
    printf("%d\n",*p);
}


Run the result output 10, you can see the function clearly changed a address to the address of B, but the main function of P's address has not changed;



We're trying with a level two pointer.


#include <stdio.h> void change(int** a,int* b){
    printf("%p\n",*a);
    printf("%p\n",b); *a=b;
} int main(){ int a=10; int b=20; int *p=&a;
    change(&p,&b);
    printf("%d\n",*p);
}


The run output *p value is 20, which means that the address the P points to has changed



To make it more obvious that address changes can be output before and after the change () method call



printf ("%p\n", p);



The second level hands are here ... Next, we introduce a constant pointer, a pointer to a constant, a constant pointer to a constant


#include <stdio.h>
int main () {
     int a = 10;
     int b = 20;
     // int * const p = & a; // const pointer
     // a = 20;
     // p = & b; // X is not allowed to change the address pointed to
     // * p = b; // √You can change the value of the address

     // const int * p = & a; // a pointer to a constant
     // a = 20;
     // p = & b; // √Can change the value of the address
     // * p = b; // X is not allowed to change the address pointed to

     // const int const * p = & a;
     // Neither the pointer address nor the value in the address can be modified by the pointer

     // The above three methods can be modified by the variable a
     printf ("% d \ n", a);
     printf ("% d \ n", * p);
}


If you still do not know a friend can comment message ask me



Finally, we introduce the function pointers


#include <stdio.h>
int sum(int a,int b){
    return a+b;
}
int main(){
    int a=10;
    int b=20;
    int (*p)(int,int);    //Define a function pointer
    p=&sum;               //Assign function address to function pointer
    int result=(*p)(a,b); //Calling a function pointer is equivalent to calling the sum function through a function pointer
    printf("%d\n",result);
}


Function pointers are typically used for abstraction: For example, I want to filter an array, but the filtering method doesn't decide that you can set a function pointer as an abstract method.


#include <stdio.h>
#include <stdbool.h>
int*filter(int* start,int* end, bool (*test)(int a)){
    int *b;
    while(start<end){
        if(!(*test)(*start)){
            b=start;
            while(b<end){
                *b=*(b+1);
                b++;
            }
            end--;
            continue ;
        }
        start++;
    }
    return end;
}


The method call requires the first and the end address of the array, and the Filter method function return address is the end address of the filtered array






OBJECTIVE-C Study Preparation __c language 5


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.