&a v.s. a v.s. &a[0]

Discussion in 'C++' started by Wenjie, Jun 24, 2003.

  1. Wenjie

    Wenjie Guest


    I have an array: unsigned char a[1024];
    I want to copy some data into it:
    memcpy(a, anotherArray, sizeof(a));

    But I saw somebody is using this:
    memcpy(&a, anotherArray, sizeof(a));
    And it passed the tests (VC++6.0).

    Could someone confirm me that the 2nd use of
    of &a is not valid? Why?
    Also I would think it is OK to do:
    memcpy(&a[0], anotherArray, sizeof(a));

    Best regards,
    Wenjie, Jun 24, 2003
    1. Advertisements

  2. You would pretty quickly see the difference though if you did this

    memcpy(&a + 1, anotherArray, sizeof a - 1);

    or this

    memcpy(a + 1, anotherArray, sizeof a - 1);

    or this

    memcpy(&a[1], anotherArray, sizeof a - 1);

    The first is a bug, the second and third are OK. Try printing out the
    pointer values to see the difference.

    John Harrison, Jun 24, 2003
    1. Advertisements

  3. The point is that they will still compile. The "bug" is due
    to undefined behaviour, not due to "illegal C++ construct".

    Victor Bazarov, Jun 24, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.