#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 <
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.
I think what you need to do is to add a p = p - 5;
#include <stdio.h>
int main (void)
{
int a[] = {1,2,3,4,5};
int b[] = {0,0,0,0,0};
int *p = b;
int i =0;
for (i =0; i < 5; i++)
{
b[i] = a[i]+1;
*p = a[i]+1;
p++;
}
p = p - 5;
for (i = 0; i < 5; i++)
{
printf (" %i \t %i \t %i \n", *p++, b[i], a[i]);
}
return 0;
}
you shouldnt make seperate loop for printing and incrementing the values of the array . do both in same loop and do the follouwing to get ur output :) #include #include
int main(void)
{
int a[]={1,2,3,4,5};
int b[]={0,0,0,0,0};
int c[]={0,0,0,0,0};
int *p;
int i;
p=c;
for( i =0 ; i<5 ; i++)
{
b[i]=a[i]+1;
*p=b[i]-1;
//*p++;
//for( i =0 ; i<5 ; i++)
printf(" %i \t %i \t %i \n" ,*p,b[i],a[i]);
}
return 0;
}
after the first for{},p point at b[5],but the size of b is 5,so b[5] value is unknow,the printf *p is the same value as a[i],the reason may be in memory b[5] is a[0].