Total members 11894 |It is currently Sun Nov 24, 2024 11:07 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Code:
1: #include <iostream>
2:
3: int main()
4: {
5: short age[4];
6: short same_age[4];
7: age[0]=23;
8: age[1]=34;
9: age[2]=65;
10: age[3]=74;
11:
12: same_age=age; --> Array copying
13:
14: std::cout << same_age[0] << std::endl;
15: std::cout << same_age[1] << std::endl;
16: std::cout << same_age[2] << std::endl;
17: std::cout << same_age[3] << std::endl;
18: return 0;
19: }



Array copying is done in line number 12. Is this allowed in C++? I did
googling, got few answers saying "No". :grin:




Author:
Proficient
User avatar Posts: 280
Have thanks: 1 time

Arrays can be copied but only by copying their individual
elements. E.g. you can use the library function strcpy() to
copy strings (arrays of char).

Because of the close link between array names and pointers,
assigning an array name does not copy the underlying array,
but instead makes the pointer an alias for the array name:

Code:
int i;
int a[] = {1, 2, 3};
int *b = a;


Now a[i] and b[i] refer to the same area of memory. The
array values themselves have not been copied. Changing b[i]
will change a[i] and vice versa, because they're the same
thing.

If you want to copy an array, as John Matthews suggests, you
can wrap it in a struct. Structs can be copied and can even
be returned by functions.

_________________
Please recommend my post if you found it helpful


Author:
Proficient
User avatar Posts: 228
Have thanks: 0 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : C++ array copying
 Array difference for associate array     -  
 compare an array with another array?     -  
 Pop the element off the end of array     -  
 Here is how to display any 2d array     -  
 PHP Array Functions     -  
 Array Passing     -  
 Array shuffle     -  
 Average of an array. Please help     -  
 Imploding an Array     -  
 array unshift     -  



Topic Tags

C++ Arrays
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com