3

Is it possible to do something like this in C++ (can't test it myself right now)?

int myarray[10] = {111,222,333,444,555,666,777,888,999,1234};

void functioncc()
{
 int temparray = myarray;
 for(int x=0; x<temparray.length; x++){
    .... do something
 }

}

And maybe this (but i dont think it is):

int array1[5] = {0,1,2,3,4,5,6,7,8,9};
int array2[5] = {9,8,7,6,5,4,3,2,1,0};

void functioncc(int arid)
{
  temparray[10] = "array"+arid;
  ........

}

I can do stuff like that in JavaScript, but like I said - don't think it would be possible in C++.

Thanks for your time.

6
  • Can you ask what you want to know is possible rather than posting some incorrect code and asking if something similar is possible? It's not possible to know exactly what you are trying to do from your code. Commented Aug 30, 2010 at 8:20
  • What are you trying to do? assigning a pointer to int? adding string and integer? Commented Aug 30, 2010 at 8:20
  • 7
    You should probably get a book on C++ instead of guessing around. You'll get much further. Look into std::vector, or boost::/std::array. Commented Aug 30, 2010 at 8:21
  • Trying to copy myarray to temparray. Commented Aug 30, 2010 at 8:24
  • 3
    Applying techniques from one language to another is never productive. Each language has its own way of doing things. In C++ both the above affects can be achieved just the method and style of doing so are different. Commented Aug 30, 2010 at 8:27

3 Answers 3

8
#include <cstring>

int temparray[10] ;
memcpy (temparray, myarray, sizeof (myarray)) ;
Sign up to request clarification or add additional context in comments.

3 Comments

<memory> is not the correct header for memcpy, try <cstring> or <string.h> . In C++, many people would prefer std::copy from <algorithm> .
Thanks Charles! I've changed it to <cstring>.
@Charles: Many people still prefer memcpy in this case because of the annoying deprecation (sic) warnings MSVC gives when using std::copy...
3

Sure.

int myarray[] = {111,222,333,444,555,666,777,888,999,1234};

void function() {
    std::vector<int> temparray(std::begin(myarray), std::end(myarray));
}

Do note that the use of static non-const variables in this way is really looked down on, and if you pass them to other functions, you will have to also pass the "end" pointer.

However, C++ is so distinct from Javascript, seriously, just don't bother. If you need to code C++, get an actual C++ resource and learn it. The syntax for the basic stuff is the ONLY thing in common.

5 Comments

myarray[sizeof(myarray)] is definitely out of bounds. I think you meant myarray[sizeof(myarray) / sizeof(int)] which will probably work but is technically undefined behavior.
@FredOverflow: Are you sure about the technically undefined behaviour? stackoverflow.com/questions/988158
I'm pretty sure that &myarray[sizeof(myarray)] is well-defined behaviour, as it's legal to point one past the end of an array.
Can't edit my comment: I'm pretty sure that sizeof(array) returns a size in elements, not bytes. But I've barely ever had to use it, so.
No, sizeof yields the number of bytes, not elements.
1

Both cases are impossible. You must either put array length as an argument (know about it), or put inside of array some kind of "terminator" as last element. (I.E. in pointer array put NULL pointer at end of array)

Comments

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.