函数指针和传递指针之间的区别
Difference between Function pointer and passing pointers
我有这些代码...
#include <iostream>
using namespace std;
int * Function (int a, int b);
int main()
{
int a = 2;
int b = 7;
int * x = &a;
int * y = &b;
cout << a << " " << *x << endl;
cout << b << " " << *y << endl;
Function (a , b);
cout << endl << endl;
cout << a << " " << *x << endl;
cout << b << " " << *y << endl;
cout << endl << endl;
cout << a << " " << *x << endl;
cout << b << " " << *y << endl;
Function (a , b);
cout << endl << endl;
cout << a << " " << *x << endl;
cout << b << " " << *y << endl;
cout << endl << endl;
cout << a << " " << *x << endl;
cout << b << " " << *y << endl;
system ("PAUSE");
return 0;
}
int * Function (int a, int b)
{
int pom;
pom = a;
a = b;
b = pom;
}
这是函数指针,不更改变量。我不知道为什么,以及功能指针到底是什么意思。为什么这种指针有用?
对
#include <iostream>
using namespace std;
int Function (int * a, int * b);
int main()
{
int a = 2;
int b = 7;
int * x = &a;
int * y = &b;
cout << a << " " << *x << endl;
cout << b << " " << *y << endl;
Function (&a , &b);
cout << endl << endl;
cout << a << " " << *x << endl;
cout << b << " " << *y << endl;
cout << endl << endl;
cout << a << " " << *x << endl;
cout << b << " " << *y << endl;
Function (&a , & b);
cout << endl << endl;
cout << a << " " << *x << endl;
cout << b << " " << *y << endl;
cout << endl << endl;
cout << a << " " << *x << endl;
cout << b << " " << *y << endl;
system ("PAUSE");
return 0;
}
int Function (int * a, int * b)
{
int pom;
pom = *a;
*a = *b;
*b = pom;
}
这是普通的通过指针,这改变了一个变量,就像书本学习一样。我感兴趣的是如何通过指针传递 expamle 数组。
你混合了这些概念,这些都不是功能指针
int * Function (int a, int b);
int Function (int * a, int * b);
这:
int * Function (int a, int b);
是一个函数,它接受值 2 ints 并返回指向 int 的指针
而这个
int Function (int * a, int * b);
是一个函数,它接受 2 个指向 int 的指针并返回一个 int
相关文章:
- 在 const 函数中通过引用和指针返回之间的区别
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- 在 Rust 中,指针和引用有什么区别?
- 在以下声明中使用指针有什么区别?
- C++指针中的这两种类型的值分配有什么区别?
- 按值和引用传递指针之间的区别?
- printf() 和 std::cout 在指针方面的区别
- *++*++ppp,*++pp[1],*++(*(1+ppp)有什么具体的区别吗?(C/C++指针问题)
- 'new'和'='与指针的区别
- 使用指针向量和非指针向量有什么区别
- 函数指针和传递指针之间的区别
- C 中的通用指针和 C++ 中的通用指针有什么区别?
- 直接为浮点变量分配十六进制整数与通过指针转换分配之间的区别
- Fortran 中的指针和C++有什么区别
- 带"new"的指针和带"&variable"的指针有什么区别
- 指针声明和间接寻址之间的区别
- 在矢量的尖括号内使用指针符号与在外部使用指针符号有什么区别
- 在C++中,a+i 和 &a[i] 在指针算术方面有什么区别?
- ->运算符和指针 * 之间的区别 (C++)
- 当返回一系列字符或指针到char文字时的区别