gpt4 book ai didi

c++ - 如何初始化 C 字符串数组(无 STL)

转载 作者:太空宇宙 更新时间:2023-11-04 14:40:10 25 4
gpt4 key购买 nike

我想在 MSVC2010 中用零指针初始化 C 字符串数组

// Foo.h
#pragma once
class Foo {
int sz_;
char **arr_;
public:
Foo();
~Foo();
// ... some other functions
};

// Foo.cpp
#include "Foo.h"
#define INITIAL_SZ 20

Foo::Foo() : sz_(INITIAL_SZ) {
// there I have to initialize arr_ (dynamic array and can be enlarged later)
arr_ = (char **)calloc(INITIAL_SZ * sizeof (char *)); // ???
// or maybe arr_ = new ...
}

如何正确初始化arr_?不允许我使用 STL、MFC 等。

最佳答案

arr = new char*[INITIAL_SZ](); 可以 - 您甚至可以将其放入初始化列表中。

关于c++ - 如何初始化 C 字符串数组(无 STL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6142735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com