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.

jack moore
  • 1,989
  • 4
  • 25
  • 22
  • 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. – CB Bailey Aug 30 '10 at 08:20
  • What are you trying to do? assigning a pointer to int? adding string and integer? – lalli Aug 30 '10 at 08:20
  • 7
    You should probably [get a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) on C++ instead of guessing around. You'll get much further. Look into `std::vector`, or `boost::/std::array`. – GManNickG Aug 30 '10 at 08:21
  • Trying to copy myarray to temparray. – jack moore Aug 30 '10 at 08: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. – Martin York Aug 30 '10 at 08:27
  • `temparray` is an `int` not an array so you can't... which is what I mean. Unless you say what you want to do (e.g. "I want to create a temporary array containing copies of the values of another array"), we can only guess what you mean. – CB Bailey Aug 30 '10 at 08:52

3 Answers3

8
#include <cstring>

int temparray[10] ;
memcpy (temparray, myarray, sizeof (myarray)) ;
TonyK
  • 16,761
  • 4
  • 37
  • 72
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.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • `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](http://stackoverflow.com/questions/3144904/may-i-take-the-address-of-the-one-past-the-end-element-of-an-array-closed). – fredoverflow Aug 30 '10 at 11:47
  • @FredOverflow: Are you sure about the technically undefined behaviour? http://stackoverflow.com/questions/988158/ – CB Bailey Aug 30 '10 at 12:23
  • 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. – Puppy Aug 30 '10 at 13:17
  • 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. – Puppy Aug 30 '10 at 13:42
  • No, `sizeof` yields the number of bytes, not elements. – fredoverflow Aug 30 '10 at 14:28
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)

Kamil Klimek
  • 12,884
  • 2
  • 43
  • 58