# Thread: Initializing an array with -1

1. ## Initializing an array with -1

Hi
can I write
Code:
` int array[5]={2};`
to initialize every value of the array with 2?

2. It will only initialize the first element with 2. All other elements will be initialized with 0. As if you coded it as:

Code:
`int array[5] = { 2, 0, 0, 0, 0 };`

3. Originally Posted by rstanley
It will only initialize the first element with 2. All other elements will be initialized with 0. As if you coded it as:

Code:
`int array[5] = { 2, 0, 0, 0, 0 };`
isn't there a quick way to s=initialize the array with specific value?!

4. Use a loop to assign the initial values.

5. Originally Posted by laserlight
Use a loop to assign the initial values.
Another question if i have struct Book b1 ; struct Book b2 which b1,b2 are variable of struct book type.. What happened exactly when I do b1=b2? Will all the values in b2 moved to b1? If so then the data of b1 is gone .. Not logically!

6. Will all the values in b2 moved to b1?
Copied, not moved. Both b1 and b2 will contain the same data. But be careful if your structure contains pointers, only the pointer will be copied the actual data will not be copied.

7. Originally Posted by jimblumberg
Copied, not moved. Both b1 and b2 will contain the same data. But be careful if your structure contains pointers, only the pointer will be copied the actual data will not be copied.
But if pointers copied ... actual data will be also copied why not? Can you give me an example?

8. Originally Posted by RyanC
But if pointers copied ... actual data will be also copied why not? Can you give me an example?
How would the "actual data" be copied just because the pointer is copied? Give an example.

9. Originally Posted by john.c
How would the "actual data" be copied just because the pointer is copied? Give an example.
if the pointer copied then it means the address that was pointing at ..will be copied to the new variable ..so if i have the address in the new variable I can get the data..

10. You can get the data, but the data hasn't been copied. If I copy a treasure map, the treasure itself doesn't get copied...

11. Originally Posted by GReaper
You can get the data, but the data hasn't been copied. If I copy a treasure map, the treasure itself doesn't get copied...
Got you!!! Thanks alot .. Copied means if there's change in one of variables thats copied it will not change in both.. Because it's COPIED

Thanks!!