用指针:#include <stdio.h> int main() { int x, y; scanf("%d %d", &x, &y); int min = x < y ? x : y; printf("%d\n", min); return 0; }
时间: 2024-01-14 12:01:56 浏览: 97
以下是使用指针实现此功能的 C 语言程序:
```c
#include <stdio.h>
int main() {
int x, y;
scanf("%d %d", &x, &y);
int *ptr_x = &x;
int *ptr_y = &y;
int *ptr_min = (*ptr_x) < (*ptr_y) ? ptr_x : ptr_y;
printf("%d\n", *ptr_min);
return 0;
}
```
程序首先使用 `scanf` 函数从键盘输入两个整数 `x` 和 `y`,然后定义两个指针变量 `ptr_x` 和 `ptr_y`,并将它们分别指向变量 `x` 和 `y`。
接着,定义一个指针变量 `ptr_min`,使用条件运算符 `?:` 比较指针所指向的值的大小,并将较小的值所在的指针赋值给 `ptr_min`。
最后,使用 `printf` 函数输出指针变量 `ptr_min` 所指向的值,并在末尾加上换行符 `\n`。
注意,在比较指针所指向的值的大小时,需要使用 `*` 运算符取得指针所指向的值,即 `(*ptr_x) < (*ptr_y)`。
当输入样例为 `5 8` 时,程序将输出 `5`。
相关问题
#include<stdio.h> #include<stdlib.h> int cmp(const void *a,const void *b) { return (*(long *)b > *(long *)a) ? 1 : -1; } int main() { int i, k, n, m, cnt = 0, flag = 1; long x, s[10] = {0}; scanf("%d %d", &n, &m); int len = m < n ? m
这段代码是一个简单的用C语言实现的排序程序。程序首先读取输入的n和m,然后从输入中读取n个long类型的整数,并将它们存储在一个大小为10的long类型数组s中。接着,程序使用快速排序算法对s数组进行排序,按照从大到小的顺序排列。排序后,程序再输出排序后的前m个元素。
具体来说,程序定义了一个名为cmp的比较函数,该函数用于比较两个long类型的整数的大小。然后,程序使用qsort函数对s数组进行排序,其中第一个参数是数组的起始地址,第二个参数是数组的元素个数,第三个参数是每个元素的大小,第四个参数是一个指向比较函数的指针。
程序使用变量len来表示实际需要排序的元素个数。如果m小于n,则仅需要对前m个元素进行排序,否则需要对所有n个元素进行排序。最后,程序循环输出排序后的前m个元素,直到输出完毕为止。
#include<stdio.h> #include<string.h> struct student { int id; int age; }; int main() { struct student stu1; struct student *p1 = &stu1; scanf("%d %d",p1->age,p1->id); printf("%d %d",p1->age,p1->id); return 0; } 这串代码哪里错了
这段代码有两处错误:
1. scanf() 函数中应该传入参数的地址,而不是参数的值。因此,应该将第 10 行的 scanf() 函数修改为:
```c
scanf("%d %d", &p1->age, &p1->id);
```
2. 在第 11 行中,应该使用箭头运算符 -> 访问结构体指针中的成员,而不是使用点运算符 .。因此,应该将第 11 行的 printf() 函数修改为:
```c
printf("%d %d", p1->age, p1->id);
```
修改后的代码如下:
```c
#include<stdio.h>
#include<string.h>
struct student {
int id;
int age;
};
int main() {
struct student stu1;
struct student *p1 = &stu1;
scanf("%d %d", &p1->age, &p1->id);
printf("%d %d", p1->age, p1->id);
return 0;
}
```
阅读全文
相关推荐

















