25.以下程序的运行结果是 ______。 #include <stdio.h> void fun(int *p) { printf("%d\n",p[5]); } main() { int a[10]={1,2,3,4,5,6,7,8,9,10}; fun(&a[3]); } A.5 B.6 C.8 D.9
时间: 2023-09-20 15:06:04 浏览: 78
正确的答案是 B. 6。
解析:
- 程序中先定义了一个函数 fun,参数是一个整型指针。
- 在函数 fun 中,使用 p[5] 来访问指针 p 所指向的数组的第 6 个元素。
- 程序的主函数 main 中定义了一个整型数组 a,内容为 1 到 10。
- 然后调用函数 fun,传入的参数是数组 a 的第 4 个元素的地址,即 &a[3]。
- 在函数 fun 中,访问到的数组元素就是 a[3+5],即 a[8]。所以打印出的结果是 6。
所以,运行结果是选项 B. 6。
相关问题
这样开头写:#include <stdio.h> #include <stdlib.h> void PutRes(void); int fun(char *); int fun (char *p)
这段代码的开头定义了两个头文件:stdio.h 和 stdlib.h。这两个头文件分别提供了标准输入输出函数和内存管理函数的声明。
接下来是函数 PutRes 的声明,返回值为空,没有参数。注意,在 C 语言中,如果一个函数没有参数,需要在参数列表中显式地写上 void。
接下来是函数 fun 的声明,它有一个 char 类型的指针参数 p,返回值为 int 类型。这个函数的具体实现并没有给出,只是声明了函数名、参数和返回值类型。
*定义函数实现n个字符串按字典顺序排序 开头: #include<stdio.h> #include<string.h> void fun (char *str[],int n)
好的,以下是一个实现n个字符串按字典顺序排序的示例代码:
```
#include <stdio.h>
#include <string.h>
void fun(char *str[], int n) {
int i, j;
char *temp;
// 使用冒泡排序算法
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (strcmp(str[j], str[j+1]) > 0) {
// 交换两个字符串的位置
temp = str[j];
str[j] = str[j+1];
str[j+1] = temp;
}
}
}
}
int main() {
char *str[] = {"apple", "banana", "orange", "grape", "kiwi"};
int n = sizeof(str) / sizeof(str[0]);
fun(str, n);
printf("排序后的字符串数组:\n");
for (int i = 0; i < n; i++) {
printf("%s ", str[i]);
}
printf("\n");
return 0;
}
```
在这个示例代码中,我们使用了冒泡排序算法对字符串数组进行排序。在排序过程中,我们使用了 `strcmp()` 函数来比较两个字符串的大小关系,并使用指针变量 `temp` 来交换两个字符串在数组中的位置。最后,我们输出排好序的字符串数组。
请注意,这个示例代码中的字符串数组是在程序中直接定义的。如果您需要从用户输入中获取字符串数据,请使用 `scanf()` 或其他输入函数来读取数据,并将它们存储在字符串数组中。
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)