expected output and the practical output not matching, please explain the logic behind the code












0















    #include<stdio.h>

int main()

{

char *str = {"Frogs","Do","Not","Die.","They","Croak"};

printf("%c %c %c",*str[0],*str[1],*str[2]);//expected F D N

printf("n%u %u %u",str[0],str[1],str[2]);//expected 1000 1006 1003

}


this output is based on the assumption that froak begins at 1000



the output is as follows



F D N

2162395060 2162395057 2162395053


how can that be possible, here the address is decreasing for str[0] to str[2], printing the address of str[3], str[4], str[5], shows no pattern and rather have abrupt changes in the addresses










share|improve this question




















  • 4





    The compiler may place literal strings anywhere it pleases. There doesn't even have to be a pattern that the location is increasing only.

    – Some programmer dude
    Nov 20 '18 at 12:38






  • 2





    On a slightly related note, to use printf to print pointers, use the format specifier "%p" to print void * pointers (and you really should cast the pointers). The "%u" format expects unsigned int arguments, and pointers aren't unsigned int (and may not even be the same size).

    – Some programmer dude
    Nov 20 '18 at 12:39








  • 1





    Does it really matter, or it is just for curiosity?

    – Cid
    Nov 20 '18 at 12:41






  • 2





    On a slightly unrelated note, what reasoning could you have to expect 1000 1006 1003 as the addresses? There are lots of things wrong with those three numbers.

    – usr2564301
    Nov 20 '18 at 12:42






  • 1





    @Cid it was only for curiosity because right now I am learning pointers in c

    – rishabh jain
    Nov 20 '18 at 13:04
















0















    #include<stdio.h>

int main()

{

char *str = {"Frogs","Do","Not","Die.","They","Croak"};

printf("%c %c %c",*str[0],*str[1],*str[2]);//expected F D N

printf("n%u %u %u",str[0],str[1],str[2]);//expected 1000 1006 1003

}


this output is based on the assumption that froak begins at 1000



the output is as follows



F D N

2162395060 2162395057 2162395053


how can that be possible, here the address is decreasing for str[0] to str[2], printing the address of str[3], str[4], str[5], shows no pattern and rather have abrupt changes in the addresses










share|improve this question




















  • 4





    The compiler may place literal strings anywhere it pleases. There doesn't even have to be a pattern that the location is increasing only.

    – Some programmer dude
    Nov 20 '18 at 12:38






  • 2





    On a slightly related note, to use printf to print pointers, use the format specifier "%p" to print void * pointers (and you really should cast the pointers). The "%u" format expects unsigned int arguments, and pointers aren't unsigned int (and may not even be the same size).

    – Some programmer dude
    Nov 20 '18 at 12:39








  • 1





    Does it really matter, or it is just for curiosity?

    – Cid
    Nov 20 '18 at 12:41






  • 2





    On a slightly unrelated note, what reasoning could you have to expect 1000 1006 1003 as the addresses? There are lots of things wrong with those three numbers.

    – usr2564301
    Nov 20 '18 at 12:42






  • 1





    @Cid it was only for curiosity because right now I am learning pointers in c

    – rishabh jain
    Nov 20 '18 at 13:04














0












0








0








    #include<stdio.h>

int main()

{

char *str = {"Frogs","Do","Not","Die.","They","Croak"};

printf("%c %c %c",*str[0],*str[1],*str[2]);//expected F D N

printf("n%u %u %u",str[0],str[1],str[2]);//expected 1000 1006 1003

}


this output is based on the assumption that froak begins at 1000



the output is as follows



F D N

2162395060 2162395057 2162395053


how can that be possible, here the address is decreasing for str[0] to str[2], printing the address of str[3], str[4], str[5], shows no pattern and rather have abrupt changes in the addresses










share|improve this question
















    #include<stdio.h>

int main()

{

char *str = {"Frogs","Do","Not","Die.","They","Croak"};

printf("%c %c %c",*str[0],*str[1],*str[2]);//expected F D N

printf("n%u %u %u",str[0],str[1],str[2]);//expected 1000 1006 1003

}


this output is based on the assumption that froak begins at 1000



the output is as follows



F D N

2162395060 2162395057 2162395053


how can that be possible, here the address is decreasing for str[0] to str[2], printing the address of str[3], str[4], str[5], shows no pattern and rather have abrupt changes in the addresses







c arrays string pointers c-strings






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 13:05









Dennis Vash

609317




609317










asked Nov 20 '18 at 12:28









rishabh jainrishabh jain

285




285








  • 4





    The compiler may place literal strings anywhere it pleases. There doesn't even have to be a pattern that the location is increasing only.

    – Some programmer dude
    Nov 20 '18 at 12:38






  • 2





    On a slightly related note, to use printf to print pointers, use the format specifier "%p" to print void * pointers (and you really should cast the pointers). The "%u" format expects unsigned int arguments, and pointers aren't unsigned int (and may not even be the same size).

    – Some programmer dude
    Nov 20 '18 at 12:39








  • 1





    Does it really matter, or it is just for curiosity?

    – Cid
    Nov 20 '18 at 12:41






  • 2





    On a slightly unrelated note, what reasoning could you have to expect 1000 1006 1003 as the addresses? There are lots of things wrong with those three numbers.

    – usr2564301
    Nov 20 '18 at 12:42






  • 1





    @Cid it was only for curiosity because right now I am learning pointers in c

    – rishabh jain
    Nov 20 '18 at 13:04














  • 4





    The compiler may place literal strings anywhere it pleases. There doesn't even have to be a pattern that the location is increasing only.

    – Some programmer dude
    Nov 20 '18 at 12:38






  • 2





    On a slightly related note, to use printf to print pointers, use the format specifier "%p" to print void * pointers (and you really should cast the pointers). The "%u" format expects unsigned int arguments, and pointers aren't unsigned int (and may not even be the same size).

    – Some programmer dude
    Nov 20 '18 at 12:39








  • 1





    Does it really matter, or it is just for curiosity?

    – Cid
    Nov 20 '18 at 12:41






  • 2





    On a slightly unrelated note, what reasoning could you have to expect 1000 1006 1003 as the addresses? There are lots of things wrong with those three numbers.

    – usr2564301
    Nov 20 '18 at 12:42






  • 1





    @Cid it was only for curiosity because right now I am learning pointers in c

    – rishabh jain
    Nov 20 '18 at 13:04








