如何在此结构的定义中指向下一个结构的指针:
typedef struct A {
int a;
int b;
A* next;
} A;
Run Code Online (Sandbox Code Playgroud)
这是我第一次写它但它不起作用的方式.
出于某种原因,我无法使用附加到我想要使用的对象的函数.我在不起作用的行中添加了注释.作为一个错误,我得到"错误;不允许指向不完整类类型的指针"请帮助
这是dokter.ccp中的代码
int counter = 0;
for (list<Wielrenner*>::iterator it = wielrenners.begin(); it != wielrenners.end(); it++){
Wielrenner* wielrennerOB = *it;
cout << "\nID: " << counter;
cout << "List size: " << persons.size() << endl;
wielrennerOB->print(); // This is not working
counter++;
}
Run Code Online (Sandbox Code Playgroud)
这是wielrenner.h中的代码
#ifndef WIELRENNER_H_
#define WIELRENNER_H_
//#include <fstream>
#include "persoon.h"
#include "Onderzoek.h"
class Wielrenner :
public Persoon
{
public:
Wielrenner(string, string, Adres, string, Datum, Datum, string, int, float, float, float,list<Onderzoek>* );
~Wielrenner(void);
int getLengte() const;
float getGewicht() const;
float …Run Code Online (Sandbox Code Playgroud) 我一直在研究Cell处理器,我正在尝试创建一个结构,它将保存一个spe_context_ptr_t,它将在线程中用于启动一个spe上下文,并且还将保存指向其他将被传递给来自线程内的spu上下文(目前我正试图让它成为通用指针,但实际上它将是指向我定义的另一个结构的指针).当我尝试编译时,我收到以下错误:
spu/../common.h:38: error: expected specifier-qualifier-list before 'spe_context_ptr_t'
// here is the offending line(s)
typedef struct _PTHREAD_BLOCK {
spe_context_ptr_t * context; // Error happens here
uintptr32_t args;
} PTHREAD_BLOCK;
Run Code Online (Sandbox Code Playgroud) 我可以在不同的函数中访问局部变量吗?如果是这样,怎么样?
void replaceNumberAndPrint(int array[3]) {
printf("%i\n", array[1]);
printf("%i\n", array[1]);
}
int * getArray() {
int myArray[3] = {4, 65, 23};
return myArray;
}
int main() {
replaceNumberAndPrint(getArray());
}
Run Code Online (Sandbox Code Playgroud)
上面一段代码的输出:
65
4202656
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?"4202656"是什么意思?
我是否必须在replaceNumberAndPrint()函数中复制整个数组才能比第一次更多地访问它?
我正在为嵌入式系统编写软件.
我们使用指针访问FPGA器件的寄存器.
一些寄存器是只读的,而另一些寄存器是只写的.
只读寄存器在读取时将产生未定义的值.
我想定义一个指针类型,允许编译器检测何时从只写寄存器(也称为解除引用)读取值.
只使用C语言语法创建只写指针吗?
(我们正在使用C开发第一个原型,但是在第二代转向C++.)
如何在C++中创建高效的只写指针?(请记住,这不是跟踪动态内存中的项目,而是访问硬件地址.)
此代码用于安全性和质量最受关注的嵌入式系统.
程序:
#include<stdio.h>
int main(void) {
int x[4];
printf("%p\n", x);
printf("%p\n", x + 1);
printf("%p\n", &x);
printf("%p\n", &x + 1);
}
Run Code Online (Sandbox Code Playgroud)
输出:
$ ./a.out
0xbff93510
0xbff93514
0xbff93510
0xbff93520
$
Run Code Online (Sandbox Code Playgroud)
我希望以下是上述程序的输出.例如:
x // 0x100
x+1 // 0x104 Because x is an integer array
&x // 0x100 Address of array
&x+1 // 0x104
Run Code Online (Sandbox Code Playgroud)
但是最后一个陈述的输出与我预期的不同.&x也是数组的地址.因此,在此处递增1将打印地址递增4.但是&x+1地址递增10.为什么?
请考虑以下代码:
int* p1 = new int[100];
int* p2 = new int[100];
const ptrdiff_t ptrDiff = p1 - p2;
int* p1_42 = &(p1[42]);
int* p2_42 = p1_42 + ptrDiff;
Run Code Online (Sandbox Code Playgroud)
现在,标准保证p2_42指向p2[42]哪个?如果没有,在Windows,Linux或webassembly堆上总是如此吗?
最近提出了C++核心指南(恭喜!),我担心gsl::not_null类型.如I.12中所述:声明一个不能为null的指针not_null:
帮助避免解除引用nullptr错误.通过避免对nullptr进行冗余检查来提高性能.
...
通过声明源代码中的意图,实现者和工具可以提供更好的诊断,例如通过静态分析查找某些类错误,并执行优化,例如删除分支和空测试.
目的很明确.但是,我们已经有了语言功能.不能为null的指针称为引用.虽然引用一旦创建就无法反弹,但这个问题可以解决std::reference_wrapper.
我gsl::not_null和之间的主要区别在于,std::reference_wrapper后者只能用于代替指针,而前者适用于任何事物 - 可nullptr分配(引自 F.17:使用not_null来表示"null"不是有效值):
not_null不只是内置指针.它的工作原理为array_view,string_view,unique_ptr,shared_ptr,和其他类似指针的类型.
我想象功能比较表如下:
T&:
nullptr?- 是的std::reference_wrapper<T>:
nullptr?- 是的gsl::not_null<T*>:
nullptr?- 是的现在这里是问题,最后:
std::reference_wrapper现在没用了?PS我创建了标签cpp-core-guidelines,guideline-support-library为此,我希望正确.