# include < stdio . h > int main () ( int sub ( int x , int y , int * z ); int a =0, b =0, c =0; sub (10,5,& a ); sub (7, a ,& b ); printf ("%4d,%4d,%4d\ n ", a , b , c ); return 0; int sub ( int x , int y , int * z ) * z = y - x ; return (* z ); 程序运行结果为
时间: 2024-03-11 09:48:47 浏览: 31
程序运行结果为:
```
5, 2, 0
```
解释:
1. 首先执行 `sub (10,5,& a );`,将 `a` 的值赋为 `5`,`*z` 的值也为 `5 - 10 = -5`,但由于 `*z` 没有被使用,所以无影响。
2. 接着执行 `sub (7, a ,& b );`,将 `b` 的值赋为 `a - 7 = 5 - 7 = -2`,`*z` 的值也为 `-2 - 5 = -7`,但由于 `*z` 没有被使用,所以无影响。
3. 最后输出 `a`、`b`、`c` 的值,即 `5`、`-2`、`0`。
相关问题
#include <stdio.h> intx=2,y=3; void sub( int y ) {x++;y++;} void main() {intx=4;sub(x); printf("x+y=6d\n",x+y);
这段程序有语法错误,因为在函数 `sub` 的定义中,形式参数 `y` 的类型没有指定,应该是 `int` 类型。同时,在 `printf` 函数中,应该使用 `%d` 格式化输出整数,而不是 `%6d`。修正后的程序如下:
```c++
#include <stdio.h>
int x = 2, y = 3;
void sub(int y) {
x++;
y++;
}
void main() {
x = 4;
sub(x);
printf("x+y=%d\n", x + y);
}
```
程序的输出结果是:
```
x+y=8
```
首先定义了全局变量 `x` 和 `y`,并分别赋值为 2 和 3。然后定义了函数 `sub`,其中的形式参数 `y` 是一个局部变量,与全局变量 `y` 没有关系。在 `main` 函数中,将全局变量 `x` 赋值为 4,然后调用函数 `sub`,将全局变量 `x` 作为实际参数传递给 `sub` 函数。在 `sub` 函数中,将全局变量 `x` 的值加 1,将局部变量 `y` 的值加 1,但这个操作并没有影响到全局变量 `y` 的值。最后使用 `printf` 函数输出全局变量 `x` 和 `y` 的和,即 8。
检查下面的代码,找出错误并修改:#include<stdio.h> #include<string.h> int substring(char *str,char *sub) { int count=0,counti,countj; for(counti=0;counti<strlen(str);counti++) { for(countj=0;countj<strlen(sub);countj++) { if(str[counti+countj]!=sub[countj]) break; } if(countj==strlen(sub)) count++; } return count; } int main() { char str[50],sub[10]; gets(str); gets(sub); printf("%d\n",substring(str,sub)); return 0; }
这段代码的问题在于使用了不安全的函数`gets()`,它没有限制输入的字符串长度,可能导致缓冲区溢出漏洞。建议使用更安全的函数`fgets()`代替。
修改后的代码如下:
```
#include <stdio.h>
#include <string.h>
int substring(char *str, char *sub) {
int count = 0, counti, countj;
for (counti = 0; counti < strlen(str); counti++) {
for (countj = 0; countj < strlen(sub); countj++) {
if (str[counti + countj] != sub[countj])
break;
}
if (countj == strlen(sub))
count++;
}
return count;
}
int main() {
char str[50], sub[10];
fgets(str, 50, stdin);
fgets(sub, 10, stdin);
str[strcspn(str, "\n")] = '\0'; // 去掉字符串末尾的换行符
sub[strcspn(sub, "\n")] = '\0'; // 去掉字符串末尾的换行符
printf("%d\n", substring(str, sub));
return 0;
}
```
修改后的代码中使用了`fgets()`函数来获取输入字符串,并通过`strcspn()`函数去掉输入字符串末尾的换行符。这样可以避免缓冲区溢出漏洞。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)