c語言使用指針交換數值


練習題:將兩個int類型數值交換

#include <stdio.h>

void swap(int*,int*);

int main(void){

    int bin = 107, hex = 0x5f1043;
    
    swap(&bin,&hex);
    
    printf("%d %d\n",bin,hex);    

    return 0;
}

void swap(int *p1, int *p2)
{
    int tmp = *p1;
    *p1 = *p2;
    *p2 =tmp;
}

練習題:將兩個字符串交換

#include <stdio.h>

void swap(char**,char**);

int main(void){

    char *sz1 ="World\0";
    char *sz2 = "Hello\0";

    swap(&sz1,&sz2);

    printf("%s %s\n",sz1,sz2);

    return 0;
}

void swap(char **sz1, char **sz2)
{
    char *tmp = *sz1;
    *sz1 = *sz2;
    *sz2 = tmp;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM