tags:

views:

74

answers:

3

Hello,

I have the following code. I have a method called SendBookDiffs(MTBookDiff *bookdiffs, UINT bookdiffs_total) which I would like to use. The description for this method is "This method accepts array of MTBookDiff structures, counting 'bookdiffs_total' elements."

So what I have tried is the following:

MTBookDiff *bookdiffs;
MTBookItem items[128];
bookdiffs->items = items;

But I encountered a "error C2106: '=' : left operand must be l-value" error. According to some replies, I have tried

memcpy(bookdiffs->items, items, sizeof(bookdiffs->items));

But is there the proper way of executing it? Thanks! Edited: Simplifying the question asked.

Based on the

#define MAX_PATH          260

enum EnMTAPIConstants
  {
   MT_SYMBOL_LEN              =32,                          
   MT_BOOK_DEPTH              =32,                          
   MT_ADDRESS_LEN             =64,                          
   MT_NEWSUBJECT_LEN          =256,                         
   MT_NEWSCATEGORY_LEN        =256,                         
   MT_LOGIN_LEN               =64,                          
   MT_PASSWORD_LEN            =64,                          
   MT_PARAMS_LEN              =256,                         
   MT_DESCRIPTION_LEN         =MAX_PATH                     
  }

struct MTBookItem
  {
   enum EnBookItemType
     {
      ItemReset=0,                                          
      ItemSell =1,                                          
      ItemBuy  =2                                          
     };
   UINT              type;                                  
   double            price;                                 
   INT64             volume;                                
   UINT              reserved[8];                           
  };

struct MTBookDiff
  {
   wchar_t           symbol[MT_SYMBOL_LEN];                 
   MTBookItem        items[MT_BOOK_DEPTH*4];                
   UINT              items_total;                           
   UINT              reserved[64];                          
  };

MTBookDiff *bookdiffs;
MTBookItem items[128];
bookdiffs->items = items;
+1  A: 

You need to do one of the following:

  • memcpy(bookdiffs->items, items, sizeof(bookdiffs->items)
  • change the type of the items fields from MTBookItem items[] to MTBookItem *items.

EDIT Given that this is a C++ question, using a std::vector as others have suggested is almost certainly better than either of my two suggestions.

Oli Charlesworth
This is very incomplete advice. `memcpy` is not the best way, and changing the definition from array to point has major side-effects.
Steven Sudit
Agreed. But we'd need more info from the OP to give a more complete answer. (Incidentally, I didn't notice this was C++ rather than C, hence the suggestion for `memcpy`).
Oli Charlesworth
@StevenWhat is the recommend way then?
@OliWhat sort of info?
I took a stab at trying to explain what's going on. Please feel free to improve upon it.
Steven Sudit
Currently, I am trying to make use of the method SendBookDiffs(MTBookDiff *bookdiffs, UINT bookdiffs_total)Based on the structs available, I created a pointer to a MTBookDiff struct to create an array. Since MTBookDiff items require an array of MTBookItem, I created MTBookItem items[128];
Allocating 128 elements is bad for two reasons: 1) You will rarely want to use all 128. 2) You might sometimes what to use more than 128. In short, the fixed number is bad. The more flexible approach, though one requiring a better grasp of memory handling and pointers, is to allocate only as much as you need.
Steven Sudit
@Oli: Don't knock your suggestion. While I agree that, given than this turns out to be C++ and not C, using STL is a great idea, there's still the matter of first understanding the basics. STL is not a replacement for that. And, as part of the basics, it's important to understand why you need `memcpy` or the equivalent when filling an array.
Steven Sudit
@Steven: Really, go and read _Accelerated C++_. It's an excellent book to read even for a seasoned C++ programmer, __because it teaches you to see C++ from a different angle__. I have taught C++ for a long time, and followed its approach: Teach the safe idioms first, so that students can create good, meaningful programs right from the start. Teach the underlying mechanisms only later, when the students have a firm grasps of the simple stuff. To use `std::vector<>` to store your items, you don't need to know about templates and pointers and `new[]` and `delete[]`. You can learn that later.
sbi
@sbi: I'm not going to weigh in on whether the C++ or ++C approach works best for teaching, because I think it does depend on the student. My point here is that, whichever side you approach this from, you can't *stop* at level of only knowing how to use STL.
Steven Sudit
@Steven: No, you never stop learning C++. But _using_ the STL is much simpler than _implementing_ it, and much more useful in your daily work - which is why it should be taught first. _If_ someone stops learning after a while, then rather at the stage where they can successfully use high-level abstractions, but can't do manual memory management, than vice versa. When using C++ in the way it should, IMO, be used today, you rarely ever get down to the dirty engine room. You can count on your fingers the times I typed `delete` in the last decade. (And I've done quite performance-critical stuff.)
sbi
+2  A: 

you can't assign an array to another array as you do.

instead since you are anyway using C++, use vector

vector<MTBookItem> items
Anders K.
Using STL is certainly good advice, but here's someone who doesn't know the difference between a pointer and an array yet...
Steven Sudit
@Steven: Which is fine for quite a while when using the STL. Good books teach low-level features like pointers and arrays and manual resource management long after they taught how to use `std::vector` and the like.
sbi
+1  A: 

You need to distinguish between a[] and *a. The first allocated a block of memory big enough to hold the specified number of elements, leaving the variable pointing at the first. The second is just a pointer, which doesn't necessarily point at anything yet and which can be repointed elsewhere.

At risk of using an unpopular notation, Hungarian notation strictly distinguishes between ax and px, where the former is an array and the latter is a pointer. What's confusing is that you can pass ax to a function taking px, in which case the parameter is a pointer that is initialized to the first element of ax. However, while you can assign a pointer a new value, you can't do that for an array.

Steven Sudit