English 中文(简体)
• 如何在C++中设置一个被堵塞的阵列?
原标题:How to create a jagged string array in c++?

我想在C++中建立双层阵列。

int arrsize[3] = {10, 5, 2};
char** record;
record = (char**)malloc(3);
cout << endl << sizeof(record) << endl;
for (int i = 0; i < 3; i++) 
{
    record[i] = (char *)malloc(arrsize[i] * sizeof(char *));
    cout << endl << sizeof(record[i]) << endl;
}

我想为姓名(应当有10封信)制定<代码>record[0](有5个数字标识)和(有2位数)。 如何执行? 我将记录阵列直接写到双亲档案中。 我不想使用<条码>truct和<条码>。

问题回答

在C++中,它希望:

std::vector<std::string> record;

为什么在你的问题得到明智的解决办法时,你会不使用某种障碍?

struct record {
   char name[10];
   char mark[5];
   char id[2];
};

之后写给双亲档案就变得微不足道:

record r = get_a_record();
write( fd, &r, sizeof r );

注:

  • 你可能希望为全国人民力量的确定者分配额外空间,但这取决于你想要在档案中使用的形式。

  • If you are writing to a binary file, why do you want to write mark and id as strings? Why not store an int (4 bytes, greater range of values) and a unsigned char (1 byte)

如果你坚持不使用用户定义的类型(即,你应该),那么你只能制造一个记忆和使用点算术的单一体,但必须认识到,汇编者生成的双元件相同,唯一的区别是,你的代码将无法维持:

char record[ 10+5+2 ];
// copy name to record
// copy mark to record+10
// copy id to record+15
write( fd, record, sizeof record);

实际上,正确的“小到小”模式是:

T * p = (T *) malloc(count * sizeof(T));

www.un.org/spanish/ecosoc 可以是任何类型,包括<代码>char*>。 因此,在这种情况下分配记忆的正确守则如下:

int arrsize[3] = { 10, 5, 2 };
char** record;
record = (char**) malloc(3 * sizeof(char *));
cout << sizeof(record) << endl;
for (int i = 0; i < 3; ++i) {
    record[i] = (char *) malloc(arrsize[i] * sizeof(char));
}

我删除了<条码>(记录),因为它总能产生(一)点 point(我的膝上型计算机4)。 体积在汇编时间方面起作用,对<条码>标明的记忆没有意义。 记录(i)(实际上是一个点——char*>/code>)在执行时间分配。

http://www.ohchr.org。 你gged的阵列将是一个包含特征阵列的阵列。 每一点人通常使用4tes(在32台轨道机上),但更正确的是<条码>(char*),因此,你应使用<条码>小范围(3*)

And then record[i] = (char*)malloc((arrsize[i]+1) * sizeof(char)), because a string is a char* and a character is a char, and because each C-style string is conventionally terminated with a character to indicate its length. You could do without it, but it would be harder to use for instance:

strcpy(record[0], name);
sprintf(record[1], "%0.2f", mark);
sprintf(record[2], "%d", id);

页: 1 页: 1 缩略语

As regards writing all this to a file, if the file is binary why put everything in as strings in the first place? Assuming you do, you could use something like:

ofstream f("myfile",ios_base::out|ios_base::binary);
for (int i=0; i<3; i++)
    f.write(record[i], arrsize[i]);
f.close();

话虽如此,我还是提出安德斯的想法。 如果你使用STL病媒和扼杀物,那么你就不得不处理令人厌恶的记忆分配问题,你的代码可能也会比较清洁。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签