函数的参数是指针
用指针输出字符串
int main()
{
char* p = "graps now,don't... future.";
printf("%s", p);
printf("\n");
return 0;
}
不用strcpy复制字符串
int main()
{
char str[] = { "abcdefj" };
char str2[] = { "hijklmn" };
char* p1, * p2;
p1 = str;
p2 = str2;
while (*p1 != '\0')
{
*p2 = *p1;
p1++;
p2++;
}
*p2 = '\0';
printf("%s",str2);
//puts(str2);
printf("\n");
return 0;
}
用字符串数组输出
int main()
{
char *arr[] =
{
"chinese",
"math",
"english"
};
//puts(arr);
for (int i = 0; i < 3; i++)
{
printf("%s", arr[i]);
printf("\n");
}
return 0;
使用int**p
int main()
{
int** p;
int price = 559;
int* p1;
p1 = price;
p = p1;
printf("%d\n", p);
int i = 0;
char* price[] = { "559" };
char** p;
for (i = 0; i < 1; i++)
{
p = price + i;
printf("%s", *p);
}
return 0;
}
函数参数为指针
int area(int* l, int* w, int* h)
{
//int s = 0;
//s = (*l) * (*w) * (*h);
return (*l) * (*w) * (*h);
}
void main()
{
int lenth = 10;
int wide = 5;
int hight = 15;
int* l = &lenth;
int* w = &wide;
int* h = &hight;
printf("%d\n", area(l, w, h));
}
从小到大输出
void swap(int* q1, int* q2)
{
int temp;
temp = *q1;
*q1 = *q2;
*q2 = temp;
}
void exchange(int* x, int* y, int* z)
{
if (*x < *y)
swap(x, y);
if (*x < *z)
swap(x, z);
if (*y < *z)
swap(y, z);
}
int main()
{
int a, b, c;
int* p1, * p2, * p3;
scanf("%d, %d, %d", &a, &b, &c);
p1 = &a;
p2 = &b;
p3 = &c;
exchange(p1, p2, p3);
printf("%d, %d, %d\n", a, b, c);
return 0;
}
练习
void order(int* a, int* b)
{
if (*a > *b)
{
int imup = 0;
imup = *a;
*a = *b;
*b = imup;
}
}
int main()
{
int a, b, * p1, * p2;
scanf("%d %d", &a, &b);
p1 = &a;
p2 = &b;
order(p1, p2);
printf("%d %d", a, b);
return 0;
}
求和
int sum(int* q)
{
int Sum = 0;
for (int j = 0; j < 5; j++)
{
Sum = *q + Sum;
q++;
}
return Sum;
}
void main()
{
int arr[5], * p;
for (int i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
p = arr;
printf("%d\n", sum(p) );
}