4




4





The compiler may place literal strings anywhere it pleases. There doesn't even have to be a pattern that the location is increasing only.

– Some programmer dude
Nov 20 '18 at 12:38





The compiler may place literal strings anywhere it pleases. There doesn't even have to be a pattern that the location is increasing only.

– Some programmer dude
Nov 20 '18 at 12:38




2




2





On a slightly related note, to use printf to print pointers, use the format specifier "%p" to print void * pointers (and you really should cast the pointers). The "%u" format expects unsigned int arguments, and pointers aren't unsigned int (and may not even be the same size).

– Some programmer dude
Nov 20 '18 at 12:39







On a slightly related note, to use printf to print pointers, use the format specifier "%p" to print void * pointers (and you really should cast the pointers). The "%u" format expects unsigned int arguments, and pointers aren't unsigned int (and may not even be the same size).

– Some programmer dude
Nov 20 '18 at 12:39






1




1





Does it really matter, or it is just for curiosity?

– Cid
Nov 20 '18 at 12:41





Does it really matter, or it is just for curiosity?

– Cid
Nov 20 '18 at 12:41




2




2





On a slightly unrelated note, what reasoning could you have to expect 1000 1006 1003 as the addresses? There are lots of things wrong with those three numbers.

– usr2564301
Nov 20 '18 at 12:42





On a slightly unrelated note, what reasoning could you have to expect 1000 1006 1003 as the addresses? There are lots of things wrong with those three numbers.

– usr2564301
Nov 20 '18 at 12:42




1




1





@Cid it was only for curiosity because right now I am learning pointers in c

– rishabh jain
Nov 20 '18 at 13:04





@Cid it was only for curiosity because right now I am learning pointers in c

– rishabh jain
Nov 20 '18 at 13:04












1 Answer
1






active

oldest

votes


















4














You are printing the addresses of three string constants. The compiler is under no obligation to organize the string constants in any predictable fashion.



The compiler is required to provide an array of pointers. The array can be accessed sequentially to obtain addresses of the string constants, but the string constants may be stored in any location which the compiler deems efficient or useful.



I ran the same code on mac OS using AppleClang 10.0.0.10001044 and got the following output:



F D N
104431486 104431492 104431495


As you can see, the pointers are sequential using AppleClang.



However, that is irrelevant. Nothing in your code should depend on how the compiler chooses to allocate memory for the string constants.






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%2f53393004%2fexpected-output-and-the-practical-output-not-matching-please-explain-the-logic%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









    4














    You are printing the addresses of three string constants. The compiler is under no obligation to organize the string constants in any predictable fashion.



    The compiler is required to provide an array of pointers. The array can be accessed sequentially to obtain addresses of the string constants, but the string constants may be stored in any location which the compiler deems efficient or useful.



    I ran the same code on mac OS using AppleClang 10.0.0.10001044 and got the following output:



    F D N
    104431486 104431492 104431495


    As you can see, the pointers are sequential using AppleClang.



    However, that is irrelevant. Nothing in your code should depend on how the compiler chooses to allocate memory for the string constants.






    share|improve this answer




























      4














      You are printing the addresses of three string constants. The compiler is under no obligation to organize the string constants in any predictable fashion.



      The compiler is required to provide an array of pointers. The array can be accessed sequentially to obtain addresses of the string constants, but the string constants may be stored in any location which the compiler deems efficient or useful.



      I ran the same code on mac OS using AppleClang 10.0.0.10001044 and got the following output:



      F D N
      104431486 104431492 104431495


      As you can see, the pointers are sequential using AppleClang.



      However, that is irrelevant. Nothing in your code should depend on how the compiler chooses to allocate memory for the string constants.






      share|improve this answer


























        4












        4








        4







        You are printing the addresses of three string constants. The compiler is under no obligation to organize the string constants in any predictable fashion.



        The compiler is required to provide an array of pointers. The array can be accessed sequentially to obtain addresses of the string constants, but the string constants may be stored in any location which the compiler deems efficient or useful.



        I ran the same code on mac OS using AppleClang 10.0.0.10001044 and got the following output:



        F D N
        104431486 104431492 104431495


        As you can see, the pointers are sequential using AppleClang.



        However, that is irrelevant. Nothing in your code should depend on how the compiler chooses to allocate memory for the string constants.






        share|improve this answer













        You are printing the addresses of three string constants. The compiler is under no obligation to organize the string constants in any predictable fashion.



        The compiler is required to provide an array of pointers. The array can be accessed sequentially to obtain addresses of the string constants, but the string constants may be stored in any location which the compiler deems efficient or useful.



        I ran the same code on mac OS using AppleClang 10.0.0.10001044 and got the following output:



        F D N
        104431486 104431492 104431495


        As you can see, the pointers are sequential using AppleClang.



        However, that is irrelevant. Nothing in your code should depend on how the compiler chooses to allocate memory for the string constants.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 12:40









        John MurrayJohn Murray

        809514




        809514






























            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%2f53393004%2fexpected-output-and-the-practical-output-not-matching-please-explain-the-logic%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

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            How to fix TextFormField cause rebuild widget in Flutter