下面C++程序的输出是_________。
void f(char * x) { x++; *x='a'; } int main() { char str [sizeof("hello")]; strcpy(str, "hello"); f(str); cout<<str; return 0; }
str是一个指针,f(str)是地址传递,可以改变地址所指向内容的值,但是地址str本身没有改变,若想要改变地址str的值,需要传二级指针(&str)
#include<iostream>
#include <stdio.h>
#include <string.h>
using namespace std; void f(char *x) {
printf("指针地址%d\n", x);
x++;
*x='a';
printf("指针地址%d\n", x);
}
int main() {
char str [sizeof("hello")];
strcpy(str, "hello");
printf("数组地址%d\n", str);
f(str);
printf("数组地址%d\n", str);
cout << str;
return 0;
}
注意这里传进去的参数是字符数组的地址,在f()函数里面对字符数组进行修改操作,所以字符变成hallo.
回到main函数,str指针的值并没有发生改变,还是指向字符h。所以输出hallo