#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2

    Intialization list


    I'm studying a chapter on dynamic memory allocation, and I came up with a question on array initialization.
    When you declare an array, you have the option to initialize it with the initialization list {}.
    Why can't you do this outside declarations?
    What's so special about a declaration that allows you to use the list {}?

    I found this unintuitive, because for variables of simple data types, you can initialize the value either in the declaration or outside the declaration.
    Code:
    int x = 3;
    
    int x;
    x = 3;
    Both these are valid.

    I googled "programming c array initialization" and [URL = https://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fstrin.htm]this article[/URL] but I couldn't find my answer.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    once array is declared,name of array represent address so u can't assign list to array once its declared.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    Are you saying that
    1. a list is also a type of pointer
    2. because memory is allocated for the array, the array has its own address that is different from the list
    3. therefore assigning the list to the array would be saying like "3 = 5"?
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    Originally Posted by 046
    Are you saying that
    1. a list is also a type of pointer
    2. because memory is allocated for the array, the array has its own address that is different from the list
    3. therefore assigning the list to the array would be saying like "3 = 5"?
    yes exactly.
    name of array is constant pointer so u can't change its value once it's declared.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    name of array is constant pointer so u can't change its value once it's declared.
    Now I see; I forgot about the fact that you can't say
    Code:
    char str[30]
    str = "hello"
    which is what I was doing when I tried
    Code:
    int x[5]
    x = {1, 2, 3, 4, 5}

    Comments on this post

    • eramit2010 agrees : 100% correct :)
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Originally Posted by 046
    Code:
    int x = 3;
    
    int x;
    x = 3;
    Both these are valid.
    The first one is an initialization. The second is an assignment. Two very different things which are handled differently and which generate different code.

    When you use initialization, then the initialization takes place before the start of execution of the code: for globals that happens during program loading and start-up before main() gets called, for locals that happens during the set-up of the function call before the function call actually happens. Assignments happen during the execution of the code.

    x = 3; is a valid assignment statement.

    x = {1, 2, 3, 4, 5}; is not a valid assignment statement.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    So even though initialization and assignments look the same, both using the = sign, they're not.
    Initializations such as x = {1, 2, 3, 4, 5} can only take place at the beginning of a program / block.
    Therefore to "initialize" an array x = {1, 2, 3, 4, 5} outside a declaration is invalid, since it would take place during the execution of the code.

    Btw, I managed to watch the "Triumph of the Nerds" show yesterday.
    It was really interesting, and I had a lot of fun watching it.
    I did hear about GUIs being a big "revolution", but I never did understand how it changed the experience of using a computer.
    When the guy in the show demonstrated two different ways of copying a file, I really did feel the difference.

    Also, I had thought that Microsoft and Apple got famous independently of each other.
    So it was a surprise to me when I learned that Bill Gates actually provided application for Macintosh, as was the fact that a big guy from Pepsi had anything to do with Apple.

    And it was fun to see the hairstyle back then. (everyone looked like the Beatles to me)
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Actually, initialization and assignments do not look the same. Initializations are part of a declaration, whereas assignments are not.

    The way to "initialize" an array outside of a declaration is with a series of assignment statements, one for each element in the array (could be accomplished with a loop if the values are agreeable to the arrangement).

    The same guy did a few other shows, the only one of which I can remember the title of was "Nerds 2.0.1". In part not as interesting and in part very interesting. The not-so-interesting parts were about the Dot-Com (.com) start-up companies that were proliferating at the time, but then this was a few years before the dot-com bubble burst and most of those hot new companies disappeared.

    The interesting part of "Nerds 2.0.1" is tracing the development of the Advanced Research Projects Agency Network (ARPANET -- http://en.wikipedia.org/wiki/ARPANET), which went on to become the Internet. He shows us one of the first IMPs (Interface Message Processor) that's still installed in a major university library building hidden inside a wooden cabinet, though no longer being used. It's just a little smaller than a refridgerator and is so heavy it had to be lifted into place with a crane. We know them today as "routers", but part of the reason for the IMPs being so big and heavy was that they were modified computers, plus they were ruggedized in order to survive a nuclear attack, the main reason behind the development of the ARPANET. And when the ARPANET engineers told the telephone company what they wanted to do, Telco told them that packet switching was impossible. A lot of interesting Internet history there.

    BTW, I saw in the news that that engineer who invented the mouse died recently. I think "Triumph of the Nerds" showed him demonstrating the first mouse.

    PS
    Trivia Question:
    The European Organization for Nuclear Research, better known as CERN, provides particle accelerators for high-energy physics. Several important achievements in particle physics have been made during experiments at CERN.

    What is one of the greatest contributions that CERN has made to our everyday lives?

    Answer: HTTP and the World Wide Web. (http://en.wikipedia.org/wiki/Cern#Computer_science).
    Last edited by dwise1_aol; July 25th, 2013 at 04:05 PM.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    Initializations are part of a declaration, whereas assignments are not.
    I read the wiki article on initialization, and indeed it said that initializers are an optional part of a declarator.

    I think I wasn't making the appropriate distinction between "initializing with an initializer in the declaration" and "initializing with assignments in the body" as well as not properly understanding the meaning of "initialization", which is to assign an initial value to something.

    The interesting part of "Nerds 2.0.1" is tracing the development of the Advanced Research Projects Agency Network
    I'll need to check that out as well.

IMN logo majestic logo threadwatch logo seochat tools logo