c语言指针赋值-字符串/整型

c语言指针记录,避免忘记

字符串指针赋值

#include <stdio.h>
#include <string.h>
//*在定义的时候才为指针
void myswitch(char** str1,char** str2){
    char *tmp = NULL;
    //*在这里解引用
    tmp = *str1;
    *str1 = *str2;
    *str2 = tmp;
}

int main(){
    char ch1[50] = "字符串1";
    char ch2[50] = "字符串2";
    printf("1:%s,2:%s\n",ch1,ch2);
    //&也是解引用,取指针指向的地址
    myswitch(&ch1,&ch2);
    printf("1:%s,2:%s\n",ch1,ch2);
}

整型指针赋值

#include <stdio.h>
#include <string.h>

void swap(int *a,int *b){
    int t;
    //*a解引用
    t = *a;
    *a = *b;
    *b = t;
}
int main(){
    int a = 2;
    int b = 5;
    printf("1:%d,2:%d\n",a,b);
    swap(&a, &b);
    printf("1:%d,2:%d\n",a,b);
}

 

posted @ 2022-03-17 14:58  程序员Hiram  阅读(677)  评论(0)    收藏  举报