如何初始化这个可变大小的结构?

时间:2014-10-24 20:03:46

标签: c++

如何初始化变量大小的C ++结构" StructB"在下面声明:

#include <iostream>
#include <string>

typedef struct {
 char* a;
 int b;
} StructA;

typedef struct {
  StructA* pointers[];
  int pcount;
  int numbers[];
  int ncount;
} StructB;

int main()
{
    StructB *sb = new StructB; // I need 'sb' to be allocated in the heap
    sb->pcount = 5;
    sb->ncount = 3;
    sb->pointers = new StructA*[sb->pcount];
    sb->numbers = int[sb->ncount];
}

我遇到了这些编译错误。这些错误意味着什么,我该如何修复它们?谢谢你。

In function 'int main()': 
21:43: error: incompatible types in assignment of 'StructA**' to 'StructA* [0]' 
22:19: error: expected primary-expression before 'int' 
22:19: error: expected ';' before 'int'

1 个答案:

答案 0 :(得分:0)

听这不是很好的c ++方式,你在C ++环境下做C 如果它给你一些优势(IO,类型检查等)或学习,那就没关系。 C风格的动态内存分配完全是手动的。解除分配也是如此。 由于您使用的是typedef,因此typedef可以使您和编译器更清楚。 这是一个折磨的例子,我认为你想做什么。 请注意,它已完成&#34; The Hard Way&#34;在这种情况下,这实际上是简单的方法 C ++中的C风格编码;

#include <iostream>
#include <string>


typedef struct {
char* a;
int b;
} StructA;


typedef StructA* A_p;   // A_p is "pointer to structA" (contains address of struct)                                                                           

typedef A_p* A_p_Array; // A_p_Array is "pointer to A_p"  (aka StructA**) contains address of A_p                                                             

typedef int* int_Array; // int_Array is "pointer to int" contains address of integer                                                                          


typedef struct {
 A_p_Array A_pointers;
 int pcount;
 int_Array numbers;
 int ncount;
} StructB;


int main()
{
 int i;

 StructA *sa = new StructA;

 StructB *sb = new StructB;

 sb->pcount = 5;
 sb->ncount = 3;

 A_p_Array  tmp_A_array;
 A_p        tmp;

 for ( i = 0 ; i < sb->pcount; i++)
 {
  tmp_A_array = new A_p; // create a StructA pointer                                                                                                      
  tmp = new StructA;     // create a StructA                                                                                                              
  tmp_A_array = &tmp;    // put address of tmp in mp_A_array                                                                                              
  tmp_A_array++;         // increment array address                                                                                                       
 }


 sb->A_pointers = tmp_A_array; // A_pointers now contains address of dynamically created array                                                               

 tmp_A_array = NULL;  // clear pointer but do NOT delete!                                                                                                    

 int_Array tmp_i_array;
 for ( i = 0 ; i < sb->ncount; i++)
 {
  tmp_i_array = new int(0); // c++ can create ints with initial values                                                                                    
  tmp_i_array++;
 }

 sb->numbers = tmp_i_array;
 tmp_i_array = NULL;  // clear pointer but do NOT delete!                                                                                                    


 /****** USE  Structs A & B    *****/

 // clean up the heap                                                                                                                                        

 A_p Ap;

 for ( i = 0 ; i < sb->pcount; i++)
 {
  Ap =  sb->A_pointers[i];
  delete Ap;  // each struct released separately
 }

 int* ip;
 for ( i = 0 ; i < sb->ncount; i++)
 {
  ip = & sb->numbers[i];
  delete ip;  //each int released separately
 }

 delete sb;

  return 0;
} // main

有更好的方法来做到这一点 这是发明c ++和更高语言的一个原因。 当你上课时,它会更容易一些 但你可以遇到同样类型的问题 所以得到指针和指针指向下来 现在,以后会为你提供良好的服务。