Level two pointer (double pointer) in C language

Source: Internet
Author: User

Original works, reproduced please indicate the source Http://blog.csdn.net/yming0221/article/details/7220688C language more viewC language Use Precautions (i)C language Use Precautions (ii)C language Use Precautions (iii)

The second-level pointer is also called a double pointer. There is no reference in the C language, so you must use a level two pointer when you try to change the value of a pointer. Reference types can be used in C + + to implement.

Here's how to use level two pointers in C.

For example, we use pointers to Exchange values for two integer variables.

The error code is as follows:

First-level pointers

[CPP]View Plaincopyprint?
  1. #include <stdio.h>  
  2. void swap (int *a,int *b)
  3. {
  4. int  *tmp=null;
  5. Tmp=a;
  6. A=b;
  7. b=tmp;
  8. }
  9. int Main (int argc,char **argv)
  10. {
  11. int  a=2;
  12. int  b=3;
  13. printf ("before swap a=%d b=%d\n", A, b);
  14. Swap (&A,&B);
  15. printf ("after swap a=%d b=%d\n", A, b);
  16. return  0;
  17. }
#include <stdio.h>void swap (int *a,int *b) {        int *tmp=null;        Tmp=a;        a=b;        B=tmp;} int main (int argc,char **argv) {        int a=2;        int b=3;        printf ("Before swap a=%d  b=%d\n", A, b);        Swap (&a,&b);        printf ("After swap a=%d  b=%d\n", A, b);        return 0;}
The output is structured as follows:

Results analysis: The parameters in the Swap function pass the constant value, regardless of the value or the pointer, so in the above function even if the address of A and B is passed to the swap function, the value of a and B is exchanged within the function (the address of A and B in the main function), and the exchange is complete. The corresponding arguments of the function are popped out of the stack and cannot be returned to the calling function, so the operation in the swap function is futile.

The values of A and B can be exchanged directly here, but if A and B are not a 32-bit integer variable, it is a complex data structure, which reduces efficiency. Following

[CPP]View Plaincopyprint?
    1. void Swap (TYPE *a,type *b)
    2. {
    3. TYPE tmp;
    4. Tmp=*a;
    5. *a=*b;
    6. *b=tmp;
    7. }
void swap (Type *a,type *b) {        type tmp;        Tmp=*a;        *a=*b;        *b=tmp;}

Second-level pointers

Here is an example of allocating memory using level two pointers

[CPP]View Plaincopyprint?
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. void memorylocate (char **ptr)
  5. {
  6. *ptr= (char *) malloc (10*sizeof(char));
  7. }
  8. int Main (int argc,char **argv)
  9. {
  10. Char  *buffer;
  11. Memorylocate (&buffer);
  12. strcpy (buffer,"12345");
  13. printf ("Buffer%s\n", buffer);
  14. return  0;
  15. }
#include <stdio.h> #include <stdlib.h> #include <string.h>void memorylocate (char **ptr) {        *ptr= ( char *) malloc (10*sizeof (char));} int main (int argc,char **argv) {        char *buffer;        Memorylocate (&buffer);        strcpy (buffer, "12345");        printf ("Buffer%s\n", buffer);        return 0;}

when you want to change the value of a pointer consider using a two-dimensional pointer.

Level two pointer (double pointer) in C language

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.