Do previous declarations in C change how pointers are initialized? [duplicate]












-3
















This question already has an answer here:




  • pointer default value .?

    4 answers




How are pointers in C initialized? It seems like previous declarations change how they are initialized.



Consider the following example:



int *a;
printf("a: %pn", (void*)a);


This code snippet results in



a: (nil)


So one could think that variables at function start are initialized with null, but if I execute this code:



int *a;
for(int i = 0; i < 1; i++){
int *b;
printf("a: %pn", (void*)a);
printf("b: %p", (void*)b);
}


This is the result:



a: 0x7ffff3bb2e40
b: (nil)


How can I determine how the variables are initialized?










share|improve this question















marked as duplicate by Pedro Silva, gsamaras 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 23 '18 at 8:56


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.














  • 5





    Undefined behavior is undefined.

    – EOF
    Nov 22 '18 at 21:12











  • The pointer either has some random value or is zeroed, depending on how it is declared.

    – CoffeeTableEspresso
    Nov 22 '18 at 21:13











  • You can determine if the variable is initialized or not based on where its definition occurs in the program (and whether it's marked static or extern)

    – M.M
    Nov 22 '18 at 21:42


















-3
















This question already has an answer here:




  • pointer default value .?

    4 answers




How are pointers in C initialized? It seems like previous declarations change how they are initialized.



Consider the following example:



int *a;
printf("a: %pn", (void*)a);


This code snippet results in



a: (nil)


So one could think that variables at function start are initialized with null, but if I execute this code:



int *a;
for(int i = 0; i < 1; i++){
int *b;
printf("a: %pn", (void*)a);
printf("b: %p", (void*)b);
}


This is the result:



a: 0x7ffff3bb2e40
b: (nil)


How can I determine how the variables are initialized?










share|improve this question















marked as duplicate by Pedro Silva, gsamaras 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 23 '18 at 8:56


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.














  • 5





    Undefined behavior is undefined.

    – EOF
    Nov 22 '18 at 21:12











  • The pointer either has some random value or is zeroed, depending on how it is declared.

    – CoffeeTableEspresso
    Nov 22 '18 at 21:13











  • You can determine if the variable is initialized or not based on where its definition occurs in the program (and whether it's marked static or extern)

    – M.M
    Nov 22 '18 at 21:42
















-3












-3








-3









This question already has an answer here:




  • pointer default value .?

    4 answers




How are pointers in C initialized? It seems like previous declarations change how they are initialized.



Consider the following example:



int *a;
printf("a: %pn", (void*)a);


This code snippet results in



a: (nil)


So one could think that variables at function start are initialized with null, but if I execute this code:



int *a;
for(int i = 0; i < 1; i++){
int *b;
printf("a: %pn", (void*)a);
printf("b: %p", (void*)b);
}


This is the result:



a: 0x7ffff3bb2e40
b: (nil)


How can I determine how the variables are initialized?










share|improve this question

















This question already has an answer here:




  • pointer default value .?

    4 answers




How are pointers in C initialized? It seems like previous declarations change how they are initialized.



Consider the following example:



int *a;
printf("a: %pn", (void*)a);


This code snippet results in



a: (nil)


So one could think that variables at function start are initialized with null, but if I execute this code:



int *a;
for(int i = 0; i < 1; i++){
int *b;
printf("a: %pn", (void*)a);
printf("b: %p", (void*)b);
}


This is the result:



a: 0x7ffff3bb2e40
b: (nil)


How can I determine how the variables are initialized?





This question already has an answer here:




  • pointer default value .?

    4 answers








c pointers memory initialization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 21:51









M.M

106k11120244




106k11120244










asked Nov 22 '18 at 21:08









NightRain23NightRain23

183




183




marked as duplicate by Pedro Silva, gsamaras 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 23 '18 at 8:56


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 Pedro Silva, gsamaras 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 23 '18 at 8:56


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.










  • 5





    Undefined behavior is undefined.

    – EOF
    Nov 22 '18 at 21:12











  • The pointer either has some random value or is zeroed, depending on how it is declared.

    – CoffeeTableEspresso
    Nov 22 '18 at 21:13











  • You can determine if the variable is initialized or not based on where its definition occurs in the program (and whether it's marked static or extern)

    – M.M
    Nov 22 '18 at 21:42
















  • 5





    Undefined behavior is undefined.

    – EOF
    Nov 22 '18 at 21:12











  • The pointer either has some random value or is zeroed, depending on how it is declared.

    – CoffeeTableEspresso
    Nov 22 '18 at 21:13











  • You can determine if the variable is initialized or not based on where its definition occurs in the program (and whether it's marked static or extern)

    – M.M
    Nov 22 '18 at 21:42










5




5





Undefined behavior is undefined.

– EOF
Nov 22 '18 at 21:12





Undefined behavior is undefined.

– EOF
Nov 22 '18 at 21:12













The pointer either has some random value or is zeroed, depending on how it is declared.

– CoffeeTableEspresso
Nov 22 '18 at 21:13





The pointer either has some random value or is zeroed, depending on how it is declared.

– CoffeeTableEspresso
Nov 22 '18 at 21:13













You can determine if the variable is initialized or not based on where its definition occurs in the program (and whether it's marked static or extern)

– M.M
Nov 22 '18 at 21:42







You can determine if the variable is initialized or not based on where its definition occurs in the program (and whether it's marked static or extern)

– M.M
Nov 22 '18 at 21:42














3 Answers
3






active

oldest

votes


















1














If a pointer is defined at file scope, it is initialized to NULL.



If it is defined at block scope, the pointer is uninitialized, so it could have any random value, including NULL.






share|improve this answer



















  • 5





    uninitialized variables have indeterminate value, not any random value. The value could appear to change by itself between inspections, compare unequal with itself, and so on.

    – M.M
    Nov 22 '18 at 21:38





















1














Given that you are not assigning an initial value, it depends on what there is on the memory beforehand. So there are two possibilities




  • Garbage. Indeterminate values which come from a previous execution or status, etc...this l yields an undefined behaviour.

  • Initialization during startup. It is quite common to initialize to zero some segments of memory such us the bss segment during the startup (before main()). In this case, you are not initializating the variable, you are initializating the bunch of memory in which the variable is allocated, anyway, this won't yield an undefined behaviour.


Edited for accuracy due to M.M 's comment.






share|improve this answer

































    0















    How are pointers in C initialized?




    In C, local pointers (in automatic variables) are not initialized unless you code explicitly their initialization. You have an undefined behavior. Be scared.



    The only pointers which are implicitly initialized are those in global or static variables (so called file scope variables). In practice, such pointers are initialized to all zero bits, which usually means the NULL pointer.



    In practice, with a modern compiler like a recent GCC, you should enable all warnings and debug info (e.g. compile with gcc -Wall -Wextra -g). Then you'll get warnings. Improve your code to have none.






    share|improve this answer
































      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      If a pointer is defined at file scope, it is initialized to NULL.



      If it is defined at block scope, the pointer is uninitialized, so it could have any random value, including NULL.






      share|improve this answer



















      • 5





        uninitialized variables have indeterminate value, not any random value. The value could appear to change by itself between inspections, compare unequal with itself, and so on.

        – M.M
        Nov 22 '18 at 21:38


















      1














      If a pointer is defined at file scope, it is initialized to NULL.



      If it is defined at block scope, the pointer is uninitialized, so it could have any random value, including NULL.






      share|improve this answer



















      • 5





        uninitialized variables have indeterminate value, not any random value. The value could appear to change by itself between inspections, compare unequal with itself, and so on.

        – M.M
        Nov 22 '18 at 21:38
















      1












      1








      1







      If a pointer is defined at file scope, it is initialized to NULL.



      If it is defined at block scope, the pointer is uninitialized, so it could have any random value, including NULL.






      share|improve this answer













      If a pointer is defined at file scope, it is initialized to NULL.



      If it is defined at block scope, the pointer is uninitialized, so it could have any random value, including NULL.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 22 '18 at 21:28









      dbushdbush

      103k13108145




      103k13108145








      • 5





        uninitialized variables have indeterminate value, not any random value. The value could appear to change by itself between inspections, compare unequal with itself, and so on.

        – M.M
        Nov 22 '18 at 21:38
















      • 5





        uninitialized variables have indeterminate value, not any random value. The value could appear to change by itself between inspections, compare unequal with itself, and so on.

        – M.M
        Nov 22 '18 at 21:38










      5




      5





      uninitialized variables have indeterminate value, not any random value. The value could appear to change by itself between inspections, compare unequal with itself, and so on.

      – M.M
      Nov 22 '18 at 21:38







      uninitialized variables have indeterminate value, not any random value. The value could appear to change by itself between inspections, compare unequal with itself, and so on.

      – M.M
      Nov 22 '18 at 21:38















      1














      Given that you are not assigning an initial value, it depends on what there is on the memory beforehand. So there are two possibilities




      • Garbage. Indeterminate values which come from a previous execution or status, etc...this l yields an undefined behaviour.

      • Initialization during startup. It is quite common to initialize to zero some segments of memory such us the bss segment during the startup (before main()). In this case, you are not initializating the variable, you are initializating the bunch of memory in which the variable is allocated, anyway, this won't yield an undefined behaviour.


      Edited for accuracy due to M.M 's comment.






      share|improve this answer






























        1














        Given that you are not assigning an initial value, it depends on what there is on the memory beforehand. So there are two possibilities




        • Garbage. Indeterminate values which come from a previous execution or status, etc...this l yields an undefined behaviour.

        • Initialization during startup. It is quite common to initialize to zero some segments of memory such us the bss segment during the startup (before main()). In this case, you are not initializating the variable, you are initializating the bunch of memory in which the variable is allocated, anyway, this won't yield an undefined behaviour.


        Edited for accuracy due to M.M 's comment.






        share|improve this answer




























          1












          1








          1







          Given that you are not assigning an initial value, it depends on what there is on the memory beforehand. So there are two possibilities




          • Garbage. Indeterminate values which come from a previous execution or status, etc...this l yields an undefined behaviour.

          • Initialization during startup. It is quite common to initialize to zero some segments of memory such us the bss segment during the startup (before main()). In this case, you are not initializating the variable, you are initializating the bunch of memory in which the variable is allocated, anyway, this won't yield an undefined behaviour.


          Edited for accuracy due to M.M 's comment.






          share|improve this answer















          Given that you are not assigning an initial value, it depends on what there is on the memory beforehand. So there are two possibilities




          • Garbage. Indeterminate values which come from a previous execution or status, etc...this l yields an undefined behaviour.

          • Initialization during startup. It is quite common to initialize to zero some segments of memory such us the bss segment during the startup (before main()). In this case, you are not initializating the variable, you are initializating the bunch of memory in which the variable is allocated, anyway, this won't yield an undefined behaviour.


          Edited for accuracy due to M.M 's comment.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 8:36

























          answered Nov 22 '18 at 21:19









          JoseJose

          1,291516




          1,291516























              0















              How are pointers in C initialized?




              In C, local pointers (in automatic variables) are not initialized unless you code explicitly their initialization. You have an undefined behavior. Be scared.



              The only pointers which are implicitly initialized are those in global or static variables (so called file scope variables). In practice, such pointers are initialized to all zero bits, which usually means the NULL pointer.



              In practice, with a modern compiler like a recent GCC, you should enable all warnings and debug info (e.g. compile with gcc -Wall -Wextra -g). Then you'll get warnings. Improve your code to have none.






              share|improve this answer






























                0















                How are pointers in C initialized?




                In C, local pointers (in automatic variables) are not initialized unless you code explicitly their initialization. You have an undefined behavior. Be scared.



                The only pointers which are implicitly initialized are those in global or static variables (so called file scope variables). In practice, such pointers are initialized to all zero bits, which usually means the NULL pointer.



                In practice, with a modern compiler like a recent GCC, you should enable all warnings and debug info (e.g. compile with gcc -Wall -Wextra -g). Then you'll get warnings. Improve your code to have none.






                share|improve this answer




























                  0












                  0








                  0








                  How are pointers in C initialized?




                  In C, local pointers (in automatic variables) are not initialized unless you code explicitly their initialization. You have an undefined behavior. Be scared.



                  The only pointers which are implicitly initialized are those in global or static variables (so called file scope variables). In practice, such pointers are initialized to all zero bits, which usually means the NULL pointer.



                  In practice, with a modern compiler like a recent GCC, you should enable all warnings and debug info (e.g. compile with gcc -Wall -Wextra -g). Then you'll get warnings. Improve your code to have none.






                  share|improve this answer
















                  How are pointers in C initialized?




                  In C, local pointers (in automatic variables) are not initialized unless you code explicitly their initialization. You have an undefined behavior. Be scared.



                  The only pointers which are implicitly initialized are those in global or static variables (so called file scope variables). In practice, such pointers are initialized to all zero bits, which usually means the NULL pointer.



                  In practice, with a modern compiler like a recent GCC, you should enable all warnings and debug info (e.g. compile with gcc -Wall -Wextra -g). Then you'll get warnings. Improve your code to have none.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 '18 at 8:47

























                  answered Nov 23 '18 at 8:40









                  Basile StarynkevitchBasile Starynkevitch

                  179k13173374




                  179k13173374















                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()