Dark Horse programmer-using the Swap function to study C's pointers

Source: Internet
Author: User

3 functions are designed to implement the following functions, respectively:

    • Swap two integers
    • Swap two shaping pointers
    • Swap any two variables of the same type
1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4 voidSwap_int (int* PA,int*QA)//exchange of two integers5 {6     inttemp = *PA;7*PA = *QA;8*qa =temp;9 }Ten  One  A voidSwap_intpur (int* * PPA,int**Qqa)//Exchange two shaping pointers - { -     int* Temp = *ppa; the*ppa = *Qqa; -*qqa =temp; - } -  + voidSwap_any (voidAvoid*B, size_t len)//swap any two variables of the same type - { +    void* Temp =malloc(len); A memcpy (temp, A, Len);//Memory copy function at memcpy (A, b, Len); - memcpy (b, temp, len); - } -  - intMain () - { in     intA =4, B =3; -     int* p = &a, * q = &b; toprintf"Origin a=%d, b=%d\n", A, b); +Swap_int (&a,&b); -printf"After Swap_int, a=%d, b=%d\n", A, b); theSwap_intpur (&p,&q); *printf"After Swap_intpur, a=%d, b=%d\n", *p,*q); $Swap_any ((void*) &a, (void*) &b,sizeof(a)); Forcibly converts A and B memory address pointers to an indeterminate type of pointer passed into the functionPanax Notoginsengprintf"After Swap_any, a=%d, b=%d\n", A, b);  -     return 0; the  +}
    • Implementing an exchange of two integers

In the C language, all function arguments are passed by value, that is, the parameter values passed to the called function are stored in the temporary variable, not in the original variable, so the called function cannot directly modify the value of the variable in the key function. At this point we pass the memory address of the two variables in the main function to the called function, although the address is stored in a temporary variable, but we exchange the contents of the address in the function called, thus achieving the purpose of exchanging the values of the two integer variables in the main function.

    • Implements swapping two shaping pointers

Similarly, we can not directly exchange a plastic pointer, but also the address of the two shaping pointers into the called function, the interchange is similar to the first function. However, it is important to note that we are passing the P pointer and the address of the Q pointer to the called function, so we need to put the pointer p in the *ppa of the function Swap_intpur, so we have to use a pointer variable as the buffer variable, that is, at this point we use * temp instead of temp.

    • Implementing a variable that swaps any two similar terms

Different types of variables, in memory the length of storage is different, so we should deal with any variable, in passing the variable is also passed the variable storage length. Because the length of the pass is not deterministic, we declare the type of Len in Swap_any to be size_t to ensure that it is large enough to guarantee the size of the object in memory. When this function is called, sizeof is also used to get the length of the passed-in variable.

The malloc function is used to allocate memory space for a specified size byte to the system, and this function allows us to determine the length of the buffer variable. Finally, using the MEMCP function, the function is a memory copy, that is, starting from the origin of the memory address referred to by the source to copy n bytes to the destination refers to the memory address in the starting position.

Summarize

Pointer: points to the starting position of a piece of memory, and the type of the pointer determines the size of the memory.

Dark Horse programmer-using the Swap function to study C's pointers

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.