Level 2 pointer (double pointer) in C Language

Source: Internet
Author: User

Level 2 pointer (double pointer) in C Language
For original works, refer to the source for reprinting (1) Notes for using C language (2) Notes for using C language (3)

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

The following describes how to use the second-level pointer in C.

For example, we use pointers to exchange the values of Two integer variables.

The error code is as follows:

Level 1 pointer

 

[Cpp]View plaincopyprint?
  1. # Include <stdio. h>
  2. Void swap (int * a, int * B)
  3. {
  4. Int * tmp = NULL;
  5. Tmp =;
  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 structure is as follows:

 

Result Analysis: no matter the value or pointer, the parameters in the swap function always pass the value. Therefore, even if the addresses a and B have already passed the parameters to the swap function, in the function, the values of a and B are exchanged (the address of a in the main function and the address of B). After the switching, the corresponding parameters of the function pop up from the stack, it cannot be returned to the called function, so the operations in the swap function are futile.

The values of a and B can be directly exchanged here. However, if a and B are not a 32-bit integer variable but a complex data structure, this will reduce the efficiency. As follows;

 

[Cpp]View plaincopyprint?
  1. Void swap (TYPE * a, TYPE * B)
  2. {
  3. TYPE tmp;
  4. Tmp = *;
  5. * A = * B;
  6. * B = tmp;
  7. }
void swap(TYPE *a,TYPE *b){        TYPE tmp;        tmp=*a;        *a=*b;        *b=tmp;}

 

 

Level 2 pointer

The following is an example of using a second-level pointer to allocate memory.

 

[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 pointer value, consider using a two-dimensional pointer.

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.