& 和 * 之间的区别
Difference between & and *
这两个运算符有什么区别? 我的理解是,它们都指向使用它们的变量的内存位置。
前任。
int p;
foo(*p,&p);
&p
获取指向整数p
-> 的指针,即存储p
的内存地址。
*p
"取消引用"指针,即在p
提供的内存地址处查看对象并返回该对象。
上面的代码是无效的 C,因为您无法取消引用int
:
error: indirection requires pointer operand ('int' invalid)
请考虑以下事项:
// Create an integer
int p = 1234;
printf("Integer: %dn", p);
// Get the pointer to that integer, i.e. the memory address in which p is stored
int *pointer = &p;
printf("Pointer: %pn", pointer);
// Dereference the pointer to get the value back
int q = *pointer;
printf("Dereferenced: %dn", q);
提供以下输出:
Integer: 1234
Pointer: 0x7ffee53fd708
Dereferenced: 1234
另请注意,要打印出指针地址,我们必须使用特殊的格式说明符%p
而不是用于int
的%d
。
// declare an integer
int p = 42;
// declare a pointer type, and use it to store the address
// of the variable p.
int* ptr = &p;
// use the * to dereference the pointer, and read the integer value
// at the address.
int value = *ptr;
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 在 const 函数中通过引用和指针返回之间的区别
- 我想知道长双倍和双倍之间的区别
- 这 4 个 lambda 表达式之间有什么区别?
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- (double) 和 double() 之间的区别
- & 和 * 之间的区别
- std::is_convertible 和 std::convertible_to 之间的区别(在实践中)?
- 析构函数和'delete'之间的区别
- 在 typedef 内部使用 const 关键字和在 typedef 外部使用 const 关键字之间有区别吗?
- 从预序遍历构造 bst 的 c++ 和 python 解决方案之间的区别
- vector.back() 和 vector[vector.size() - 1] 之间的区别?
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- 函数签名与调用的函数不匹配,常量字符[]和字符*之间的区别?
- OpenMP #pragma omp for v/s #pragma omp parallel for 之间的区别?
- S() 与 S{} 之间的区别?