Why this two sizeof gives different result? [duplicate]









up vote
1
down vote

favorite













This question already has an answer here:



  • Length of array in function argument

    8 answers



  • Why isn't the size of an array parameter the same as within main?

    13 answers



I understand if you have an array, and you do sizeof, it gives you the number of bytes that block of memory occupies, but I have some confusion regarding the follow situation.



int mylen(const char *str) 
return sizeof(str);


int main(void)
char str = "hello";

printf("%dn", sizeof(str)); // this gives 6
printf("%dn", mylen(str)); // this gives 8



I understand mylen is just returning the sizeof char pointer, therefore 8, but in that case, why the first one works? It this the subtle distinction between str and char *?










share|improve this question













marked as duplicate by Eric Postpischil c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 10 at 4:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    Didn't you answer your own question? I understand if you have an array, and you do sizeof, it gives you the number of bytes that block of memory occupies and I understand mylen is just returning the sizeof char pointer, therefore 8, which answers why the results are different.
    – tkausl
    Nov 10 at 4:30










  • char str is not a pointer? const char* is. There.
    – DeiDei
    Nov 10 at 4:30











  • @DeiDei str in char str = "hello"; is an array of 6 char.
    – Swordfish
    Nov 10 at 4:31











  • Given const char *str, just what do you expect sizeof(str) to return?
    – Andrew Henle
    Nov 10 at 4:32






  • 3




    printf("%dn", sizeof(str)); has undefined behavior. sizeof yields a size_t, not an int.
    – melpomene
    Nov 10 at 4:34














up vote
1
down vote

favorite













This question already has an answer here:



  • Length of array in function argument

    8 answers



  • Why isn't the size of an array parameter the same as within main?

    13 answers



I understand if you have an array, and you do sizeof, it gives you the number of bytes that block of memory occupies, but I have some confusion regarding the follow situation.



int mylen(const char *str) 
return sizeof(str);


int main(void)
char str = "hello";

printf("%dn", sizeof(str)); // this gives 6
printf("%dn", mylen(str)); // this gives 8



I understand mylen is just returning the sizeof char pointer, therefore 8, but in that case, why the first one works? It this the subtle distinction between str and char *?










share|improve this question













marked as duplicate by Eric Postpischil c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 10 at 4:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    Didn't you answer your own question? I understand if you have an array, and you do sizeof, it gives you the number of bytes that block of memory occupies and I understand mylen is just returning the sizeof char pointer, therefore 8, which answers why the results are different.
    – tkausl
    Nov 10 at 4:30










  • char str is not a pointer? const char* is. There.
    – DeiDei
    Nov 10 at 4:30











  • @DeiDei str in char str = "hello"; is an array of 6 char.
    – Swordfish
    Nov 10 at 4:31











  • Given const char *str, just what do you expect sizeof(str) to return?
    – Andrew Henle
    Nov 10 at 4:32






  • 3




    printf("%dn", sizeof(str)); has undefined behavior. sizeof yields a size_t, not an int.
    – melpomene
    Nov 10 at 4:34












up vote
1
down vote

favorite









up vote
1
down vote

favorite












This question already has an answer here:



  • Length of array in function argument

    8 answers



  • Why isn't the size of an array parameter the same as within main?

    13 answers



I understand if you have an array, and you do sizeof, it gives you the number of bytes that block of memory occupies, but I have some confusion regarding the follow situation.



int mylen(const char *str) 
return sizeof(str);


int main(void)
char str = "hello";

printf("%dn", sizeof(str)); // this gives 6
printf("%dn", mylen(str)); // this gives 8



I understand mylen is just returning the sizeof char pointer, therefore 8, but in that case, why the first one works? It this the subtle distinction between str and char *?










share|improve this question














This question already has an answer here:



  • Length of array in function argument

    8 answers



  • Why isn't the size of an array parameter the same as within main?

    13 answers



I understand if you have an array, and you do sizeof, it gives you the number of bytes that block of memory occupies, but I have some confusion regarding the follow situation.



int mylen(const char *str) 
return sizeof(str);


int main(void)
char str = "hello";

printf("%dn", sizeof(str)); // this gives 6
printf("%dn", mylen(str)); // this gives 8



I understand mylen is just returning the sizeof char pointer, therefore 8, but in that case, why the first one works? It this the subtle distinction between str and char *?





This question already has an answer here:



  • Length of array in function argument

    8 answers



  • Why isn't the size of an array parameter the same as within main?

    13 answers







c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 4:28









Tang Dexian

1314




1314




