0

I am learning and I'd like to know the best way how to do the following array copy, Consider this code:

void Cast1LineSpell(UINT Serial, char *chant)
{
    byte packet[] = { 0x0F, 0x03, 
        (Serial >> 24) & 0xFF, (Serial >> 16) & 0xFF,(Serial >> 8) & 0xFF, Serial & 0xFF, 
        0x9A, 0x92, 0x00, 0x00, 0x00, 0x1A };

    byte prepareSpell[2] = { 0x4D, 0x01 };

    byte chant_length = sizeof(chant) / sizeof(chant[0]);
    byte chant_name[] = { 0x4E, chant_length, }; // <= how can i put the bytes in chant* into the rest of this array, and then append bytes 0x00 and 0x4E on to the end of it?
}

how can i put the bytes that are inside of *chant, and then put them into the end of chant[] and then append bytes 0x00 and 0x4E on to the end of it?

Can anybody provide a solution? Much Appreciated.

6
  • How should that work? What output do you expect?
    – K-ballo
    Dec 23, 2012 at 0:43
  • I'd like chant[] to have the output of 0x4D, 0x01, chant_length, whatever is in *chant, 0x00, 0x4E
    – Dean
    Dec 23, 2012 at 0:45
  • 1
    You'd be better off using a container like std::vector<char> since this is tagged C++..
    – K-ballo
    Dec 23, 2012 at 0:46
  • If I use a container, how can I convert it into bytes?
    – Dean
    Dec 23, 2012 at 0:46
  • 1
    what is byte - this isn't a C++ type.
    – user93353
    Dec 23, 2012 at 0:48

2 Answers 2

0

You are using dynamic arrays so the sizeof(chant) will always be the size of a pointer, and sizeof(chant) / sizeof(chant[0]) won't be the number of elements in the array. That only works for static arrays.

Also, you are redeclaring chant which is simply an error.

In conclusion, since you do not know the number of elements in chant, there is no way to do what you want to do.

5
  • I don't know what you mean I thought sizeof(array) / sizeof(array[0]) gives me the number of elemenths?
    – Dean
    Dec 23, 2012 at 0:50
  • 1
    @Dean: It does when applied to a static array, not when you apply it to a pointer. The size of a pointer is not the same as the size of an array...
    – K-ballo
    Dec 23, 2012 at 0:50
  • @Dean (And by "static array" we mean "array".)
    – melpomene
    Dec 23, 2012 at 0:53
  • 1
    The object has to be an array type for sizeof to return the size of the array. It doesn't matter whether it is statically allocated or dynamically allocated or variable length array, it just has to be an array type, and char * is not an array type, it is a pointer type.
    – dreamlax
    Dec 23, 2012 at 1:17
  • @dreamlax: That's a nice way to put it!
    – K-ballo
    Dec 23, 2012 at 1:19
0

According to my understanding, in C++, all arrays passed into a function are treated as pointers no matter they are statically allocated or dynamically allocated or even you write the argument as char chant[], (i.e., only the address of the first element is passed in).

Example:

void f(int value[]){
    cout<<"size in f: "<<sizeof(value)/sizeof(int)<<endl;
}

int main(){
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };

    cout<<"size in main: "<<sizeof(arr)/sizeof(int)<<endl;

    f(arr);

    return 0;
}

the result is:

size in main: 8
size in f: 1

So as you can see, in f(), value[] is the same as value *, and sizeof(value) is the size of the pointer.

You should (always) also pass in the length when you pass an array into a function.

void f(int value[], size_t size){
    cout<<"size in f: "<<size<<endl;
}

int main(){
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };

    size_t size = sizeof(arr)/sizeof(int);

    cout<<"size in main: "<<size<<endl;

    f(arr, size);

    return 0;
}

The output:

size in main: 8
size in f: 8

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.