#include
int a = 1, b = 2;
int *pointer1=&a, *pointer2=&b;
int main(void)
{
printf("a=%d,b=%d\n", a, b);
printf("*pointer1=%d,*pointer2=%d", *pointer1, *pointer2);
}
#include
int a = 1, b = 2;
int *pointer1=&a, *pointer2=&b;
int main(void)
{
printf("a=%d,b=%d\n", a, b);
printf("*pointer1=%d,*pointer2=%d\n", *pointer1, *pointer2);
printf("pointer1=%p,pointer2=%p", pointer1, pointer2);
}
#include
int a = 0, b = 0;
int *pointer1= &a, *pointer2= &b;
int bottle = 0;
int main(void)
{
printf("please type a value and b value:");
scanf("%d%d", &a, &b);
if (*pointer1 < *pointer2)
{
bottle = *pointer1;
*pointer1 = *pointer2;
*pointer2 = bottle;
}
printf("%d,%d", *pointer1, *pointer2);
return 0;
}
#include
int a = 0, b = 0;
int* pointer1 = &a, * pointer2 = &b, * pointer;
int main(void)
{
printf("please type a value and b value:");
scanf("%d%d", &a, &b);
if (a< b)
{
pointer = pointer1;
pointer1 = pointer2;
pointer2 = pointer;
}
printf("max= %d,min = %d", *pointer1, *pointer2);
return 0;
}
#include
int a = 0, b = 0;
int* pointer1 = &a, * pointer2 = &b;
void swap(int* pointer1, int* pointer2);
int main(void)
{
printf("please type a value and b value:");
scanf("%d%d", &a, &b);
swap(pointer1, pointer2);
printf("max= %d,min = %d", *pointer1, *pointer2);
return 0;
}
void swap(int* pointer1, int* pointer2)
{
int bottle;
if (a < b)
{
bottle = *pointer1;
*pointer1 = *pointer2;
*pointer2 = bottle;
}
}
#include
int a = 0, b = 0;
int* pointer1 = &a, * pointer2 = &b;
void swap(int* pointer1, int* pointer2);
int main(void)
{
printf("please type a value and b value:");
scanf("%d%d", &a, &b);
swap(pointer1, pointer2);
printf("max= %d,min = %d", *pointer1, *pointer2);
return 0;
}
void swap(int* pointer1, int* pointer2)
{
int* pointer;
if (a < b)
{
pointer = pointer1;
pointer1 = pointer2;
pointer2 = pointer;
}
}
#include
int a = 0, b = 0, c = 0;
int *poninter1 = &a, *poninter2 = &b, *poninter3 = &c;
void exchange(int* a, int* b, int* c);
void swap(int* pointer1, int* pointer2);
int main(void)
{
printf("please type a value、b value、c value:");
scanf("%d%d%d", &a, &b, &c);
exchange(poninter1, poninter2, poninter3);
printf("max1=%d,max2=%d,max3=%d", *poninter1, *poninter2, *poninter3);
return 0;
}
void exchange(int* a, int* b, int* c)
{
if (*a < *b)
swap(a, b);
if (*b < *c)
swap(b, c);
if (*a < *b)
swap(a, b);
}
void swap(int* pointer1, int* pointer2)
{
int bottle;
bottle = *pointer1;
*pointer1 = *pointer2;
*pointer2 = bottle;
}
//下标法
#include
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
int main(void)
{
for (int i = 0; i < 10; i++)
{
printf("%d ",a[i]);
}
return 0;
}
//数组名计算数组元素地址
#include
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
int *p = a;//或者int *p = &a[0];
int main(void)
{
for (int i = 0; i < 10; i++)
{
printf("%d ", *(p + i));//或者printf("%d ", *(a + i));
}
return 0;
}
//用指针变量指向数组元素
#include
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
int *p = a;//或者int *p = &a[0];
int main(void)
{
for (int i = 0; i < 10; i++)
{
printf("%d ", *(p++));
}
return 0;
}
#include
int a[10] = { 0 };
int *p = a;
int main(void)
{
printf("please type ten integer numbers:");
for(int i = 0;i<10;i++)
scanf("%d", p++);
p = p - 10;
for (int i = 0; i < 10; i++)
printf("%d ", *(p++));
return 0;
}
方法1
#include
int a[10] = { 0 };
int n = 10;
int *p = a;
void swap(int *a, int *b);
int main(void)
{
printf("please type ten integer numbers:");
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
for (int i = 0; i < n/2; i++)
{
swap(p + i, p + (n-1) - i);
}
for (int i = 0; i < n; i++)
{
printf("%d ", *(p++));
}
return 0;
}
void swap(int *pointer1, int * pointer2)
{
int bottle = 0;
bottle = *pointer1;
*pointer1 = *pointer2;
*pointer2 = bottle;
}
#include
int a[10] = { 0 };
int n = 10; //we can change it
int* p = a;
void swap(int x[], int n);
int main(void)
{
printf("please type ten integer numbers:");
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
swap(a, 10);
for (int i = 0; i < n; i++)
{
printf("%d ", *(p++));
}
return 0;
}
void swap(int x[ ],int n)
{
int bottle = 0;
for (int i = 0; i < n / 2; i++)
{
bottle = x[i];
x[i] = x[(n - 1) - i];
x[(n - 1) - i] = bottle;
}
}
#include
int a[10] = {12,34,5,689,-43,56,-21,0,24,65};
void arrange(int* x, int number);
int main(void)
{
arrange(a, 10);
for (int i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}
return 0;
}
void arrange(int* x, int number)
{
for (int i = 0; i < number-1; i++)
for (int j = 0; j < number - i - 1; j++)
{
int bottle = 0;
if (*(x+j) < *(x + j+1))
{
bottle = *(x + j);
*(x + j) = *(x + j + 1);
*(x + j + 1) = bottle;
}
}
}
#include
int a[3][4] = { 1,3,5,7,9,11,13,15,17,19,21,23 };
int main(void)
{
printf("%p,%p\n", a,*a);
printf("%p,%p\n", a[0], *(a+0));
printf("%p,%p\n", &a[0], &a[0][0]);
printf("%p,%p\n", a[1], a+1);
printf("%p,%p\n", &a[1][0], *(a+1)+0);
printf("%p,%p\n", a[2], *(a + 2));
printf("%p,%p\n", &a[2], a+2);
printf("%d,%d\n", a[1][0], *(*(a + 1) + 0));
printf("%d,%d\n", *a[2], *(*(a + 2) + 0));
return 0;
}
#include
int a[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
int main(void)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
printf("%d\t", *(*(a + i) + j));
printf("\n");
}
return 0;
}
方法2
#include
int a[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
int* p;
int main(void)
{
for (p = a[0]; p<a[0]+12; p++)
{
if ((p - a[0]) % 4 == 0) printf("\n");
printf("%4d", *p);
}
return 0;
}
#include
int a[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
int (*p)[4], x=0, y=0;
int main(void)
{
p = a;
printf("please type row and colum:");
scanf("%d%d", &x, &y);
printf("a[%d][%d]=%d", x, y, *(*(p + x) + y));
return 0;
}
#include
char string[] = "I love China";
int main(void)
{
printf("%s\n", string);
printf("%c", string[7]);
return 0;
}
#include
char a[] = "I love China";
char *p = &a[0];//char *p = a;
int main(void)
{
printf("%s\n", a);
printf("%s\n", &a[0]);
printf("%s\n", p);
printf("%c\n", *(p));
printf("%c\n", *(p+2));
return 0;
}
#include
char *string = "I love China";
int main(void)
{
printf("%s\n", string);
return 0;
}
#include
char *string;
int main(void)
{
string = "I love China";
printf("%s\n", string);
return 0;
}
#include
char string1[] = "I am student";
char string2[20];
int main(void)
{
for (int i = 0; *(string1 + i)!='\0'; i++)
*(string2 + i) = *(string1 + i);
printf("string1 = %s,string2 = %s", string1, string2);
return 0;
}
#include
char string1[] = "I am student";
char string2[20];
char* pointer1 = string1;
char* pointer2 = string2;
int main(void)
{
for (int i = 0; *(pointer1 + i)!='\0'; i++)
*(pointer2 + i) = *(pointer1 + i);
printf("string1 = %s,string2 = %s", pointer1, pointer2);
return 0;
}
#include
char string1[] = "I am student";
char string2[20];
void copy_string(char a[], char b[]);
int main(void)
{
copy_string(string1, string2);
printf("string1 = %s,string2 = %s", string1, string2);
return 0;
}
void copy_string(char a[], char b[])
{
//for (int i = 0; *(a + i) != '\0'; i++)
// *(b + i) = *(a + i);
for (int i = 0; a[i] != '\0'; i++)
b[i] = a[i];
}
#include
char string1[] = "I am student";
char string2[20];
char* pointer1 = string1;
char* pointer2 = string2;
void copy_string(char* a, char* b);
int main(void)
{
copy_string(pointer1, pointer2);
printf("string1 = %s,string2 = %s", pointer1, pointer2);
return 0;
}
void copy_string(char* a, char* b)
{
for (int i = 0; *(a + i) != '\0'; i++)
*(b + i) = *(a + i);
}
#include
char* a = "I love China!";
int main(void)
{
a = a + 7;
printf("%s\n", a);
return 0;
}