How to input char array in c++ in a single line?












0















I just encountered an input statement for char array in c++ and I didn't understand how it works.



char s[100];
cin >> s + 1;
cout << s + 1;


Input : Harold



Output: Harold



Can anyone explain to me how it works?










share|improve this question




















  • 2





    It works exactly like any other char array. Sorry we don't know how much of C++ you know, so it's a bit difficult to pick a starting point for an explanation. Why exactly are you confused about these lines?

    – n.m.
    Dec 28 '18 at 18:58











  • If it said ((&s[0]) + 1) would you understand it?

    – David Schwartz
    Dec 28 '18 at 21:55











  • Make these changes char s[100] = "a"; Now at the end add cout << 'n' << s << 'n'; With that, can you tell what is going on?

    – David C. Rankin
    Dec 28 '18 at 22:02











  • @n.m. I was wondering how it works. We used to use loops to input an array but didn't know about this. Now I get this. Thanks a lot

    – Bhashkar Pandit
    Dec 30 '18 at 6:40











  • @DavidSchwartz Yeah i got this. Thanks a lot.

    – Bhashkar Pandit
    Dec 30 '18 at 6:44
















0















I just encountered an input statement for char array in c++ and I didn't understand how it works.



char s[100];
cin >> s + 1;
cout << s + 1;


Input : Harold



Output: Harold



Can anyone explain to me how it works?










share|improve this question




















  • 2





    It works exactly like any other char array. Sorry we don't know how much of C++ you know, so it's a bit difficult to pick a starting point for an explanation. Why exactly are you confused about these lines?

    – n.m.
    Dec 28 '18 at 18:58











  • If it said ((&s[0]) + 1) would you understand it?

    – David Schwartz
    Dec 28 '18 at 21:55











  • Make these changes char s[100] = "a"; Now at the end add cout << 'n' << s << 'n'; With that, can you tell what is going on?

    – David C. Rankin
    Dec 28 '18 at 22:02











  • @n.m. I was wondering how it works. We used to use loops to input an array but didn't know about this. Now I get this. Thanks a lot

    – Bhashkar Pandit
    Dec 30 '18 at 6:40











  • @DavidSchwartz Yeah i got this. Thanks a lot.

    – Bhashkar Pandit
    Dec 30 '18 at 6:44














0












0








0








I just encountered an input statement for char array in c++ and I didn't understand how it works.



char s[100];
cin >> s + 1;
cout << s + 1;


Input : Harold



Output: Harold



Can anyone explain to me how it works?










share|improve this question
















I just encountered an input statement for char array in c++ and I didn't understand how it works.



char s[100];
cin >> s + 1;
cout << s + 1;


Input : Harold



Output: Harold



Can anyone explain to me how it works?







c++ input c++14






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 21:41









HolyBlackCat

15.9k33363




15.9k33363










asked Dec 28 '18 at 18:04









Bhashkar PanditBhashkar Pandit

43




43








  • 2





    It works exactly like any other char array. Sorry we don't know how much of C++ you know, so it's a bit difficult to pick a starting point for an explanation. Why exactly are you confused about these lines?

    – n.m.
    Dec 28 '18 at 18:58











  • If it said ((&s[0]) + 1) would you understand it?

    – David Schwartz
    Dec 28 '18 at 21:55











  • Make these changes char s[100] = "a"; Now at the end add cout << 'n' << s << 'n'; With that, can you tell what is going on?

    – David C. Rankin
    Dec 28 '18 at 22:02











  • @n.m. I was wondering how it works. We used to use loops to input an array but didn't know about this. Now I get this. Thanks a lot

    – Bhashkar Pandit
    Dec 30 '18 at 6:40











  • @DavidSchwartz Yeah i got this. Thanks a lot.

    – Bhashkar Pandit
    Dec 30 '18 at 6:44














  • 2





    It works exactly like any other char array. Sorry we don't know how much of C++ you know, so it's a bit difficult to pick a starting point for an explanation. Why exactly are you confused about these lines?

    – n.m.
    Dec 28 '18 at 18:58











  • If it said ((&s[0]) + 1) would you understand it?

    – David Schwartz
    Dec 28 '18 at 21:55











  • Make these changes char s[100] = "a"; Now at the end add cout << 'n' << s << 'n'; With that, can you tell what is going on?

    – David C. Rankin
    Dec 28 '18 at 22:02











  • @n.m. I was wondering how it works. We used to use loops to input an array but didn't know about this. Now I get this. Thanks a lot

    – Bhashkar Pandit
    Dec 30 '18 at 6:40











  • @DavidSchwartz Yeah i got this. Thanks a lot.

    – Bhashkar Pandit
    Dec 30 '18 at 6:44








