以下程序程序执行后的输出结果是()。 #include<stdio.h> void fun1(char *p) { char *q; q=p; while(*q!="\0") { (*q)++; q++; } } int main() { char a[]={"Program"),*p; p=&a[3]; fun1(p); printf("%s\n",a); return 0; }
时间: 2024-04-29 15:19:02 浏览: 17
程序执行后的输出结果是"Provnun"。原因是在fun1函数中,把指针q指向了字符串p,并不断将指针所指的字符加1,直到遇到字符串结束符"\0"为止。在主函数中,p指向了字符串a的第4个字符"gram",因此fun1函数会将"gram"之后的字符都加1,包括字符串结束符"\0",所以最终输出的结果是"Provnun"。
相关问题
*定义函数实现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()` 或其他输入函数来读取数据,并将它们存储在字符串数组中。
:C 5.下面程序执行后的输出结果是0。 #include <stdio.h> void fun(char *cint d) (*c=*c+1;d=d+1; printf(%c.%c".*c.d): int mainO (char a='A, b='a; fun(&b,a); printf("%c.%cc"a,b): return O; ,
答案是:程序会报编译错误。主函数中的fun(&b,a);应该是fun(&b,&a);,同时在fun函数中缺少引号,应该是printf("%c.%c",*c,*d);。修改后的程序如下所示:
```c
#include <stdio.h>
void fun(char *c, int *d) {
(*c) = (*c) + 1;
d = d + 1;
printf("%c.%c",*c,*d);
}
int main() {
char a='A', b='a';
fun(&b,&a);
printf("%c.%c",a,b);
return 0;
}
```
执行后的输出结果是:B.bA。
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)