我正在研究一个布尔代数的简单程序,但双重否定不能按预期工作.
我有以下课程:
运营商:
#ifndef OPERATOR_H
#define OPERATOR_H
class Operator {
public:
virtual int getArity(void) const = 0;
virtual bool calc(void) const = 0;
};
#endif // OPERATOR_H
Run Code Online (Sandbox Code Playgroud)
假:
#ifndef FALSE_H
#define FALSE_H
#include "operator.h"
class False : public Operator {
public:
int getArity() const {
return 0;
}
bool calc(void) const {
return false;
}
};
#endif // FALSE_H
Run Code Online (Sandbox Code Playgroud)
不:
#ifndef NOT_H
#define NOT_H
#include "operator.h"
class Not : public Operator {
public:
Not(Operator& child) : m_child(child) {
std::cout << …Run Code Online (Sandbox Code Playgroud) 我想动态分配一个字符串数组.所以arr应该包含指向每个字符串的第一个字符的二十个指针.
为简单起见,每个字符串都是相同的,存储在base中.第一个for循环现在应该创建我的数组,这似乎工作得很好.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n = 20;
char** arr = malloc( sizeof(char*) + n );
char* base = "abcdefghijklmnopqrstuvwxyz";
for(int i = 0; i < n; i++)
{
*(arr + sizeof(char*) * i) = base;
}
for(int i = 0; i < n; i++)
{
printf("%s\n", *(arr + sizeof(char*) * i));
}
}
Run Code Online (Sandbox Code Playgroud)
循环的秒数在第二次迭代期间创建分段错误.