2




2





It works exactly like any other char array. Sorry we don't know how much of C++ you know, so it's a bit difficult to pick a starting point for an explanation. Why exactly are you confused about these lines?

– n.m.
Dec 28 '18 at 18:58





It works exactly like any other char array. Sorry we don't know how much of C++ you know, so it's a bit difficult to pick a starting point for an explanation. Why exactly are you confused about these lines?

– n.m.
Dec 28 '18 at 18:58













If it said ((&s[0]) + 1) would you understand it?

– David Schwartz
Dec 28 '18 at 21:55





If it said ((&s[0]) + 1) would you understand it?

– David Schwartz
Dec 28 '18 at 21:55













Make these changes char s[100] = "a"; Now at the end add cout << 'n' << s << 'n'; With that, can you tell what is going on?

– David C. Rankin
Dec 28 '18 at 22:02





Make these changes char s[100] = "a"; Now at the end add cout << 'n' << s << 'n'; With that, can you tell what is going on?

– David C. Rankin
Dec 28 '18 at 22:02













@n.m. I was wondering how it works. We used to use loops to input an array but didn't know about this. Now I get this. Thanks a lot

– Bhashkar Pandit
Dec 30 '18 at 6:40





@n.m. I was wondering how it works. We used to use loops to input an array but didn't know about this. Now I get this. Thanks a lot

– Bhashkar Pandit
Dec 30 '18 at 6:40













@DavidSchwartz Yeah i got this. Thanks a lot.

– Bhashkar Pandit
Dec 30 '18 at 6:44





@DavidSchwartz Yeah i got this. Thanks a lot.

– Bhashkar Pandit
Dec 30 '18 at 6:44












1 Answer
1






active

oldest

votes


















1














When you give the input 'Harold', the indices from 1 to 6 (As you do with s+1) will be assigned with the given characters. Other indices still contain garbages.
This array looks like this: {'X','H','a','r','o','l','d','X','X',........} (Here 'X' means garbage)



But the thing is that when you want to get this array as output from s+1 (literally s[1]), It shows only the value you have assigned. Hence the output 'Harold'.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53962568%2fhow-to-input-char-array-in-c-in-a-single-line%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    When you give the input 'Harold', the indices from 1 to 6 (As you do with s+1) will be assigned with the given characters. Other indices still contain garbages.
    This array looks like this: {'X','H','a','r','o','l','d','X','X',........} (Here 'X' means garbage)



    But the thing is that when you want to get this array as output from s+1 (literally s[1]), It shows only the value you have assigned. Hence the output 'Harold'.






    share|improve this answer






























      1














      When you give the input 'Harold', the indices from 1 to 6 (As you do with s+1) will be assigned with the given characters. Other indices still contain garbages.
      This array looks like this: {'X','H','a','r','o','l','d','X','X',........} (Here 'X' means garbage)



      But the thing is that when you want to get this array as output from s+1 (literally s[1]), It shows only the value you have assigned. Hence the output 'Harold'.






      share|improve this answer




























        1












        1








        1







        When you give the input 'Harold', the indices from 1 to 6 (As you do with s+1) will be assigned with the given characters. Other indices still contain garbages.
        This array looks like this: {'X','H','a','r','o','l','d','X','X',........} (Here 'X' means garbage)



        But the thing is that when you want to get this array as output from s+1 (literally s[1]), It shows only the value you have assigned. Hence the output 'Harold'.






        share|improve this answer















        When you give the input 'Harold', the indices from 1 to 6 (As you do with s+1) will be assigned with the given characters. Other indices still contain garbages.
        This array looks like this: {'X','H','a','r','o','l','d','X','X',........} (Here 'X' means garbage)



        But the thing is that when you want to get this array as output from s+1 (literally s[1]), It shows only the value you have assigned. Hence the output 'Harold'.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 28 '18 at 22:28









        smac89

        12.3k43675




        12.3k43675










        answered Dec 28 '18 at 22:24









        Muhimin_OsimMuhimin_Osim

        112




        112






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53962568%2fhow-to-input-char-array-in-c-in-a-single-line%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas