0

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
  • 1
    you cannot copy array using assignment. Either copy arrays (using std::copy) or use vector Commented Jul 26, 2010 at 6:40

3 Answers 3

2

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

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

vector<MTBookItem> items
Sign up to request clarification or add additional context in comments.

2 Comments

Using STL is certainly good advice, but here's someone who doesn't know the difference between a pointer and an array yet...
@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.
1

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.

Comments

1

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.

10 Comments

This is very incomplete advice. memcpy is not the best way, and changing the definition from array to point has major side-effects.
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).
@Steven What is the recommend way then?
I took a stab at trying to explain what's going on. Please feel free to improve upon it.
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];
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.