marked as duplicate by Eric Postpischil c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 10 at 4:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Eric Postpischil c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Nov 10 at 4:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 2




    Didn't you answer your own question? I understand if you have an array, and you do sizeof, it gives you the number of bytes that block of memory occupies and I understand mylen is just returning the sizeof char pointer, therefore 8, which answers why the results are different.
    – tkausl
    Nov 10 at 4:30










  • char str is not a pointer? const char* is. There.
    – DeiDei
    Nov 10 at 4:30











  • @DeiDei str in char str = "hello"; is an array of 6 char.
    – Swordfish
    Nov 10 at 4:31











  • Given const char *str, just what do you expect sizeof(str) to return?
    – Andrew Henle
    Nov 10 at 4:32






  • 3




    printf("%dn", sizeof(str)); has undefined behavior. sizeof yields a size_t, not an int.
    – melpomene
    Nov 10 at 4:34












  • 2




    Didn't you answer your own question? I understand if you have an array, and you do sizeof, it gives you the number of bytes that block of memory occupies and I understand mylen is just returning the sizeof char pointer, therefore 8, which answers why the results are different.
    – tkausl
    Nov 10 at 4:30










  • char str is not a pointer? const char* is. There.
    – DeiDei
    Nov 10 at 4:30











  • @DeiDei str in char str = "hello"; is an array of 6 char.
    – Swordfish
    Nov 10 at 4:31











  • Given const char *str, just what do you expect sizeof(str) to return?
    – Andrew Henle
    Nov 10 at 4:32






  • 3




    printf("%dn", sizeof(str)); has undefined behavior. sizeof yields a size_t, not an int.
    – melpomene
    Nov 10 at 4:34







2




2




Didn't you answer your own question? I understand if you have an array, and you do sizeof, it gives you the number of bytes that block of memory occupies and I understand mylen is just returning the sizeof char pointer, therefore 8, which answers why the results are different.
– tkausl
Nov 10 at 4:30




Didn't you answer your own question? I understand if you have an array, and you do sizeof, it gives you the number of bytes that block of memory occupies and I understand mylen is just returning the sizeof char pointer, therefore 8, which answers why the results are different.
– tkausl
Nov 10 at 4:30












char str is not a pointer? const char* is. There.
– DeiDei
Nov 10 at 4:30





char str is not a pointer? const char* is. There.
– DeiDei
Nov 10 at 4:30













@DeiDei str in char str = "hello"; is an array of 6 char.
– Swordfish
Nov 10 at 4:31





@DeiDei str in char str = "hello"; is an array of 6 char.
– Swordfish
Nov 10 at 4:31













Given const char *str, just what do you expect sizeof(str) to return?
– Andrew Henle
Nov 10 at 4:32




Given const char *str, just what do you expect sizeof(str) to return?
– Andrew Henle
Nov 10 at 4:32




3




3




printf("%dn", sizeof(str)); has undefined behavior. sizeof yields a size_t, not an int.
– melpomene
Nov 10 at 4:34




printf("%dn", sizeof(str)); has undefined behavior. sizeof yields a size_t, not an int.
– melpomene
Nov 10 at 4:34












1 Answer
1






active

oldest

votes

















up vote
0
down vote













It is the same reason why in



char foo[6];
sizeof(foo); // yields 6


and in



char *bar;
sizeof(bar); // yields 8 (or in general: the size of a pointer on your system)


btw, since the result of the sizeof-operator is of type size_t, your mylen() should return size_t (defined in <stddef.h>) instead of int.



Also, if you want to printf() a size_t you'll have to use "%zu", not "%d".



For more detail you might want to read something on array-to-pointer decay: Why do arrays in C decay to pointers?






share|improve this answer





























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    It is the same reason why in



    char foo[6];
    sizeof(foo); // yields 6


    and in



    char *bar;
    sizeof(bar); // yields 8 (or in general: the size of a pointer on your system)


    btw, since the result of the sizeof-operator is of type size_t, your mylen() should return size_t (defined in <stddef.h>) instead of int.



    Also, if you want to printf() a size_t you'll have to use "%zu", not "%d".



    For more detail you might want to read something on array-to-pointer decay: Why do arrays in C decay to pointers?






    share|improve this answer


























      up vote
      0
      down vote













      It is the same reason why in



      char foo[6];
      sizeof(foo); // yields 6


      and in



      char *bar;
      sizeof(bar); // yields 8 (or in general: the size of a pointer on your system)


      btw, since the result of the sizeof-operator is of type size_t, your mylen() should return size_t (defined in <stddef.h>) instead of int.



      Also, if you want to printf() a size_t you'll have to use "%zu", not "%d".



      For more detail you might want to read something on array-to-pointer decay: Why do arrays in C decay to pointers?






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        It is the same reason why in



        char foo[6];
        sizeof(foo); // yields 6


        and in



        char *bar;
        sizeof(bar); // yields 8 (or in general: the size of a pointer on your system)


        btw, since the result of the sizeof-operator is of type size_t, your mylen() should return size_t (defined in <stddef.h>) instead of int.



        Also, if you want to printf() a size_t you'll have to use "%zu", not "%d".



        For more detail you might want to read something on array-to-pointer decay: Why do arrays in C decay to pointers?






        share|improve this answer














        It is the same reason why in



        char foo[6];
        sizeof(foo); // yields 6


        and in



        char *bar;
        sizeof(bar); // yields 8 (or in general: the size of a pointer on your system)


        btw, since the result of the sizeof-operator is of type size_t, your mylen() should return size_t (defined in <stddef.h>) instead of int.



        Also, if you want to printf() a size_t you'll have to use "%zu", not "%d".



        For more detail you might want to read something on array-to-pointer decay: Why do arrays in C decay to pointers?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 4:40

























        answered Nov 10 at 4:34









        Swordfish

        8,86311335




        8,86311335













            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus