Thread: Initializing an array with -1

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    Initializing an array with -1

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

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    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. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by rstanley View Post
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use a loop to assign the initial values.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    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. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    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. #7
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by jimblumberg View Post
    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?
    Last edited by RyanC; 12-04-2018 at 04:50 PM.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by RyanC View Post
    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.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  9. #9
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by john.c View Post
    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. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    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...
    Devoted my life to programming...

  11. #11
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by GReaper View Post
    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!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing a 3D array
    By thefirstone92 in forum C Programming
    Replies: 5
    Last Post: 10-01-2013, 02:28 PM
  2. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 08:26 PM
  3. Initializing an Array
    By IdioticCreation in forum C++ Programming
    Replies: 9
    Last Post: 04-30-2007, 03:49 PM
  4. help on initializing array
    By neversell in forum C Programming
    Replies: 6
    Last Post: 06-30-2002, 05:07 AM
  5. Initializing an Array to Zero
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 03-06-2002, 12:49 PM

Tags for this Thread