我想手动将Pointer的地址设置为存储在字符串变量中的值.我有:
addr : String;
ptr : Pointer;
Run Code Online (Sandbox Code Playgroud)
然后:
addr:='005F5770';
Run Code Online (Sandbox Code Playgroud)
如何将其分配给ptr?
如上所述,this当在类中定义的结构中调用时,指针如何起作用?假设我有以下代码:
class A {
public:
struct {
void bar() { std::cout << this << std::endl; }
} foo;
};
int main() {
A a; a.foo.bar();
std::cout << &a << std::endl;
std::cout << &a.foo << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这会产生这个输出:
0x62fe9f
0x62fe9f
0x62fe9f
Run Code Online (Sandbox Code Playgroud)
a.foo与之共享同一个地址a,如何才能访问this指针foo?
使用this->foo引发错误:
test.cpp:20:23: error: 'struct A::<anonymous>' has no member named 'foo'
由于C语言中没有数组这样的东西,以下是否都存储在一个内存位置,或者每个元素的值存储在内存位置的"数组"中?
int array[] = {11, 13, 17, 19};
Run Code Online (Sandbox Code Playgroud)
场景1
{11, 13, 17, 19} --> location A
Run Code Online (Sandbox Code Playgroud)情景2
{
11 --> location A
13 --> location B
17 --> location C
19 --> location D
}
Run Code Online (Sandbox Code Playgroud)哪一个是有效的内存布局?
我正在通过矩阵数学来学习Rust.第一步是创建一个新的行和列矩阵,并使用行 - 主要顺序的值初始化矩阵.
如果我想将输入矩阵值向量作为&[T]传递,但我不确定如何使用元素值初始化矩阵.
pub struct Matrix<T> {
data: Vec<T>,
row: usize,
col: usize,
}
/// Creates a new matrix of `row` rows and `col` columns, and initializes
/// the matrix with the elements in `values` in row-major order.
pub fn new(row: usize, col: usize, values: &[T]) -> Matrix<T> {
Matrix {
data: *values, // ??
row: row,
col: col,
}
}
fn main() {
let x = Matrix::new(2, 3, &[-2, -1, 0, 1, 2, 3]);
let y = Matrix::new(2, …Run Code Online (Sandbox Code Playgroud) #include <iostream>
using namespace std;
void fn(int* input_ptr) {
int *ptr2 = input_ptr;
cout << "pointer 2 is " << *ptr2 << endl;
}
int main() {
int *ptr1;
*ptr1 = 7;
fn(ptr1);
}
Run Code Online (Sandbox Code Playgroud)
这个例子工作,因为我传递一个指向函数的指针,并将其分配给函数内部的临时指针,结果显示指针2也是7.但是,
#include <iostream>
using namespace std;
int main() {
int *ptr1;
*ptr1 = 7;
int *ptr2;
*ptr2 = ptr1; // or I have also tried *ptr2 = *ptr1
cout << "pointer 2 is " << *ptr2 << endl;
}
Run Code Online (Sandbox Code Playgroud)
同一步骤在主函数中不起作用.我知道你可以使用没有星号的地址ptr2 = ptr1,它会工作.
但是,在第一个例子中,我可以直接将指针作为函数参数分配给带有星号的新指针(或称为dereferecing?),但在第二个示例中,我不能在main函数中执行此操作.
有人可以帮忙解决这个问题吗?感谢你的时间.
我有两个整数指针,用于保存(SDL)屏幕高度和宽度的数据(以像素为单位).他们在这里宣布:
int *w = nullptr;
int *h = nullptr;
Run Code Online (Sandbox Code Playgroud)
给出这里的值:
int SDL_GetRendererOutputSize(SDL_Renderer* renderTarget, int *w, int *h);
Run Code Online (Sandbox Code Playgroud)
并在这里使用:
Dest.w = (*w / 2.0f - 58 / 2.0f, *h / 2.0f - 8 / 2.0f);
Dest.h = (*w / 2.0f - 58 / 2.0f, *h / 2.0f - 8 / 2.0f);
Dest.x = (*w / 2.0f - 58 / 2.0f, *h / 2.0f - 8 / 2.0f);
Dest.y = (*w / 2.0f - 58 / 2.0f, *h / 2.0f - 8 …Run Code Online (Sandbox Code Playgroud) 我有这样的代码
int main()
{
char* word = "kotok";
char* word1 = word;
while (word1 != '\0'){
printf("%s ", word1);
word1++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是在打印"kotok,otok,tok,ok,k"之后它并没有结束.此代码继续打印一些垃圾,然后停止.如何克服它?
我试图添加,word1[strlen(word1)] = '\0';但它给出了分段错误.
根据我对使用const类型限定符和指针的理解,它取决于你使用它的位置.
const MyType *
Run Code Online (Sandbox Code Playgroud)
意味着该位置无法修改,但该位置的值可以.
MyType const *
Run Code Online (Sandbox Code Playgroud)
意味着可以修改位置,但不能修改位置的值.
由此,我认为没有理由不能使以下内容无效,
const MyType const *
Run Code Online (Sandbox Code Playgroud)
定义一个位置固定的指针,并且无法修改指向的值.但是,这就是"不止一次使用相同类型的限定符".我应该忽略这个吗?我对指针上下文中const语义的理解是否有缺陷?
我和我的朋友在我们的代码中使用结构(我们的代码彼此分开).让我们举个例子:
struct Book {
char title;
int pages;
}
void setBook(struct Book *tempBook) {
tempBook->title = "NiceTitle";
tempBook->pages = 50;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码很简单.问题是,拥有这两个主电源是否有任何区别:
int main() {
struct book obj1;
setBook(&obj);
}
Run Code Online (Sandbox Code Playgroud)
要么
int main() {
struct Book *obj2;
setBook(obj2);
}
Run Code Online (Sandbox Code Playgroud)
编辑:我的发言中并不清楚.我已经把品酒初始化了
struct Book *obj2 = malloc(sizeof(struct obj2));
Run Code Online (Sandbox Code Playgroud) 从我现在所理解的,我可以说C#中的引用是一种指向具有引用计数的对象的指针,并且知道类型兼容性.我的问题不是关于值类型如何与引用类型不同,而是关于如何实现引用的更多信息.
我已经阅读了这篇文章,介绍了引用和指针之间的差异,但这并没有涵盖引用的内容,但是它与C++中的指针相比,它描述了更多的属性.我也理解传递引用传递值之间的差异(因为C#对象默认按值传递,甚至是引用),但是当我试图向我解释时,我很难理解什么是真正的引用.同事为什么通过引用发送的参数不能存储在闭包内,如Eric Lippert 博客条目中关于堆栈作为实现细节.
有人可以向我提供一个完整的,但希望简单的解释,说明C#中的引用是什么,还有一些关于它们是如何被赋予的?
编辑:这不是重复,因为在C#中的Reference类型中,它解释了引用如何工作以及它与值的不同之处,但我要问的是如何在低级别定义引用.