f1,(*f1),f2,(*f2)之间有什么区别?(功能)和(&功能)有什么区别?
#include <iostream>
using namespace std;
void function (char *s) {
cout << s << endl;
}
int main () {
void (*f1) (char*) = &function;
void (*f2) (char*) = function;
f1 ("f1 function without dereference.");
有人能告诉我指针调用和引用调用之间的确切区别吗?这两个案子到底发生了什么?
Eg:
引用调用:
void swap(int &x, int &y)
{
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */
return;
}
swap(a, b);
指针调用:
void swap(int *x, int *y)
{
int temp;
temp = *x; /* s
首先,
我知道&和*变量之间的区别,但对类的对象不了解。
考虑到我有一门课让我们称之为“数学”
Math math; // a simple object called math
Math* mathPtr = &math; // this will holds the address of math object
Math& mathRef = math ; // this one is exactly equivalent to the previous one
有什么区别?!
我知道这是一个非常基本的问题。我有一些内存问题需要澄清。以下是我的疑虑:
int *p = malloc (50); // will allocate 50 bytes and it is pointed by p.
// Freeing C pointer-->
free(p);
Objective-C指针:
ClassAobject *objA = .... // allocated ClassAobject..
// Freeing obj-C pointer--->
objA = nil // Is it enough??? will it release all
我有一个Text类,它有某些方法将指针返回给自己,允许将调用链接起来。(老实说,我只是喜欢链子的外观和感觉!)
我的问题是,在实践中哪个更好(在安全性>通用性>性能方面)?返回和使用推荐信?还是返回并使用指针?
这两者的一个例子,从指针版本开始:
class Text{
public:
Text * position(int x, int y){
/* do stuff */
return this;
}
Text * write(const char * string);
Text * newline();
可能重复:
要在C++中进行逐引用调用,我想我可以使用这两种方法中的任何一种:
int f(int *x);
int g(int &x);
他们会被这样称呼:
int *w;
f(w);
g(*w);
int y;
f(&y);
g(y);
f和g的函数有什么不同吗?我应该能够在这两个函数中使用x作为int*指针,*x作为int。那有什么区别呢?
我有一个函数可以添加两个nos并返回(a+b)。然后我创建了一个指向func的func指针。希望为该函数指针的数组分配内存并访问它们。密码在下面。
我的问题是使用malloc的下面一行:
pp = (add_2nos*)malloc(5 * sizeof(add_2nos*))
Add_2nos*和add_2nos在编译时没有任何区别。如果有什么区别呢?另外,如果在我分配相同类型的内存时需要类型转换.?
#include <stdio.h>
#include <stdlib.h>
int add(int a, int b) {
return (a+b)
const限定符有一些问题。我试过这样做:
int a{ 3 };
int *p{ &a };
//const int **pp{ &p }; // FIXME: Compiler demands that p must have a const int *type
const int *const* pp{ &p }; // works here but it means, that I have a pointer to the const pointer to the const value
*pp = nullptr; // doesn't work c
下面的代码有什么不同:
int a;
int *p;
p=&a;
function(p);
和
int a;
function(&a);
我正在读一本书,在书中他们有时使用第一个代码,有时使用另一个代码。这不是一本好书(本地作者写的)。
这两个代码的工作方式是否相同,或者有什么不同?
另外,在效率方面有什么不同吗?有那么重要吗?
谢谢