c pointers and array

前端 未结 4 1687
后悔当初
后悔当初 2021-01-18 02:20
#include 
#include 

int main (void)
{
    int a[] = {1,2,3,4,5};
    int b[] = {0,0,0,0,0};
    int *p = b;

    for (int i =0; i <         


        
4条回答
  •  天命终不由人
    2021-01-18 03:02

    You are getting undefined behavior. At the end of the first loop p points to "one past the end" of b. Without resetting it, you then dereference it and continue to increment it, both of which cause undefined behavior.

    It may be that on your implementation the array a is stored immediately after array b and that p has started to point into array a. This would be one possible "undefined" bahaviour.

提交回复
热议问题