【发布时间】:2011-10-12 01:34:15
【问题描述】:
我必须创建一个 char 指针的二维数组。该数组将存储姓名和姓氏的列表 - 第 0 行将保存姓名,第 1 行将保存姓氏。这是我目前写的代码(这个文件包含在主文件中):
#include "myFunction.h"
#include <iostream>
#include <string.h>
using namespace std;
char ***persons;
void createArray(int n)
{
*persons = new char * int[n];
for(int i=0; i<n; i++)
*persons[i]=new char[n];
}
main 调用这个函数:
createArray(3);
但是当我运行它时,我不断收到“分段错误”,我不知道为什么
我该如何解决这个问题?
【问题讨论】:
-
*persons 将导致第一个 segv。
-
您正在创建一个指向二维字符数组的指针,这与二维字符指针数组完全不同(真的!)。你要哪个?
标签: c++ pointers char segmentation-fault new-operator