English 中文(简体)
快速新运营商问题
原标题:Quick new operator issue
  • 时间:2011-11-18 17:28:24
  •  标签:
  • c++

我知道这确实是容易的,我看一看问题,但这是我掌握的:

typedef struct
{
    char s1[81];
    char s2[81];
    char s3[81];
}Rec;

int main()
{   

   Rec *a[10];

   a[0] = (Rec*)new unsigned char(sizeof(Rec));
   a [0]->s1=“hello”
   printf("a[0] = %s
",a[0]->s1);
   delete(a[0]);


   getchar();
   return 0;
}

现在:

a [0]->s1=“hello”

对这一说法的申诉必须是可变的利价值。 我确信,这确实是我如何把它投放到我的新运营线上,它需要一个长期的价值或东西,但我不相信守则能够这样做......容易知道,而是肯定的。 任何帮助都将受到高度赞赏。

最佳回答

你们不能被分配到像这样的果园。 要么使用斜体,要么将你的焦炭阵列改为std:string

strcpy(a[0]->s1, "hello");

Why are you doing this:

a[0] = (Rec*)new unsigned char(sizeof(Rec));

而不是:

a[0] = new Rec;
问题回答

两点。 段 次

a[0] = (Rec*)new unsigned char(sizeof(Rec));

分配一个<代码>的单一签名代号,该编码已初步输入<代码>sizeof(Rec)。 可能的话

a[0] = (Rec*)new unsigned char[sizeof(Rec)];

a[0] = new Rec;

第二,你不能把直面字面上传给一系列的果园,你需要用一个字面复制,例如。

char s[80];
s = "hello"; // won t work
strcpy(s, "hello"); // correct

不过,请在此处使用<代码>:>。

我猜想,你们在生活中做了很多工作。 铭记C++是different 语言,与C大部分星群和一些标准图书馆共享。 这意味着在C++中,C级的罚款可能相当高(甚至危险)。

顺便说一句,请以更“C++-ish”的方式重写你的法典:

#include <iostream> // std::cout, std::endl
#include <string> // std::string

struct Rec // typedef is implicit for structs in C++
{
    std::string s1; // use std::string instead of char arrays
    std::string s2;
    std::string s3;
}; // don t forget the semicolon!

int main()
{   

   Rec * a[10];

   a[0] = new Rec; // allocates the right amount of memory, no need to cast
   a[0]->s1 = "hello"; // std::sring handles the assignment for you
   std::cout << "a[0] = " << a[0]->s1 << std::endl; // use iostreams
   delete a[0]; // delete is an operator, not a function, no need for parentheses

   getchar(); // warning, this is not portable
   return 0;
}

如你所知,<代码>新不是“经过改进的<代码>小”。 它的类型(不需要投放)、使用更加安全(分配所需记忆的准确数量、不需要<>条码>的<>条码/代码>)以及“<条码>>、cannot :它援引班级建筑商(作为<条码><><>><>>>条/代码>援引承载人)。

在C++,如C,alplace不同于 初始<>。 在C部分,只有<代码>mset。 在C++物体的构造中,零块可能更为复杂。 因此,请在never上使用 Smalloc,以创建具有非属地构造者(或拥有无三维构体的田地-Rec就是这种情况。 由于<条码>新始终有效,而且具有额外特点,你应当加以利用。

问题不在于你。 您的新表述分配了一张<条码>的单一签名<>条/代码>,并将其引入<代码>sizeof(Rec),而不是将足够的空间分配到<条码>新的未签署果[(Rec)];。 尽管如此,<代码>1和>的类型有所不同,你可以相互调和。 您应当使用诸如<代码>strcpy等内容,但既然你把这个标题贴上了。 C++ 然后,你将更好地利用<代码>std:string。 另外,为什么不只打电话new Rec;?

a[0] is a pointer to an array of chars that can t be modified -- a[0] will always point to the same address. you need to use strcpy to copy from your "hello" string to a[0]





相关问题
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?

热门标签