为什么这是可行的?
int **ar1 = new int*[5];
虽然这会在c++中抛出一个错误:
int *ar2[] = new int*[5];
我知道在第一种情况下,ar1表示一个指向指针的指针,而在第二种情况下,ar2表示一个指向整数的指针数组。假设"new int*5“这一行意味着new运算符返回指向内存空间的指针以存储5个指向整数的指针,那么第二种情况有什么问题?
如果我用默认构造函数声明一个类,并用初始化列表定义这个类的一个实例,如下所示,这个定义会调用默认构造函数吗?为什么会被调用或者没有被调用?
class Sample
{
// this should be any aggregate type in c++
};
int main()
{
Sample s = {0};
return 0;
}
当我的程序初始化时,我试图将任意数量的字符串项读取到一个结构数组中。我想分配堆内存给 当编译器到达下一行时,它抛出一个error: invalid initializer。 我代码的第一部分: int main() {
printf("Work starts in the vineyard.\n");
typedef struct {
char* name[20];
unsigned int jobs;
}Plantation;
// read from list of plantations
FIL
我已经得到了一些需要修补的代码,这是一个囚犯困境游戏,最初玩家是用这段代码设置成一个数组的。
IPDPlayer[] currentPlayers = { new RandomPlayer(),
new TFTPlayer(), new TFTPlayer(), new TFTPlayer(),
new CoopPlayer(), new CoopPlayer(), new CoopPlayer(),
我创建了一个简单的程序来测试智能指针。我开始使用标准库,但后来我想使用boost。我有这样的编译问题:
In file included from main.cpp:1:0:
test.hpp:14:21: error: ‘p’ is not a type
shared_ptr<int>a (p);
^
我的文件和makefile:
test.hpp:
#include <string>
#include <stdlib.h>
#include <vector>
#include <memory>
我是一个C编程的初学者,我不知道这两个表达式(带和不带大括号)之间有什么区别,如下所示。
char s1[] = {"The course of true love never did run smooth"};
char s1[] = "The course of true love never did run smooth";
我尝试使用以下命令进行测试
printf("%c", s1[0]), and
printf("%s", s1)
都给了我相同的答案。
这段代码
std::initializer_list<const char*> list {"something", "somthingElse" /*..*/};
const char* array[] = list;
编译失败,出现以下错误:
array initializer must be an initializer list
我不能真正理解我在这里做错了什么,因为我毕竟是在使用initializer_list。
(我使用initializer_list的原因是为了以后可以在代码的几个部分中使用list.size();每次我在列表中添加/
我过去常常这样初始化我的结构:
struct A a = {0};
这似乎对我很有效,但是我被争论关于ANSI,C89,C99标准。
简单地说,我在任何文档中都找不到。
你能帮我吗?
这是一个'cl‘的例子(VS express 2008)。
#include <stdio.h>
struct DATA
{
int a;
int b;
char tab[3];
};
int main(void)
{
struct DATA A;
struct DATA B = {0};
printf("A.a: %d, A.b:
我试图在我的函数中执行下面的操作,但一直收到分段错误。当我尝试使用[iModify - 1]作为我的索引时,它失败了。
在C中,可以不使用int变量计算作为索引吗?
int modify(pb *PhoneBook)
{
int x;
int iModify = 0;
char name_num[] = {'\0'};
print(PhoneBook);
printf("\nWhich entry would you like to modify? ");
scanf("%d", &iModify);
我一直在研究一个现有的C99代码库,该代码库使用指针指向到处都是打包的结构成员。这会导致窗体出现警告。 my_file.c:xxx:yy: error: taking address of packed member of 'struct some_packed_struct' may result in an unaligned pointer value [-Werror=address-of-packed-member] 大多数情况下,这些警告是在将指向结构成员的指针传递给函数时生成的。例如: int bar(const int *val)
{ ... }
struc
我有一个类functionCombiner,它的构造函数如下所示
FunctionCombiner::FunctionCombiner(std::vector<std::shared_ptr<valuationFunction>> Inner_) : Inner(std::move(Inner_)), valuationFunction("", 0) //<- Needs to initalize itself even though it gets all data from inner functions.
{
}
在我的主类中,我这样命名它: