I am trying to copy the values from one array to another using strictly pointer arithmetic. This is the code I have right now:
int *p;
int arraySize = 20;
int array[arraySize];
for (p = array; p< array+(sizeof(array)/sizeof(int)); p++){
int random = rand() % 200;
*p = random;
}
for (p = array; p< array+(sizeof(array)/sizeof(int)); p++){
printf("%d\t%x\n", *p, p);
}
//the code above works fine
printf("\n");
//grow the new array by one to insert value at end later
int array2[(arraySize+1)];
int *p2;
for(p2 = array2; p2< array2+(sizeof(array2)/sizeof(int)); p2++){
*(p2) = *(p);
}
for(p2 = array2; p2< array2+(sizeof(array2)/sizeof(int)); p2++){
printf("%d\t%x\n", *p2, p2);
}
But when i run the code all that is outputted is 0 at each of the memory locations. what am i doing wrong that is preventing the values from being copied over?
pis not incremented.p2, but it never changesp. Voting to close as a typo.[x]syntax,*(ptr + (x * sizeof(int)))is exactly what the compiler is doing (whereintis the array type, as this example)...