dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] C++ what means assignment an array with just 1 value?

  1. #1
    Join Date
    Apr 2009
    Posts
    1,272

    [RESOLVED] C++ what means assignment an array with just 1 value?

    see these line:
    Code:
    int hey[3][4]={100};
    all array elements will be '100' or just the 1st and the others will be random?

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: C++ what means assignment an array with just 1 value?

    The 1st will be 100 and the others will be 0.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  3. #3
    Join Date
    Apr 2009
    Posts
    1,272

    Re: C++ what means assignment an array with just 1 value?

    correct me more: if i use zero instead '100', all will be zero, right?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,248

    Re: C++ what means assignment an array with just 1 value?

    Quote Originally Posted by Cambalinho View Post
    correct me more: if i use zero instead '100', all will be zero, right?
    Why not simply try it?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,624

    Re: C++ what means assignment an array with just 1 value?

    Quote Originally Posted by Cambalinho View Post
    correct me more: if i use zero instead '100', all will be zero, right?
    Just have a look at https://en.cppreference.com/w/c/lang...initialization
    Victor Nijegorodov

  6. #6
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    Re: C++ what means assignment an array with just 1 value?

    If initialisation values are specified, then elements are initialised from the value(s) specified until they are used. If all the elements are not initialised, those left are default initialised with the default value for the used type. For int this is 0. So if you use zero (0) instead of 100, then the first element will be initialised to this value (0) and the rest will be default initialised to 0.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  7. #7
    Join Date
    Apr 2009
    Posts
    1,272

    Re: C++ what means assignment an array with just 1 value?

    i did tried and i getted zero.... i did had sure if was the correct value or random..
    thank so much for all to all... thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)