#include <stdlib.h>
#include <stdio.h>
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 < 5; i++)
{
b[i] = a[i]+1;
*p = a[i]+1;
p++;
}
for (int i = 0; i < 5; i++)
{
printf (" %i \t %i \t %i \n", *p++, b[i], a[i]);
}
return 0;
}
For this code I get why the output for a and b but why does the pointer have the same value of a?
*p is b[0] = a[0]+1, isn't it? So p++ means next address over for b so it's b[1]=a[1]+1.
ie
*p b a
1 2 1
2 3 2
3 4 3
4 5 4
5 6 5
*p b a, nota b *p.int b[sizeof(a)/sizeof(a[0])] = {...and replace the5values by the same term. This way, you can change the arrayaand everything will be ok.