#include<stdio.h>
int main(void)
{
int i = 5;
int *p,*q;
p = &i;
//*q = *p;//q未初始化
q = p;
printf("%d\n", *q);
return 0;
}
#include<stdio.h>
void f(int *pArr, int len);
int main(void)
{
int a[5] = { 1,2,3,4,5 };
f(a, 5);
return 0;
}
void f(int *pArr, int len)
{
int i;
for (i = 0; i < len; ++i)
{
printf("%d ", *(pArr + i));
}
printf("\n");//输出结果1 2 3 4 5
}
#include<stdio.h>
void f(int * p)
{
*p = 10;
}
int main(void)
{
int i = 5;
f(&i);
printf("i=%d\n", i);
return 0;
}
#include <stdio.h>
int main(void)
{
int i = 5, j = 10, *p = &i, *q = &j;
int a[5];
p = &a[1];
q = &a[4];
printf("p和q所指向的单元相隔%d个单元\n", q - p);
return 0;
}
#include <stdio.h>
int main(void)
{
int i = 10,*p=&i,**q=&p,***r=&q;//*p=**q=***r=i *q=&p *r=&q
printf("i=%d\ni=%d\ni=%d\n", ***r, **q, *p);
return 0;
}
#include<stdio.h>
void f(int **q)
{
//*q就是p
}
void g()
{
int i = 10, *p = &i;
f(&p); //p是int *类型,&p是int**类型
}
int main(void)
{
g();
}
#include <stdio.h>
int main(void)
{
int a[3][4] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
int(*p)[4];
int i, j;
p = a;
printf("%d", (*p)[3]);
}
#include<stdio.h>
void main()
{
int a = 4, *pa, k, j, m, n;
pa = &a;
k = (*pa)++;//先把*p的值赋给变量后,*p+1
printf("a=%d,k=%d,*pa=%d\n", a, k, *pa);//5 4 5
j = *pa++;//先把*p的值赋给变量后,p+1
printf("a=%d,k=%d,*pa=%d,j=%d\n", a, k, *pa, j);//5 4 -85899340 5
}
#include<stdio.h>
void main()
{
int a = 3, b = 4, *pa, *pb;
pb = &a;
pa = &b;
*pa = *pb + b;
*pb = *pa + a;
printf("a=%d,b=%d,*pa=%d,*pb=%d",a,b,*pa,*pb);//10 7 7 10
}
#include<stdio.h>
void main()
{
int a = 3, b = 4, *pa = &a, *pb = &b, *t;
t = pa;
pa = pb;
pb = t;//pa指向了b,pb指向了a
*pa = *pb + b;
*pb = *pa + a;
printf("a=%d,b=%d,*pa=%d,*pb=%d",a,b,*pa,*pb);//10 7 7 10
}
#include<stdio.h>
void main()
{
int a = 3, b = 4, *pa = &a, *pb = &b, t;
t = *pa;
*pa = *pb;
*pb = t;
*pa = *pb + b;
*pb = *pa + a;
printf("a=%d,b=%d,*pa=%d,*pb=%d",a,b,*pa,*pb);//6 12 6 12
}
#include<stdio.h>
void main()
{
int a = 3, b = 4, c = 5, *pa = &a, *pb = &b, *pc = &c;
pb = &c;
pa = &b;
pc = &a;
if (*pa > *pb)
{
pb = pc;
}
else
{
pa = pb;
}
*pa = *pb + *pc;
*pb = *pa + *pc;
*pc = *pa + *pb;
printf("a=%d,b=%d,c=%d\n*pa=%d,*pb=%d,*pc=%d", a, b, c, *pa, *pb, *pc);
//画图理解
//22 4 11
//11 11 22
}
#include<stdio.h>
void fun(int *p)
{
if (*p > 5)
{
*p = *p + 1;
}
else
{
*p = *p - 1;
}
}
void fun1(int a)
{
if (a > 5)
{
a = a + 1;
}
else
{
a = a - 1;
}
}
void main()
{
int a = 2;
//fun(&a);//1
fun1(a);//2
printf("a=%d", a);
}
#include<stdio.h>
void sum1(int *a, int *b, int *s)
{
*s = *a + *b;
}
void sum2(int a, int b, int s)
{
s = a + b;
}
void main()
{
int a = 2, b = 4, s;
sum1(&a, &b, &s);//6
sum2(a, b, s);//6
printf("s=%d", s);
}
//判断一个数是否为质数
#include<stdio.h>
int IsPrimer(int *n)
{
int i;
if (*n == 2)
return 1;
for (i = 2; i <= *n; i++)
{
if (*n%i == 0)
{
return 0;
}
return 1;
}
}
void main()
{
int a;
printf("请输入一个整数\n");
scanf("%d", &a);
if (IsPrimer(&a))
{
printf("%d是质数", a);
}
else
{
printf("%d不是质数", a);
}
}
//求两个数的绝对值的和
#include<stdio.h>
int abs(int *n)//求一个数的绝对值
{
if (*n > 0)
return *n;
return -(*n);
}
int sum(int *n1 ,int *n2)//求两数和
{
return abs(n1) + abs(n2);
}
void main()
{
int n1, n2;
printf("请输入两个整数:\n");
scanf("%d %d", &n1, &n2);
printf("sum=%d", sum(&n1, &n2));
}
#include<stdio.h>
void main()
{
int a[5] = { 1,2,3,4,5 },k1,k2,k3;
k1 = *a * *a + 1;
k2 = *a * *(a + 1);
k3 = *a * (*a + 1);
printf("k1=%d,k2=%d,k3=%d", k1, k2, k3);//2 2 2
}
#include<stdio.h>
void fun(float *a, float *b)
{
float w;
*a = *a + *a;
w = *a;
*a = *b;
*b = w;
}
void main()
{
float x = 2.0, y = 3.0;
float *px = &x, *py = &y;
fun(px, py);
printf("x=%.1f y=%.1f", x, y); //3.0 4.0
}
#include<stdio.h>
void sub(int x, int y, int *z)
{
*z = y - x;
}
void main()
{
int a, b, c;
sub(10, 5, &a);
sub(7, a, &b);
sub(a, b, &c);
printf("a=%d,b=%d,c=%d", a, b, c);//-5 -12 -7
}
#include<stdio.h>
void main()
{
int a[] = { 1,2,3,4,5,6 }, *p;
p = a;
printf("%d\n", *p++);
printf("%d", *(p + 3)); //1 5
}
#include<stdio.h>
void main()
{
int a[6] = { 1,2,3,4,5,6 }, *p;
for (p = a; p < a + 6; p++)
{
printf("%d\n", *p);//1 2 3 4 5 6
}
}/*
尽管数组名a是指针,但这个指针常量并不能移动,
为了能产生个可以移动的指针,
我们定义一个指向一维数组元素的指针*/
#include<stdio.h>
void sum(int *a)
{
a[0] = a[1];
//*(a + 0) = *(a + 1);
}
void main()
{
int aa[4] = { 1,2,3,4 },i;
for (i = 2; i >= 0; i--)
{
sum(&aa[i]);
}
printf("aa[0]=%d", aa[0]);//4
}
#include<stdio.h>
void func(int *a, int b[])
{
b[0] = *a + 6;
}
void main()
{
int a, b[5] = { 0 };
a = 0;
b[0] = 3;
func(&a, b);
printf("b[0]=%d", b[0]);//6
}
#include<stdio.h>
void f(int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
void main()
{
int a[8] = { 1,2,3,4,5,6,7,8 },*p,*q,i;
p = a;
q = &a[7];
while (p < q)
{
f(p, q);
p++;
q--;
}
for (i = 0; i < 8; i++)
{
printf("%5d", a[i]);//87654321
}
}
#include<stdio.h>//分别求一个数组中的奇数和、偶数和
void fun(int *a, int n, int *even, int *odd)
{
int i;
*odd = *even=0;
for (i = 0; i < n; i++)
{
if (*(a + i) % 2 == 0)
{
*even += *(a + i);
}
else
{
*odd += *(a + i);
}
}
}
void main()
{
int a[5] = { 1,2,3,4,5 },odd,even;
fun(a, 5, &even, &odd);
printf("even=%d,odd=%d", even, odd);
}
多级指针与指针概念理解(新手必看加敲)
最新推荐文章于 2024-07-26 23:51:02 发布