c语言知识

指针、指针与数组、指针与字符数组及指针与函数


声明:int *p = NULL;p为地址*类似钥匙*p就是该地址所指向的值。


#include <stdio.h>

#include <stdlib.h>

int main(){

int a=10;

int *p = NULL;

p=&a;

printf("%d",*p)//*p的值就为a的值,即10

system("pause");

retrun 0;

}

 

#include <stdio.h>

#include <stdlib.h>

int main(){

int b[] = {1,2,3,4,5,6};

int *p = NULL;

p=&a;//此时p的地址为&a[0]

printf("%x",*p)//*pa[0]的值

system("pause");

retrun 0;

}

 

#include <stdio.h>

#include <stdlib.h>

int main(){

int b[] = {1,2,3,4,5,6};

int *p = NULL;

p=&a;

printf("%x",*(p+1))//*(p+1)a[1]的值

system("pause");

retrun 0;

}

 

#include <stdio.h>

#include <stdlib.h>

int main(){

int a[3][4] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};

int *p = NULL;

p = a[0][0];

for (int i = 0; i<3; i++) {

for (int j = 0; j<4; j++) {

printf("&a[%d][%d] = %x\n",i,j,&a[i][j]);//输出a[i][j]的地址

printf(“a[%d][%d]= %d\n”,i,j,a[i][j]);//输出a[i][j]的值

printf("%d\n",*(p+i*4+j));//p输出a[i][j]的地址

}

}

system("pause");

retrun 0;

}

 

#include<stdio.h>

#include<stdlib.h>

int main(){

int i,j;

int a[2][3]={{1,2,3},{4,5,6}};

int (*p)[3] = NULL;

p=a;

for(i = 0;i<2;i++){

for(j = 0;j<3;j++){

printf("%d\n",*(*(p+i)+j));

}

}

system("pause");//通过指针来确定数组元素的位置

return 0;

}


#include <stdio.h>

#include <stdlib.h>

void swap(int *p1, int *p2) {

int p;

p=*p1;

*p1=*p2;

*p2=p;

}

 

int main(){

int a=10;

int b=20;

swap(&a,&b);

printf("a=%d,b=%d",a,b);//a,b交换位置

system("pause");

retrun 0;

}

 

 

#include <stdio.h>

#include <stdlib.h>

 

int main(){

char *ch ="1234";

printf("%s\n",ch);//ch可以直接输出字符串”1234”,与其他数组不同的地方

return 0;

system("pause");

}

 

 

#include <stdio.h>

#include <stdlib.h>

 

int max(int a,int b){

    if(a>b) {

        return a;

    }

    else{

        return b;

    }

}

 

int main(){

int (*p)(int,int);

     p = max;

   int c = p(10,20);

printf("c = %d\n",c);//输出较小的数

     return 0;

}

 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值